From 128d4058a4e0d37b7bef394ccde7277391e4ae0e Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 9 Aug 2015 18:28:26 +0200 Subject: [PATCH] Fix throwing grenades from moving platforms. The train and obstacle collision code made a distinction between horizontally moving entities (which covers bullets and lasers), and vertically moving entities (falling items?), but didn't handle diagonally moving entities correctly, like grenades. --- src/bullets.cpp | 2 +- src/defs.h | 6 ++++++ src/entities.cpp | 4 ++-- src/obstacles.cpp | 12 ++++++++---- src/trains.cpp | 14 +++++++++----- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/bullets.cpp b/src/bullets.cpp index 88e288c..fb84642 100644 --- a/src/bullets.cpp +++ b/src/bullets.cpp @@ -264,7 +264,7 @@ bool bulletHasCollided(Entity *bullet, float dx, float dy) checkSwitchContact(bullet); - if ((checkTrainContact(bullet, 0)) || (checkObstacleContact(bullet, 0))) + if ((checkTrainContact(bullet, DIR_XY)) || (checkObstacleContact(bullet, DIR_XY))) { if (bullet->flags & ENT_BOUNCES) bounceBullet(bullet, dx, dy); diff --git a/src/defs.h b/src/defs.h index d358d3e..5c59832 100644 --- a/src/defs.h +++ b/src/defs.h @@ -378,6 +378,12 @@ enum { PAK_TAGS }; +/* ######### miscellaneous ############## */ + +#define DIR_X 1 +#define DIR_Y 2 +#define DIR_XY 3 + /* ############# debug ################## */ #if DEBUG diff --git a/src/entities.cpp b/src/entities.cpp index 25ae954..4a48160 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -321,7 +321,7 @@ void moveEntity(Entity *ent) if (ent->dx != 0) { - if ((checkBrickContactX(ent)) || (checkObstacleContact(ent, 0)) || (checkTrainContact(ent, 0))) + if ((checkBrickContactX(ent)) || (checkObstacleContact(ent, DIR_X)) || (checkTrainContact(ent, DIR_X))) { ent->dx = 0; } @@ -336,7 +336,7 @@ void moveEntity(Entity *ent) if (ent->dy != 0) { - if ((checkBrickContactY(ent)) || (checkObstacleContact(ent, 1)) || (checkTrainContact(ent, 1))) + if ((checkBrickContactY(ent)) || (checkObstacleContact(ent, DIR_Y)) || (checkTrainContact(ent, DIR_Y))) { if ((ent->flags & ENT_BOUNCES) && (ent->dy >= 3)) { diff --git a/src/obstacles.cpp b/src/obstacles.cpp index dc1ddfe..a919e52 100644 --- a/src/obstacles.cpp +++ b/src/obstacles.cpp @@ -57,18 +57,22 @@ bool checkObstacleContact(Entity *ent, int dir) continue; } - if (dir == 0) + if (dir == DIR_X) { collision = Collision::collision(ent->x + ent->dx, ent->y, ent->width, ent->height - 1, obstacle->x, obstacle->y, obstacle->width, obstacle->height); } - else + else if (dir == DIR_Y) { collision = Collision::collision(ent->x, ent->y + ent->dy, ent->width, ent->height - 1, obstacle->x, obstacle->y, obstacle->width, obstacle->height); } + else + { + collision = Collision::collision(ent->x + ent->dx, ent->y + ent->dy, ent->width, ent->height - 1, obstacle->x, obstacle->y, obstacle->width, obstacle->height); + } if (collision) { - if (dir == 0) + if (dir & DIR_X) { if ((ent->y + ent->height == obstacle->y + obstacle->height) || ((ent->flags & ENT_BULLET) && (ent->owner == &player))) { @@ -79,7 +83,7 @@ bool checkObstacleContact(Entity *ent, int dir) } } - if (dir == 1) + if (dir & DIR_Y) { ent->falling = false; ent->dy = 0; diff --git a/src/trains.cpp b/src/trains.cpp index 2c7585f..9853123 100644 --- a/src/trains.cpp +++ b/src/trains.cpp @@ -87,14 +87,14 @@ void trainBlockEntity(Entity *ent, const char *message, Train *train, int dir) if ((ent->flags & ENT_BULLET) && (!(ent->flags & ENT_BOUNCES))) { - if (dir == 0) + if (dir & DIR_X) { if (ent->dx < 0) ent->x = train->x + train->sprite->image[0]->w; if (ent->dx > 0) ent->x = train->x - ent->width; } } - if (dir == 1) + if (dir & DIR_Y) { if ((ent->dy >= 0) && (train->type >= TR_SLIDEDOOR)) { @@ -122,14 +122,18 @@ bool checkTrainContact(Entity *ent, int dir) { train = (Train*)train->next; - if (dir == 0) + if (dir == DIR_X) { collision = (Collision::collision(ent->x + ent->dx, ent->y, ent->width, ent->height - 1, train->x, train->y, train->width, train->height)); } - else + else if (dir == DIR_Y) { collision = (Collision::collision(ent->x, ent->y + ent->dy, ent->width, ent->height - 1, train->x, train->y, train->width, train->height)); } + else + { + collision = (Collision::collision(ent->x + ent->dx, ent->y + ent->dy, ent->width, ent->height - 1, train->x, train->y, train->width, train->height)); + } if (collision) { @@ -185,7 +189,7 @@ bool checkTrainContact(Entity *ent, int dir) openDoor(train); } - if (dir == 1) + if (dir & DIR_Y) { ent->dy = 0; ent->falling = false; -- 2.39.5