Add pausing

This commit is contained in:
Camden Dixie O'Brien
2025-10-16 14:22:10 +01:00
parent 07553feef1
commit 03f1bedc61
4 changed files with 76 additions and 10 deletions

43
game.c
View File

@@ -123,6 +123,9 @@ static float aspect;
static unsigned level;
static bool dead;
static bool clear;
static bool paused;
static unsigned asteroid_count;
static uint8_t counter;
static unsigned score;
@@ -233,10 +236,10 @@ static void spawn_asteroid()
const float rnd = rng_canon();
if (rnd < 0.4)
r = ASTEROID_MEDIUM;
else if (rnd < 0.8)
r = ASTEROID_LARGE;
else
else if (level > 3 && rnd < 0.45)
r = ASTEROID_HUGE;
else
r = ASTEROID_LARGE;
unsigned id;
entity_t *e = gen_asteroid(r, &id);
@@ -349,7 +352,9 @@ static void bounce(collision_t c)
static void cleared()
{
clear = true;
renderer_set_wrap(false);
counter = 0;
}
static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
@@ -401,6 +406,13 @@ static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
}
}
static void die()
{
dead = true;
clear = false;
counter = 0;
}
static void handle_collisions(const collision_t *collisions, unsigned count)
{
for (unsigned i = 0; i < count; ++i) {
@@ -409,7 +421,7 @@ static void handle_collisions(const collision_t *collisions, unsigned count)
entity_t *b = entities + c.entities[1];
if (a->tag == COLLISION_SHIP || b->tag == COLLISION_SHIP) {
dead = true;
die();
continue;
}
@@ -459,7 +471,8 @@ static void draw_arrow()
static void create_field()
{
dead = false;
counter = 0;
clear = false;
paused = false;
entity_count = shape_count = 0;
asteroid_count = 0;
@@ -497,10 +510,17 @@ static void reset()
create_field();
}
static void pause()
{
paused = !paused;
counter = 0;
}
void game_init(float _aspect)
{
input_on_shoot(shoot);
input_on_restart(reset);
input_on_pause(pause);
aspect = _aspect;
reset();
@@ -508,9 +528,9 @@ void game_init(float _aspect)
void game_update()
{
if (dead || asteroid_count == 0)
if (dead || clear || paused)
++counter;
if (dead)
if (dead || paused)
return;
ship_update();
@@ -521,7 +541,7 @@ void game_update()
e->dir = mat2_mul_mat2(mat2_rotation(e->omg), e->dir);
e->pos = vec2_add(e->pos, e->vel);
if (asteroid_count == 0 && i == ship_entity_id) {
if (clear && i == ship_entity_id) {
if (e->pos.x > aspect)
win();
}
@@ -560,10 +580,15 @@ void game_draw()
text_draw_score(aspect, score);
if (paused && !(counter & COUNTER_MASK)) {
text_draw_centre("PAUSED");
return;
}
if (dead && !(counter & COUNTER_MASK))
text_draw_centre("GAME OVER");
if (!dead && asteroid_count == 0) {
if (clear) {
draw_arrow();
if (!(counter & COUNTER_MASK))
text_draw_centre("CLEAR");