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