Handle collisions
This commit is contained in:
40
game.c
40
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()
|
||||
|
||||
Reference in New Issue
Block a user