-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.c
More file actions
242 lines (212 loc) · 5.37 KB
/
game.c
File metadata and controls
242 lines (212 loc) · 5.37 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
#include <stdbool.h>
#include <stdlib.h>
#include <rand.h>
#include <stdio.h>
#include <libetc.h>
#include "game.h"
#include "fixed.h"
#include "math.h"
#include "vec2.h"
#include "mem.h"
#include "gfx.h"
#include "audio.h"
#include "input.h"
#include "ship.h"
#include "bullet_ship.h"
#include "asteroid.h"
static bool game_started = false;
static uint32_t game_title_count = 0;
static uint32_t game_cleared_count = 0;
static uint32_t ship_destroyed_count = 0;
static uint32_t go_count = 0;
static uint32_t game_over_count = 0;
static uint32_t score = 0;
static uint8_t lives = 3;
static Entity center_space_check_entity;
static void reset_ship()
{
Entity *ship = ship_create();
entity_add_after(ship_tag_start, ship);
ship_destroyed_count = 0;
}
static void reset_level()
{
const ASTEROID_SIZE asteroid_sizes[] = { SIZE_SMALL, SIZE_SMALL, SIZE_LARGE };
const uint8_t asteroid_nums[] = { 5, 2, 2 };
for (size_t s = 0; s < 3; s++)
{
for (uint32_t i = 0; i < asteroid_nums[s]; i++)
{
// ensure the spawned asteroid does not hit ship at center of screen
int32_t px = center_space_check_entity.radius_collision;
px += asteroid_sizes[s];
px += rand() % 64;
px = (px << 12);
Vec2 position = vec2_init(px, 0);
fixed angle = rand() % 4096;
position = vec2_rotate(angle, &position);
position.x += center_space_check_entity.position.x;
position.y += center_space_check_entity.position.y;
angle = rand() % 4096;
asteroid_spawn(asteroid_sizes[s], &position, angle);
}
}
}
static void start_title()
{
audio_play_music(MUSIC_ID_TITLE);
entity_remove_all();
reset_level();
game_title_count = 0;
game_started = false;
lives = 3;
score = 0;
}
static void start_new_game()
{
srand(VSync(-1));
audio_play_music(MUSIC_ID_PLAYING);
audio_pause_music();
audio_play_sound(SOUND_ID_START);
VSync(60);
audio_resume_music();
entity_remove_all();
reset_ship();
reset_level();
game_started = true;
game_cleared_count = 0;
game_over_count = 0;
lives = 3;
score = 0;
}
static void next_level()
{
entity_remove_all();
reset_ship();
reset_level();
game_started = true;
game_cleared_count = 0;
game_over_count = 0;
}
static void handle_title()
{
game_title_count++;
gfx_print("\n\n\n\n\n\n\n\n ASTEROIDS");
if (game_title_count % 30 < 20)
{
gfx_print("\n\n\n\n\n\n\n - PUSH START BUTTON -");
}
else {
gfx_print("\n\n\n\n\n\n\n");
}
gfx_print("\n\n\n\n\n\n\n\n (C)2023 danx2016");
if (!game_started && input_is_action_just_pressed(0, START))
{
start_new_game();
}
}
// check if there is a large empty space enough at center of screen where the ship can respawn
static bool can_ship_respawn()
{
Entity *next_asteroid = asteroid_tag_start->next_entity;
while (next_asteroid != NULL && next_asteroid != asteroid_tag_end)
{
if (entity_check_collision(¢er_space_check_entity, next_asteroid))
{
return false;
}
next_asteroid = next_asteroid->next_entity;
}
return true;
}
static void handle_playing()
{
Entity *ship = entity_find_first_type(SHIP);
// check level cleared (all asteroids was destroyed?)
if (ship != NULL && asteroid_tag_start->next_entity == asteroid_tag_end)
{
gfx_print("\n\n\n\n\n\n\n\n\n\n LEVEL CLEARED!");
game_cleared_count++;
}
if (game_cleared_count >= 360)
{
next_level();
}
if (ship == NULL)
{
ship_destroyed_count++;
}
else if (ship->is_invincible)
{
gfx_print("\n\n\n\n\n\n\n\n\n READY?");
go_count = 30;
}
else if (!ship->is_invincible && go_count > 0)
{
gfx_print("\n\n\n\n\n\n\n\n\n GO!");
go_count--;
}
if (ship_destroyed_count >= 180)
{
if (lives > 0)
{
if (!can_ship_respawn())
{
return;
}
reset_ship();
lives--;
}
else {
audio_stop_music();
gfx_print("\n\n\n\n\n\n\n\n\n\n GAME OVER");
game_over_count++;
if (game_over_count >= 540)
{
start_title();
}
}
}
}
void game_add_score(uint32_t points)
{
score += points;
}
static void game_init_all()
{
mem_init();
gfx_init();
audio_init();
math_init();
input_init();
entity_init();
ship_init();
asteroid_init();
}
void game_start()
{
game_init_all();
center_space_check_entity.radius_collision = 40;
center_space_check_entity.position = vec2_init(160 << 12, 128 << 12);
start_title();
uint8_t hud_info[128];
while (true)
{
input_update();
entity_update_all();
entity_render_all();
// show lives and score
sprintf(hud_info, "\n\n\n LIVES: %d SCORE: %06d\n", lives, score);
gfx_print(hud_info);
// handle title & playing states
if (!game_started)
{
handle_title();
}
else
{
handle_playing();
}
gfx_swap_buffers();
}
}