From 9ce6624fc0e2812db608bf08b03afc3db993388e Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 15 Nov 2015 11:31:30 +0000 Subject: [PATCH] Moved all rope handling code to rope.c. --- src/battle/entities.c | 2 ++ src/battle/entities.h | 1 + src/battle/extractionPoint.c | 20 ++------------------ src/battle/extractionPoint.h | 1 - src/battle/fighters.c | 25 ------------------------- src/battle/fighters.h | 2 +- src/battle/rope.c | 35 +++++++++++++++++++++++++++++++++++ src/battle/rope.h | 4 ++++ src/structs.h | 1 + 9 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/battle/entities.c b/src/battle/entities.c index a15fdf4..a0676fd 100644 --- a/src/battle/entities.c +++ b/src/battle/entities.c @@ -118,6 +118,8 @@ void doEntities(void) battle.playerSelect = battle.epic; } + cutRope(e); + prev->next = e->next; free(e); e = prev; diff --git a/src/battle/entities.h b/src/battle/entities.h index 8e11a75..ca7582e 100644 --- a/src/battle/entities.h +++ b/src/battle/entities.h @@ -31,6 +31,7 @@ extern void removeFromGrid(Entity *e); extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern void doRope(Entity *e); extern void drawRope(Entity *e); +extern void cutRope(Entity *e); extern App app; extern Battle battle; diff --git a/src/battle/extractionPoint.c b/src/battle/extractionPoint.c index 6421af5..ebbc4b1 100644 --- a/src/battle/extractionPoint.c +++ b/src/battle/extractionPoint.c @@ -22,7 +22,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void think(void); static void handleFleeingEntities(void); -static void cutTowRope(Entity *e); Entity *spawnExtractionPoint(void) { @@ -57,28 +56,13 @@ static void handleFleeingEntities(void) Entity *e, **candidates; int i; - candidates = getAllEntsWithin(self->x, self->y, self->w, self->h, self); + candidates = getAllEntsWithin(self->x - self->w / 2, self->y - self->h / 2, self->w, self->h, self); for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) { - if (e->health > 0 && e->flags & EF_FLEEING && getDistance(e->x, e->y, self->x, self->y) <= 64) + if (e->health > 0 && e->flags & EF_FLEEING && getDistance(self->x, self->y, e->x, e->y) <= 64) { e->alive = ALIVE_ESCAPED; - - cutTowRope(e); - } - } -} - -static void cutTowRope(Entity *attachment) -{ - Entity *e; - - for (e = battle.entityHead.next ; e != NULL ; e = e->next) - { - if (e->towing == attachment) - { - e->towing = NULL; } } } diff --git a/src/battle/extractionPoint.h b/src/battle/extractionPoint.h index b14545b..b24b9dd 100644 --- a/src/battle/extractionPoint.h +++ b/src/battle/extractionPoint.h @@ -27,7 +27,6 @@ extern SDL_Texture *getTexture(char *filename); extern Entity *spawnEntity(void); extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern int getDistance(int x1, int y1, int x2, int y2); -extern void destroyRope(Entity *owner); extern Battle battle; extern Entity *self; diff --git a/src/battle/fighters.c b/src/battle/fighters.c index 3e4a6a9..72407cd 100644 --- a/src/battle/fighters.c +++ b/src/battle/fighters.c @@ -27,7 +27,6 @@ static void spinDie(void); static void straightDie(void); static void randomizeDart(Entity *dart); static void randomizeDartGuns(Entity *dart); -static void attachRope(void); Entity *spawnFighter(char *name, int x, int y, int side) { @@ -335,30 +334,6 @@ static void separate(void) } } -static void attachRope(void) -{ - int i, distance; - Entity *e, **candidates; - - if ((self->flags & EF_HAS_ROPE) && self->towing == NULL) - { - candidates = getAllEntsWithin(self->x, self->y, self->w, self->h, self); - - for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) - { - if ((e->flags & EF_DISABLED) && e->alive == ALIVE_ALIVE) - { - distance = getDistance(e->x, e->y, self->x, self->y); - - if (distance > 0 && distance <= 32) - { - self->towing = e; - } - } - } - } -} - void drawFighter(Entity *e) { SDL_Texture *shieldHitTexture = getTexture("gfx/battle/shieldHit.png"); diff --git a/src/battle/fighters.h b/src/battle/fighters.h index a9c43ee..0c629c9 100644 --- a/src/battle/fighters.h +++ b/src/battle/fighters.h @@ -42,7 +42,7 @@ extern void checkTrigger(char *name, int type); extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity *spawnEntity(void); extern void adjustObjectiveTargetValue(char *name, int type, int amount); -extern void addRope(Entity *src, Entity *dest); +extern void attachRope(void); extern App app; extern Battle battle; diff --git a/src/battle/rope.c b/src/battle/rope.c index 4e006dd..d14bde8 100644 --- a/src/battle/rope.c +++ b/src/battle/rope.c @@ -20,6 +20,32 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "rope.h" +void attachRope(void) +{ + int i, distance; + Entity *e, **candidates; + + if ((self->flags & EF_HAS_ROPE) && self->towing == NULL) + { + candidates = getAllEntsWithin(self->x, self->y, self->w, self->h, self); + + for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) + { + if ((e->flags & EF_DISABLED) && e->alive == ALIVE_ALIVE) + { + distance = getDistance(e->x, e->y, self->x, self->y); + + if (distance > 0 && distance <= 64) + { + self->towing = e; + e->owner = self; + addHudMessage(colors.white, "Tow rope attached"); + } + } + } + } +} + void doRope(Entity *owner) { float dx, dy, angle, force; @@ -55,3 +81,12 @@ void drawRope(Entity *owner) SDL_RenderDrawLine(app.renderer, owner->x - battle.camera.x, owner->y - battle.camera.y, owner->towing->x - battle.camera.x, owner->towing->y - battle.camera.y); } } + +void cutRope(Entity *e) +{ + if (e->owner && e->owner->towing == e) + { + e->owner->towing = NULL; + e->owner = NULL; + } +} diff --git a/src/battle/rope.h b/src/battle/rope.h index b5f608f..be3c4fc 100644 --- a/src/battle/rope.h +++ b/src/battle/rope.h @@ -27,6 +27,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern float getAngle(int x1, int y1, int x2, int y2); extern int getDistance(int x1, int y1, int x2, int y2); +extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); +extern void addHudMessage(SDL_Color c, char *format, ...); extern App app; extern Battle battle; +extern Colors colors; +extern Entity *self; diff --git a/src/structs.h b/src/structs.h index 8731b96..a7db757 100644 --- a/src/structs.h +++ b/src/structs.h @@ -111,6 +111,7 @@ struct Entity { SDL_Point targetLocation; Entity *towing; Entity *target; + Entity *owner; void (*action)(void); void (*defaultAction)(void); void (*die)(void);