From 64b7fc60fcd37239470eaf7fd5a46d1d8431bf96 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 18 Oct 2025 17:43:22 +0100 Subject: [PATCH] Handle collisions --- game.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/game.c b/game.c index dbbb2cb..a021173 100644 --- a/game.c +++ b/game.c @@ -56,6 +56,7 @@ typedef struct { float collision_radius; collision_tag_t tag; bool wrap; + bool dead; } entity_t; typedef struct { @@ -89,7 +90,8 @@ static shape_t shapes[MAX_SHAPES]; static unsigned entity_count; static unsigned shape_count; -static bool quit; + +static bool dead; static float aspect; static unsigned ship_entity_id; @@ -259,12 +261,43 @@ static unsigned check_collisions(collision_t *out) return count; } +static void handle_collisions(const collision_t *collisions, unsigned count) +{ + for (unsigned i = 0; i < count; ++i) { + const collision_t c = collisions[i]; + entity_t *a = entities + c.entities[0]; + entity_t *b = entities + c.entities[1]; + + if (a->tag == COLLISION_SHIP || b->tag == COLLISION_SHIP) { + dead = true; + continue; + } + + if (a->tag == COLLISION_SHOT && b->tag == COLLISION_SHOT) + continue; + + if (a->tag == COLLISION_SHOT) { + a->dead = true; + b->dead = true; + } + if (b->tag == COLLISION_SHOT) { + a->dead = true; + b->dead = true; + } + } + + for (unsigned i = 0; i < entity_count; ++i) { + if (entities[i].dead) + remove_entity(i--); + } +} + void game_init(float _aspect) { input_on_shoot(shoot); aspect = _aspect; - quit = false; + dead = false; entity_count = shape_count = 0; entity_t *ship = add_entity(&ship_entity_id); @@ -290,6 +323,8 @@ void game_init(float _aspect) void game_update() { + if (dead) + return; ship_update(); @@ -321,6 +356,7 @@ void game_update() static collision_t collisions[MAX_COLLISIONS]; const unsigned ncols = check_collisions(collisions); + handle_collisions(collisions, ncols); } void game_draw()