-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.h
executable file
·160 lines (138 loc) · 6.06 KB
/
game.h
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
typedef struct Game {
void play() {
drawScreenFlash();
drawGrid();
if (!player.hasCrate) {
crate.update();
crate.draw();
drawLocator(crate, 2, false);
} else {
drop.update();
drop.draw();
drawLocator(drop, 2, false);
}
player.update();
player.draw();
// make the camera follow the player
camera.follow(player.x + player.width / 2, player.y + player.height / 2, 54, 27);
if (!player.hasCrate && collide(player.cbox, crate.cbox) && player.callback(crate)) {
// TODO make this more exciting for the player
drop = Drop(randomPointOffCamera(LEVEL_SIZE)); // move the drop to a new spot
} else if (player.hasCrate && collide(player.cbox, drop.cbox) && player.callback(drop)) {
// TODO make this more exciting for the player
WANTED = true;
if (LEVEL < MAX_LEVEL) { LEVEL++; }
crate = Crate(randomPointOffCamera(LEVEL_SIZE)); // move the crate to a new spot
}
// increase the score for surviving when wanted
if (WANTED && arduboy.everyXFrames(60)) {
player.increaseScore(1);
}
// handle dust
for (uint8_t i = 0; i < dust.size(); i++) {
if (dust[i].ttl) {
dust[i].update();
dust[i].draw();
} else {
dust.erase(i);
if (i) i--;
}
}
// TODO move setup elsewhere
while (!cacti.full()) cacti.add(Cactus());
// draw cacti
for (uint8_t i = 0; i < cacti.size(); i++) {
cacti[i].draw();
}
// handle spikes
for (uint8_t i = 0; i < spikes.size(); i++) {
if (spikes[i].ttl) {
// delete the spike if the player collides with it
// TODO make the player react to hitting a spike
if (collide(player.cbox, spikes[i].cbox) && player.callback(spikes[i])) {
spikes.erase(i);
if (i) i--;
continue;
}
spikes[i].update();
spikes[i].draw();
} else {
spikes.erase(i);
if (i) i--;
}
}
// TODO implement levels
while (cops.size() < COP_COUNT[LEVEL]) cops.add(Cop(randomPointOffCamera(LEVEL_SIZE)));
while (swat.size() < SWAT_COUNT[LEVEL]) swat.add(Swat(randomPointOffCamera(LEVEL_SIZE)));
// handle cops
for (uint8_t i = 0; i < cops.size(); i++) {
// only collide with the player if it still exists
if (player.health) {
if (collide(player.cbox, cops[i].cbox)) player.callback(cops[i]);
}
// check for collisions with other cops
for (uint8_t j = 0; j < cops.size(); j++) {
if (j != i) {
if (collide(cops[i].cbox, cops[j].cbox)) {
if (cops[i].callback(cops[j])) {
sound.tone(50, 200);
if (!dust.full())
dust.add(
Dust(cops[i].x + cops[i].width / 2 - 4, cops[i].y + cops[i].height / 2 - 4,
cops[i].angle, cops[i].speed / 2)); // generate a dust cloud
player.increaseScore(3); // award the player for destroying a cop
cops.erase(i);
break; // break instead of continuing because we don't want the nested for loops to get out of sync
} else {
separate(cops[i], cops[j]);
}
}
}
}
cops[i].update();
cops[i].draw();
cops[i].follow(player.x, player.y);
// the magic number here represents the distance at which the indicator starts to grow
uint8_t locatorRadius = 200 / distanceBetween(player.x, player.y, cops[i].x, cops[i].y);
drawLocator(cops[i], locatorRadius);
}
// handle swat
for (uint8_t i = 0; i < swat.size(); i++) {
// only collide with the player if it still exists
if (player.health) {
if (collide(player.cbox, swat[i].cbox)) player.callback(swat[i]);
}
// check for collisions with other swat
for (uint8_t j = 0; j < swat.size(); j++) {
if (j != i) {
if (collide(swat[i].cbox, swat[j].cbox)) {
if (swat[i].callback(swat[j])) {
if (!dust.full())
dust.add(
Dust(swat[i].x + swat[i].width / 2 - 4, swat[i].y + swat[i].height / 2 - 4,
swat[i].angle, swat[i].speed / 2)); // generate a dust cloud
player.increaseScore(5); // award the player for destroying a cop
// swat.erase(i);
break; // break instead of continuing because we don't want the nested for loops to get out of sync
} else {
separate(swat[i], swat[j]);
}
}
}
}
swat[i].update();
swat[i].draw();
swat[i].follow(player.x, player.y);
// the magic number here represents the distance at which the indicator starts to grow
uint8_t locatorRadius = 200 / distanceBetween(player.x, player.y, swat[i].x, swat[i].y);
drawLocator(swat[i], locatorRadius);
}
drawNumber(2, 2, SCORE);
// game over
if (player.health == 0) {
// TODO save score
busted.gameOver(); // show "busted" and return to the menu
}
arduboy.display(CLEAR_BUFFER); // draw everything to the screen
}
} Game;