-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlevel.cpp
More file actions
204 lines (161 loc) · 5.04 KB
/
level.cpp
File metadata and controls
204 lines (161 loc) · 5.04 KB
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
#include "level.h"
Level::Level(int screenWidth, int screenHeight, int difficulty, Ship *ship, int score, int shotsHit) {
_screenWidth = screenWidth;
_screenHeight = screenHeight;
_difficulty = difficulty;
_ship = ship;
_score = score;
_shotsHit = shotsHit;
if (difficulty % 3 == 0) {
Rectangle bounds(0, 0, _screenWidth, _screenHeight - 32);
Boss *boss = new Boss(_screenWidth / 2, _screenHeight / 4, bounds,
AssetManager::getTexture("enemies"), _difficulty);
_enemies.push_back(boss);
} else {
int totalWidth = 6 * 32 + 5 * 10;
for (int y = 0; y < 2; y++) {
for (int x = 0; x < 6; x++) {
int type;
std::string deathSound;
if (x < 2 || x > 3) {
type = 0;
deathSound = "enemy_death_1";
} else {
type = 1;
deathSound = "enemy_death_2";
}
Rectangle bounds(42 * x, 0, _screenWidth - 42 * (5 - x), _screenHeight);
int enemyX = _screenWidth / 2 - totalWidth + 42 * x;
Enemy *enemy = new Enemy(enemyX, _screenHeight / 4 + 42 * y, bounds,
AssetManager::getTexture("enemies"), type, deathSound, _difficulty);
_enemies.push_back(enemy);
}
}
}
}
Level::~Level() {
for (Enemy *enemy : _enemies) {
delete enemy;
}
_enemies.clear();
_enemyBullets.clear();
_particleManagers.clear();
_powerups.clear();
}
bool Level::update(unsigned int ticks) {
std::list<Enemy *>::iterator enemyIter = _enemies.begin();
_ship->hitTest(&_enemyBullets);
_ship->hitTest(&_powerups);
std::list<Powerup>::iterator powerupIter = _powerups.begin();
while (powerupIter != _powerups.end()) {
powerupIter->update(ticks);
if (powerupIter->didHit()) {
_ship->addPowerup(powerupIter->getType());
}
if (!powerupIter->isAlive()) {
_powerups.erase(powerupIter++);
} else {
++powerupIter;
}
}
while (enemyIter != _enemies.end()) {
(*enemyIter)->update(ticks);
(*enemyIter)->decideShot(*_ship);
(*enemyIter)->hitTest(_ship);
if ((*enemyIter)->hitTest(_ship->getBullets())) {
AssetManager::playSample((*enemyIter)->getSample(), NULL);
++_shotsHit;
_score += (*enemyIter)->getPointsWorth();
Rectangle enemyContainer = (*enemyIter)->getContainer();
ParticleManager particleManager(enemyContainer.getX(), enemyContainer.getY());
_particleManagers.push_back(particleManager);
// TODO: make less constant-y
if (rand() % 100 < 20) {
Rectangle bounds(0, 0, _screenWidth, _screenHeight);
Powerup powerup(enemyContainer.getX(), enemyContainer.getY(),
AssetManager::getTexture("powerups"), rand() % 2, bounds);
_powerups.push_back(powerup);
}
}
if (!(*enemyIter)->isAlive()) {
delete (*enemyIter);
_enemies.erase(enemyIter++);
} else {
if ((*enemyIter)->needsFire()) {
int x = (*enemyIter)->getContainer().getX() +
(*enemyIter)->getContainer().getW() / 4;
Rectangle bounds(0, 0, _screenWidth, _screenHeight);
Bullet newBullet(x, (*enemyIter)->getContainer().getY(),
AssetManager::getTexture("bullet"), false, bounds);
_enemyBullets.push_back(newBullet);
(*enemyIter)->fire();
}
++enemyIter;
}
}
std::list<Bullet>::iterator bulletIter = _enemyBullets.begin();
while (bulletIter != _enemyBullets.end()) {
bulletIter->update(ticks);
if (!bulletIter->isAlive()) {
_enemyBullets.erase(bulletIter++);
} else {
++bulletIter;
}
}
std::list<ParticleManager>::iterator particleManagerIter = _particleManagers.begin();
while (particleManagerIter != _particleManagers.end()) {
particleManagerIter->update(ticks);
if (!particleManagerIter->isAlive()) {
_particleManagers.erase(particleManagerIter++);
} else {
++particleManagerIter;
}
}
return true;
}
void Level::render() {
for (Bullet bullet : _enemyBullets) {
bullet.render();
}
for (ParticleManager particleManager : _particleManagers) {
particleManager.render();
}
for (Powerup powerup : _powerups) {
powerup.render();
}
for (Enemy *enemy : _enemies) {
enemy->render();
}
renderPowerups();
}
bool Level::isComplete() {
return _enemies.size() == 0 && _enemyBullets.size() == 0
&& _particleManagers.size() == 0;
}
void Level::renderPowerups() {
ALLEGRO_FONT *font = AssetManager::getFont("normal");
int y = _screenHeight - 32;
int textOffset = 0;
int ascent = al_get_font_ascent(font);
int toRender = 0;
std::list<ActivePowerup> powerups = _ship->getActivePowerups();
int powerupsToRender[2] = {0, 0};
for (ActivePowerup powerup : powerups) {
if (!powerup.complete) {
++powerupsToRender[powerup.type];
}
}
for (int i = 0; i < 2; i++) {
if (powerupsToRender[i] > 0) {
al_draw_bitmap_region(AssetManager::getTexture("powerups"), 0, 32 * i,
32, 32, 40 * toRender + textOffset, y, NULL);
++toRender;
if (powerupsToRender[i] > 1) {
al_draw_textf(font, al_map_rgb(255, 255, 255),
40 * (i + 1) + textOffset, y + 16 - ascent / 2, ALLEGRO_ALIGN_LEFT,
"x%d", powerupsToRender[i]);
textOffset += al_get_text_width(font, "x1");
}
}
}
}