From f10ec392efa28667a5443c6277f565990cc38696 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 2 Nov 2015 12:04:11 +0000 Subject: [PATCH] Bullets get collision candidates from grid. --- src/battle/bullets.c | 24 +++++++++++++++++------- src/battle/bullets.h | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/battle/bullets.c b/src/battle/bullets.c index 7f68649..16689b4 100644 --- a/src/battle/bullets.c +++ b/src/battle/bullets.c @@ -93,15 +93,21 @@ void doBullets(void) static void checkCollisions(Bullet *b) { - Entity *f; + Entity *e, **candidates; + int i; - for (f = battle.entityHead.next ; f != NULL ; f = f->next) + candidates = getAllEntsWithin(b->x, b->y, b->w, b->h, NULL); + i = 0; + + e = candidates[i]; + + while (e) { - if (f->active && f->type == ET_FIGHTER) + if (e->type == ET_FIGHTER) { - if (b->owner != f && f->health > 0 && collision(b->x - b->w / 2, b->y - b->h / 2, b->w, b->h, f->x - f->w / 2, f->y - f->h / 2, f->w, f->h)) + if (b->owner != e && e->health > 0 && collision(b->x - b->w / 2, b->y - b->h / 2, b->w, b->h, e->x - e->w / 2, e->y - e->h / 2, e->w, e->h)) { - if (b->owner->side == f->side) + if (b->owner->side == e->side) { b->damage = 0; } @@ -110,7 +116,7 @@ static void checkCollisions(Bullet *b) battle.stats[STAT_SHOTS_HIT]++; } - damageFighter(f, b->damage, b->flags); + damageFighter(e, b->damage, b->flags); b->life = 0; @@ -120,7 +126,7 @@ static void checkCollisions(Bullet *b) } /* assuming that health <= 0 will always mean killed */ - if (f->health <= 0 && b->owner == player) + if (e->health <= 0 && b->owner == player) { battle.stats[STAT_ENEMIES_KILLED_PLAYER]++; } @@ -133,6 +139,10 @@ static void checkCollisions(Bullet *b) return; } } + + i++; + + e = (i < MAX_GRID_CANDIDATES) ? candidates[i] : NULL; } } diff --git a/src/battle/bullets.h b/src/battle/bullets.h index 4d96e43..ef69d3e 100644 --- a/src/battle/bullets.h +++ b/src/battle/bullets.h @@ -39,6 +39,7 @@ extern float getAngle(int x1, int y1, int x2, int y2); extern void addMissileEngineEffect(Bullet *b); extern int mod(int n, int x); extern void addMissileExplosion(Bullet *b); +extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Battle battle; extern Entity *player;