-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.cpp
206 lines (175 loc) · 5.05 KB
/
Character.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Character.h"
#include "WeaponProperties.h"
#include <cmath>
#include <iostream>
using namespace std;
void Character::AddHealth (unsigned int h)
{
health += h;
addedHealth += h;
}
Character::Character(int health, double xPos, double yPos, int radius, WeaponType activeWeapon):
xPos(xPos),yPos(yPos),xVel(0),yVel(0),radius(radius),health(health),initialHealth(health),addedHealth(0),
attackDelay(0),activeWeapon(activeWeapon){}
Character::~Character()
{
for (unsigned int i = 0; i < weapons.size(); i++)
delete weapons[i];
}
bool Character::CollideWithWall (char xy, int ** grid, int size)
{
if (xy == 'x' && xVel != 0)
{
if (abs (GetY() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) > BOX_PIXEL_WIDTH/2 - radius)
{
return grid[((int)round(xPos+xVel+xVel/abs(xVel)*radius))/BOX_PIXEL_WIDTH][GetY()/BOX_PIXEL_WIDTH] == 1 ||
grid[((int)round(xPos+xVel+xVel/abs(xVel)*radius))/BOX_PIXEL_WIDTH][GetY()/BOX_PIXEL_WIDTH+GetY()/abs(GetY())] == 1;
}
else
return grid[((int)round(xPos+xVel+xVel/abs(xVel)*radius))/BOX_PIXEL_WIDTH][GetY()/BOX_PIXEL_WIDTH] == 1;
}
else if (xy == 'y' && yVel != 0)
{
if (abs (GetX() % BOX_PIXEL_WIDTH - BOX_PIXEL_WIDTH/2) > BOX_PIXEL_WIDTH/2 - radius)
{
return grid[GetX()/BOX_PIXEL_WIDTH][((int)round(yPos+yVel+yVel/abs(yVel)*radius))/BOX_PIXEL_WIDTH] == 1 ||
grid[GetX()/BOX_PIXEL_WIDTH+GetX()/abs(GetX())][((int)round(yPos+yVel+yVel/abs(yVel)*radius))/BOX_PIXEL_WIDTH] == 1;
}
else
return grid[GetX()/BOX_PIXEL_WIDTH][((int)round(yPos+yVel+yVel/abs(yVel)*radius))/BOX_PIXEL_WIDTH] == 1;
}
return false;
}
bool Character::Dead()
{
return health <= 0;
}
WeaponType Character::GetActiveWeapon() const
{
return activeWeapon;
}
int Character::GetAddedHealth() const
{
return addedHealth;
}
int Character::GetHealth() const
{
return health;
}
int Character::GetInitialHealth() const
{
return initialHealth;
}
WeaponProperties &Character::GetWeaponProperties(WeaponType type)
{
return weaponProperties[type];
}
vector<Weapon *> Character::GetWeapons() const
{
return weapons;
}
int Character::GetX () const
{
return static_cast<int>(round(xPos));
}
int Character::GetY () const
{
return static_cast<int>(round(yPos));
}
void Character::Hit (int damage)
{
health -= damage;
if (health < 0)
health = 0;
}
bool Character::InRange(int weaponX, int weaponY, int radius)
{
return sqrt( pow(weaponX-xPos, 2) + pow(weaponY-yPos, 2) ) < radius + this->radius;
}
void Character::NextWeapon()
{
if (activeWeapon == _Gun && GetWeaponProperties(_Grenade).GetWeaponQuantity() != 0)
activeWeapon = _Grenade;
else if (activeWeapon == _Gun && GetWeaponProperties(_WallBreaker).GetWeaponQuantity() != 0)
activeWeapon = _WallBreaker;
else if (activeWeapon == _Gun)
activeWeapon = _Gun;
else if (activeWeapon == _Grenade && GetWeaponProperties(_WallBreaker).GetWeaponQuantity() != 0)
activeWeapon = _WallBreaker;
else if (activeWeapon == _Grenade)
activeWeapon = _Gun;
else if (activeWeapon == _WallBreaker)
activeWeapon = _Gun;
else
cerr << "Invalid active weapon set for Character::NextWeapon\n";
}
void Character::Notify(Weapon * weapon)
{
for (vector<Weapon *>::iterator it = weapons.begin(); it < weapons.end(); ++it)
{
if (weapon == *it)
weapons.erase(it);
}
}
void Character::PrevWeapon()
{
if (activeWeapon == _Gun && GetWeaponProperties(_WallBreaker).GetWeaponQuantity() != 0)
activeWeapon = _WallBreaker;
else if (activeWeapon == _Gun && GetWeaponProperties(_Grenade).GetWeaponQuantity() != 0)
activeWeapon = _Grenade;
else if (activeWeapon == _Gun)
activeWeapon = _Gun;
else if (activeWeapon == _Grenade)
activeWeapon = _Gun;
else if (activeWeapon == _WallBreaker && GetWeaponProperties(_Grenade).GetWeaponQuantity() != 0)
activeWeapon = _Grenade;
else if (activeWeapon == _WallBreaker)
activeWeapon = _Gun;
else
cerr << "Invalid active weapon set for Character::NextWeapon\n";
}
void Character::SetActiveWeapon(WeaponType activeWeapon)
{
this->activeWeapon = activeWeapon;
}
void Character::SetWeaponProperties(WeaponProperties weaponProperties, WeaponType type)
{
this->weaponProperties[type] = weaponProperties;
}
void Character::SetX (int x)
{
this->xPos = x;
}
void Character::SetY (int y)
{
this->yPos = y;
}
bool Character::Visit (AbstractGun &gun)
{
return false;
}
bool Character::Visit (Grenade &grenade)
{
return false;
}
bool Character::Visit (Bomb &bomb)
{
return false;
}
bool Character::Visit (Nuke &nuke)
{
return false;
}
bool Character::Visit (EnemyWeapon &enemyWeapon)
{
return false;
}
bool Character::Visit (WallBreaker &wallBreaker)
{
return false;
// wall breaker doesn't affect characters
}
bool Character::Visit (Weapon &weapon)
{
return false;
}