diff options
Diffstat (limited to 'sdl3_main.c')
| -rw-r--r-- | sdl3_main.c | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/sdl3_main.c b/sdl3_main.c index 28d7c89..507a64c 100644 --- a/sdl3_main.c +++ b/sdl3_main.c @@ -43,6 +43,8 @@ typedef struct { Entity *target; } EntityCollision; +typedef void (EntityCollisionProc)(Entity *self, Entity *other); + static void render(void); static void update(float delta); static void process_events(void); @@ -52,8 +54,14 @@ static void freeentity(Entity *); static void entphysics(float delta); -static void enqcoll(EntityCollision *c); -static void deqcoll(EntityCollision *c); +static void enqcoll(EntityCollision *c); +static EntityCollision *deqcoll(void); + +static EntityCollisionProc bulletcollproc; + +static EntityCollisionProc *collisionProcs[] = { + [EBULLET] = bulletcollproc, +}; static SDL_Window *window; static SDL_Renderer *renderer; @@ -211,9 +219,21 @@ update(float delta) # define SPEED 100 const _Bool *kstate = SDL_GetKeyboardState(NULL); Entity *bullet, *other; + EntityCollision *coll; + float vx, vy; entphysics(delta); + while((coll = deqcoll())) { + Entity *self = coll->self; + Entity *target = coll->target; + + if(collisionProcs[self->type]) + collisionProcs[self->type](self, target); + + if(collisionProcs[target->type]) + collisionProcs[target->type](target, self); + } for(int ei = 0; ei < ebufi; ei++) { Entity *e = entitybuffer + ei; @@ -274,7 +294,7 @@ update(float delta) for(int j = 0; j < 1024; j++) { other = entitybuffer + j; - if(!other->active || other == e) + if(!other->active || other == e || other->type != EGHOST) continue; float dx = other->body.pos[0] - e->body.pos[0]; @@ -353,7 +373,7 @@ entphysics(float delta) for(int i = 0; i < ebufi; i++) { Entity *e = entitybuffer + i; if(!e->active) - break; + continue; for(int y = 0; y < map_height; y++) for(int x = 0; x < map_width; x++) { @@ -402,7 +422,7 @@ entphysics(float delta) for(int i = 0; i < ebufi; i++) { Entity *e = entitybuffer + i; if(!e->active) - break; + continue; for(int j = 0; j < 2; j++) e->body.pos[j] += e->body.vel[j] * delta; @@ -422,15 +442,28 @@ enqcoll(EntityCollision *c) ecollend = 0; } -void -deqcoll(EntityCollision *c) +EntityCollision * +deqcoll(void) { + EntityCollision *c; if(ecollsi <= 0) - return; + return NULL; ecollsi--; - ecoll[ecollstart++] = *c; + c = ecoll + ecollstart++; + if(ecollstart >= 2048) ecollstart = 0; + + return c; +} + +void +bulletcollproc(Entity *self, Entity *target) +{ + if(target->type == EGHOST) { + self->active = false; + target->active = false; + } } |
