-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.cpp
127 lines (100 loc) · 2.21 KB
/
enemy.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
#include "enemy.h"
std::vector<Enemy*> Enemy::enemies;
Enemy::Enemy(Application *application, int type, int life, int left, int top, int move)
{
this->application = application;
screen = application->getScreen();
aircraft = application->getWorld()->getAircraft();
std::ostringstream filename;
filename << IMG_ENEMY_PREFIX << type << IMG_ENEMY_SUFFIX;
image = new Image(filename.str());
width = image->getWidth();
height = image->getHeight();
this->life = life;
this->left = left + width / 2;
this->top = top - height;
this->move = move;
addEnemy(this);
application->addUpdater(this);
screen->addDrawer(1, this);
}
Enemy::~Enemy()
{
application->removeUpdater(this);
screen->removeDrawer(1, this);
delete image;
removeEnemy(this);
}
void Enemy::addEnemy(Enemy *enemy)
{
enemies.push_back(enemy);
}
void Enemy::removeEnemy(Enemy *enemy)
{
for (std::vector<Enemy*>::iterator it = enemies.begin() ; it < enemies.end(); it++ )
{
if (*it == enemy)
{
enemies.erase(it);
break;
}
}
}
void Enemy::deleteAll()
{
for (std::vector<Enemy*>::iterator it = enemies.begin() ; it < enemies.end(); it++ )
{
delete *it;
}
}
bool Enemy::checkShotCollision(int damage, int left, int top, int width, int height)
{
bool collision = false;
for (std::vector<Enemy*>::iterator it = enemies.begin() ; it < enemies.end(); it++ )
{
if ((*it)->collide(left, top, width, height))
{
(*it)->damage(damage);
collision = true;
}
}
return collision;
}
void Enemy::update()
{
top += move * ENEMY_SPEED;
if (aircraft->collide(left, top, width, height))
{
aircraft->damage(life * ENEMY_EXPLOSION);
life = 0;
explode();
}
if (top > SCREEN_HEIGHT) {
delete this;
}
}
void Enemy::draw()
{
screen->blitImage(left, top, image);
}
bool Enemy::collide(int left, int top, int width, int height)
{
return
life > 0 &&
left + width >= this->left &&
top + height >= this->top &&
left <= this->left + this->width &&
top <= this->top + this->height;
}
void Enemy::damage(int damage)
{
life -= damage;
if (life < 0) application->computePoints(damage + life);
else application->computePoints(damage);
if (life <= 0) delete this;
}
void Enemy::explode()
{
// TODO: do explosion
delete this;
}