Add collision info to entities

This commit is contained in:
Camden Dixie O'Brien
2025-10-14 16:13:46 +01:00
parent 57f5b345f8
commit 85984a0a07

20
game.c
View File

@@ -9,6 +9,8 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#define SHIP_COLLIDE_R 0.05
#define FIRE_MEAN -0.15 #define FIRE_MEAN -0.15
#define FIRE_JITTER 0.01 #define FIRE_JITTER 0.01
@@ -29,6 +31,7 @@
#define ROT_PWR 0.002 #define ROT_PWR 0.002
#define SHOT_VEL 0.04 #define SHOT_VEL 0.04
#define SHOT_COLLIDE_R 0.005
#define MAX_SHAPES 256U #define MAX_SHAPES 256U
#define MAX_ENTITIES 128U #define MAX_ENTITIES 128U
@@ -36,6 +39,12 @@
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
typedef enum {
COLLISION_SHIP,
COLLISION_SHOT,
COLLISION_ASTEROID,
} collision_tag_t;
typedef struct { typedef struct {
vec2_t pos; vec2_t pos;
vec2_t vel; vec2_t vel;
@@ -43,6 +52,8 @@ typedef struct {
float omg; float omg;
unsigned shapes[MAX_SHAPES_PER_ENTITY]; unsigned shapes[MAX_SHAPES_PER_ENTITY];
unsigned shape_count; unsigned shape_count;
float collision_radius;
collision_tag_t tag;
bool wrap; bool wrap;
} entity_t; } entity_t;
@@ -118,6 +129,8 @@ static void shoot()
ship->pos, mat2_mul_vec2(ship->dir, ship_shape->verts[0])); ship->pos, mat2_mul_vec2(ship->dir, ship_shape->verts[0]));
e->vel = vec2_add( e->vel = vec2_add(
ship->vel, mat2_mul_vec2(ship->dir, (vec2_t) { 0, SHOT_VEL })); ship->vel, mat2_mul_vec2(ship->dir, (vec2_t) { 0, SHOT_VEL }));
e->collision_radius = SHIP_COLLIDE_R;
e->tag = COLLISION_SHOT;
shape_t *s = add_shape(id, nullptr); shape_t *s = add_shape(id, nullptr);
s->visible = true; s->visible = true;
@@ -130,6 +143,8 @@ static entity_t *gen_asteroid(float r_mean)
unsigned id; unsigned id;
entity_t *e = add_entity(&id); entity_t *e = add_entity(&id);
e->wrap = true; e->wrap = true;
e->collision_radius = r_mean;
e->tag = COLLISION_ASTEROID;
shape_t *s = add_shape(id, nullptr); shape_t *s = add_shape(id, nullptr);
s->visible = true; s->visible = true;
@@ -177,7 +192,7 @@ static void remove_shape(unsigned id)
{ {
if (id < shape_count - 1) { if (id < shape_count - 1) {
const shape_t *last = shapes + shape_count - 1; const shape_t *last = shapes + shape_count - 1;
memcpy(shapes + id, shapes + shape_count - 1, sizeof(shape_t)); memcpy(shapes + id, last, sizeof(shape_t));
for (unsigned i = 0; i < entities[last->entity].shape_count; ++i) { for (unsigned i = 0; i < entities[last->entity].shape_count; ++i) {
if (entities[last->entity].shapes[i] == shape_count - 1) { if (entities[last->entity].shapes[i] == shape_count - 1) {
entities[last->entity].shapes[i] = id; entities[last->entity].shapes[i] = id;
@@ -227,9 +242,12 @@ void game_init(float _aspect)
aspect = _aspect; aspect = _aspect;
quit = false; quit = false;
entity_count = shape_count = 0;
entity_t *ship = add_entity(&ship_entity_id); entity_t *ship = add_entity(&ship_entity_id);
ship->wrap = true; ship->wrap = true;
ship->collision_radius = SHIP_COLLIDE_R;
ship->tag = COLLISION_SHIP;
shape_t *ship_shape = add_shape(ship_entity_id, &ship_shape_id); shape_t *ship_shape = add_shape(ship_entity_id, &ship_shape_id);
ship_shape->visible = true; ship_shape->visible = true;