Handle collisions

This commit is contained in:
Camden Dixie O'Brien
2025-10-14 16:51:52 +01:00
parent d3b1b3f926
commit 7d729e42c4

40
game.c
View File

@@ -56,6 +56,7 @@ typedef struct {
float collision_radius; float collision_radius;
collision_tag_t tag; collision_tag_t tag;
bool wrap; bool wrap;
bool dead;
} entity_t; } entity_t;
typedef struct { typedef struct {
@@ -89,7 +90,8 @@ static shape_t shapes[MAX_SHAPES];
static unsigned entity_count; static unsigned entity_count;
static unsigned shape_count; static unsigned shape_count;
static bool quit;
static bool dead;
static float aspect; static float aspect;
static unsigned ship_entity_id; static unsigned ship_entity_id;
@@ -259,12 +261,43 @@ static unsigned check_collisions(collision_t *out)
return count; 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) void game_init(float _aspect)
{ {
input_on_shoot(shoot); input_on_shoot(shoot);
aspect = _aspect; aspect = _aspect;
quit = false; dead = false;
entity_count = shape_count = 0; entity_count = shape_count = 0;
entity_t *ship = add_entity(&ship_entity_id); entity_t *ship = add_entity(&ship_entity_id);
@@ -290,6 +323,8 @@ void game_init(float _aspect)
void game_update() void game_update()
{ {
if (dead)
return;
ship_update(); ship_update();
@@ -321,6 +356,7 @@ void game_update()
static collision_t collisions[MAX_COLLISIONS]; static collision_t collisions[MAX_COLLISIONS];
const unsigned ncols = check_collisions(collisions); const unsigned ncols = check_collisions(collisions);
handle_collisions(collisions, ncols);
} }
void game_draw() void game_draw()