-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_objects.cpp
More file actions
78 lines (69 loc) · 2.34 KB
/
game_objects.cpp
File metadata and controls
78 lines (69 loc) · 2.34 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
#include <Adafruit_SSD1306.h>
#include "game_objects.h"
#include "game_state.h"
#include "gfx.h"
#include "game.h"
extern Adafruit_SSD1306 display;
extern Game shootduino;
GameObject bullets[MAX_BULLETS];
GameObject asteroids[MAX_ASTEROIDS];
GameObject player;
void init_objects(GameObject* objects, uint8_t max_objects) {
for (uint8_t i = 0; i < max_objects; i++) {
objects[i].is_active = false;
}
}
void draw_objects(GameObject* objects, uint8_t max_objects) {
const uint8_t* bmp = NULL;
for (uint8_t i = 0; i < max_objects; i++) {
if (objects[i].is_active) {
switch (objects[i].type) {
case ASTEROID:
bmp = asteroid_anim + (2 * ASTEROID_H) * objects[i].anim_frame;
display.drawBitmap(asteroids[i].x, asteroids[i].y, bmp, ASTEROID_W, ASTEROID_H, WHITE);
if (shootduino.state == RUNNING)
objects[i].frame_count++;
if (objects[i].frame_count == ANIM_FRAME_DELAY) {
objects[i].anim_frame++;
objects[i].anim_frame %= NUM_ASTEROID_FRAMES;
objects[i].frame_count = 0;
}
break;
case EXPLOSION:
bmp = explosion_anim + (2 * EXPLOSION_H) * objects[i].anim_frame;
display.drawBitmap(asteroids[i].x, asteroids[i].y, bmp, EXPLOSION_W, EXPLOSION_H, WHITE);
objects[i].frame_count++;
if (objects[i].frame_count == ANIM_FRAME_DELAY) {
objects[i].frame_count = 0;
objects[i].anim_frame++;
if (objects[i].anim_frame == NUM_EXPLOSION_FRAMES)
objects[i].is_active = false;
}
break;
case BULLET:
display.drawFastHLine(objects[i].x, objects[i].y, BULLET_W, WHITE);
break;
}
}
}
}
void move_objects(GameObject* objects, uint8_t max_objects) {
for (uint8_t i = 0; i < max_objects; i++) {
if (objects[i].is_active) {
objects[i].x += objects[i].vx;
switch (objects[i].type) {
case ASTEROID:
if (objects[i].x + (int)ASTEROID_W <= 0) {
objects[i].is_active = false;
shootduino.asteroids_missed++;
}
break;
case BULLET:
if (objects[i].x >= display.width()) {
objects[i].is_active = false;
}
break;
}
}
}
}