-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollision.cpp
More file actions
243 lines (226 loc) · 8.56 KB
/
collision.cpp
File metadata and controls
243 lines (226 loc) · 8.56 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "collision.h"
#include "generator.h"
void checkCollision() {
checkCollisionPlayer();
checkCollisionEnemy();
checkEnemiesInFrame();
checkBulletsInFrame();
}
void checkCollisionPlayer() {
if(player.alive) {
for(byte i=0; i<numberOfBullets; ++i) {
// First check if player and enemy/bullet collide on the x-coordinate
// and then on the y-coordinate.
if((abs(player.x-bullets[i].x) < player.width-3
&& bullets[i].x >= player.x)
&&(abs(player.y-bullets[i].y) < player.height-1
&& bullets[i].y >= player.y)
&& !bullets[i].playersBullet && player.invincible == 0) {
if(bullets[i].damage >= player.lifepoints) {
player.alive = false;
player.lives--;
bullets[i].alive = false;
break;
}
player.lifepoints -= bullets[i].damage;
bullets[i].alive = false;
}
}
}
// Only check this one if the player did not die due to a bullet.
if(player.alive) {
for(byte i=0; i<numberOfEnemies; ++i) {
if(((enemies[i].x-player.x < player.width && enemies[i].x > player.x)
|| (player.x-enemies[i].x < enemies[i].width && enemies[i].x < player.x))
&& ((enemies[i].y-player.y < player.height && enemies[i].y > player.y)
|| (player.y-enemies[i].y < enemies[i].height && enemies[i].y < player.y))
&& player.invincible == 0) {
player.alive = false;
if(!boss_coming)
enemies[i].alive = false;
player.lives--;
break;
}
}
}
// Check for supplies.
if(player.alive) {
for(byte i=0; i<noOfSupplies; ++i) {
if((abs(player.x-supplies[i].x) < player.width
&& supplies[i].x >= player.x)
&&(abs(player.y-supplies[i].y) < player.height-1
&& supplies[i].y >= player.y)) {
supplies[i].alive = false;
extra_tick = 160;
// Each number is a placeholder for different supplies,
// e.g. extra live weapon upgrade or invincibility
byte type = random(0,8);
// Make getting an extra life more unlikely
if(type == 0 and random(0, 100) < 80)
type = 8;
// which_extra = type;
switch(type) {
case 0:
player.lives++;
break;
case 1:
if(player.speed < 2) {
player.speed++;
} else {
player.score += 5;
}
break;
case 2:
if(player.fireSpeed > 10) {
player.fireSpeed -= 10;
} else {
player.score += 5;
}
break;
case 3:
if(player.maxBullets < 20) {
player.maxBullets++;
} else {
player.score += 5;
}
break;
case 4:
if(player.bulletSpeed < 4) {
player.bulletSpeed++;
} else {
player.score += 5;
}
break;
case 5:
player.invincible = 16;
break;
case 6:
if(player.firetype < 4) {
player.firetype++;
} else {
player.score += 5;
}
break;
case 7:
if(player.fireSpeed > 10) {
player.fireSpeed -= 10;
} else {
player.score += 5;
}
break;
case 8:
player.score += 10;
break;
default:
player.lives++;
}
}
}
}
}
// Checks only collisions between bullets and enemies.
// Collisions of player and enemies are done in checkCollisionPlayer.
void checkCollisionEnemy() {
for(byte i=0; i<numberOfEnemies; ++i) {
for(byte j=0; j<numberOfBullets; ++j) {
if(bullets[j].playersBullet
&& (abs(enemies[i].x-bullets[j].x) < enemies[i].width
&& bullets[j].x >= enemies[i].x)
&&(abs(enemies[i].y-bullets[j].y) < enemies[i].height
&& bullets[j].y >= enemies[i].y)) {
bullets[j].alive = false;
if(enemies[i].lifepoints < bullets[j].damage){
enemies[i].alive = false;
player.score++;
if(enemies[i].shipType == 32) boss_coming = false;
if(enemies[i].supply && noOfSupplies < MAXSUPPLY) {
createSupply(enemies[i].x, enemies[i].y);
}
} else {
enemies[i].lifepoints -= bullets[j].damage;
}
}
}
}
}
void checkEnemiesInFrame() {
for(byte i=0; i<numberOfEnemies; ++i) {
if(enemies[i].shipType != 32 && (enemies[i].x > 128 || enemies[i].y > 64
|| enemies[i].x == 0 || enemies[i].y == 0)) {
enemies[i].alive = false;
}
}
}
void checkBulletsInFrame() {
for(byte i=0; i<numberOfBullets; ++i) {
if(bullets[i].x > 128 || bullets[i].y > 64
|| bullets[i].x == 0 || bullets[i].y == 0) {
bullets[i].alive = false;
}
}
}
void checkSuppliesInFrame() {
for(byte i=0; i<noOfSupplies; ++i) {
if(supplies[i].x > 128 || supplies[i].y > 64
|| supplies[i].x == 0 || supplies[i].y == 0) {
supplies[i].alive = false;
}
}
}
void checkAlive() {
if(!player.alive) {
// Add an explosion for the player and delete all the others
explosions[0].tick = 0;
explosions[0].x = player.x + (player.width >> 1);
explosions[0].y = player.y + (player.height >> 1);
for(byte j = 1; j <= MAXENEMIES; ++j) {
explosions[j].tick = 200;
}
} else {
// int is needed or else i <- 0-1 = 255
for(int i=numberOfBullets-1; i>=0; --i) {
if(!bullets[i].alive) {
// player.bullets should always be bigger than 0 if a player bullet is available...
if(bullets[i].playersBullet && player.bullets > 0) {
player.bullets--;
}
if(i<numberOfBullets-1) {
bullets[i] = bullets[numberOfBullets-1];
}
numberOfBullets--;
}
}
// int is needed or else i <- 0-1 = 255
for(int i=noOfSupplies-1; i>=0; --i) {
if(!supplies[i].alive) {
if(i<noOfSupplies-1) {
supplies[i] = supplies[noOfSupplies-1];
}
noOfSupplies--;
}
}
// For all enemies, if he is not alive, check if he is within the frame.
for(int i=numberOfEnemies-1; i>=0; --i) {
if(!enemies[i].alive) {
byte j=0;
if(enemies[i].x <= 128
&& enemies[i].y <= 64 && enemies[i].x > 0
&& enemies[i].y > 0) {
// Search the first free slot in explosions and add a new explosion.
for(byte j = 0; j <= MAXENEMIES; ++j) {
if(explosions[j].tick >= 75) {
explosions[j].tick = 0;
explosions[j].x = enemies[i].x + (enemies[i].width >> 1);
explosions[j].y = enemies[i].y + (enemies[i].height >> 1);
j = 255;
}
}
}
if(i<numberOfEnemies-1) {
enemies[i] = enemies[numberOfEnemies-1];
}
numberOfEnemies--;
}
}
}
}