Collision bug fixes.

This commit is contained in:
Steve 2018-02-03 11:32:03 +00:00
parent 0a131e25c0
commit c77cea92a6
18 changed files with 47 additions and 130 deletions

View File

@ -61,7 +61,7 @@ static int isBlockedByMap(Entity *src, Entity *dest)
mapBlock.w = MAP_TILE_SIZE; mapBlock.w = MAP_TILE_SIZE;
mapBlock.h = MAP_TILE_SIZE; mapBlock.h = MAP_TILE_SIZE;
if (lineIntersectsRect(mapBlock, src->x, src->y, dest->x, dest->y)) if (lineRectIntersection(src->x, src->y, dest->x, dest->y, &mapBlock))
{ {
return 1; return 1;
} }
@ -75,7 +75,7 @@ static int isBlockedByMap(Entity *src, Entity *dest)
static int isBlockedByEntities(Entity *src, Entity *dest) static int isBlockedByEntities(Entity *src, Entity *dest)
{ {
Entity **candidates, *e; Entity **candidates, *e;
SDL_Rect losBounds; SDL_Rect losBounds, eBounds;
int i; int i;
losBounds.x = (int) MIN(src->x, dest->x); losBounds.x = (int) MIN(src->x, dest->x);
@ -92,7 +92,12 @@ static int isBlockedByEntities(Entity *src, Entity *dest)
continue; continue;
} }
if (lineIntersectsRect(e->bounds, src->x, src->y, dest->x, dest->y)) eBounds.x = e->x;
eBounds.y = e->y;
eBounds.w = e->w;
eBounds.h = e->h;
if (lineRectIntersection(src->x, src->y, dest->x, dest->y, &eBounds))
{ {
return 1; return 1;
} }

View File

@ -23,5 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern int isSolid(int x, int y); extern int isSolid(int x, int y);
extern int lineIntersectsRect(SDL_Rect r, int x1, int y1, int x2, int y2); extern int lineIntersectsRect(SDL_Rect r, int x1, int y1, int x2, int y2);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore);
extern int lineRectIntersection(int x1, int y1, int x2, int y2, SDL_Rect *r);
extern World world; extern World world;

View File

@ -29,7 +29,6 @@ static void die1(void);
static void die2(void); static void die2(void);
static void attack(void); static void attack(void);
static void applyDamage(int amount); static void applyDamage(int amount);
static SDL_Rect *getBounds(void);
static void preFire(void); static void preFire(void);
static void selectWeapon(void); static void selectWeapon(void);
static void moveTowardsPlayer(void); static void moveTowardsPlayer(void);
@ -62,7 +61,6 @@ void initTankCommander(Entity *e)
b->tick = tick; b->tick = tick;
b->die = die1; b->die = die1;
b->applyDamage = applyDamage; b->applyDamage = applyDamage;
b->getBounds = getBounds;
brakingTimer = 0; brakingTimer = 0;
@ -348,23 +346,3 @@ static void applyDamage(int amount)
addExplosion(self->x + self->w / 2, self->y + self->h / 2, 50, self); addExplosion(self->x + self->w / 2, self->y + self->h / 2, 50, self);
} }
} }
static SDL_Rect *getBounds(void)
{
if (self->facing == FACING_LEFT)
{
self->bounds.x = self->x + 98;
self->bounds.y = self->y;
self->bounds.w = 140;
self->bounds.h = self->h;
}
else
{
self->bounds.x = self->x;
self->bounds.y = self->y;
self->bounds.w = 140;
self->bounds.h = self->h;
}
return &self->bounds;
}

View File

@ -23,7 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static Entity *tankCommander; static Entity *tankCommander;
static void tick(void); static void tick(void);
static void touch(Entity *other); static void touch(Entity *other);
static SDL_Rect *getBounds(void);
static void animate(void); static void animate(void);
static void (*superAnimate)(void); static void (*superAnimate)(void);
@ -43,7 +42,6 @@ void initTankTrack(Entity *e, Entity *tank)
e->tick = tick; e->tick = tick;
e->touch = touch; e->touch = touch;
e->getBounds = getBounds;
e->animate = animate; e->animate = animate;
tankCommander = tank; tankCommander = tank;
@ -88,16 +86,6 @@ static void touch(Entity *other)
} }
} }
static SDL_Rect *getBounds(void)
{
self->bounds.x = self->x + 16;
self->bounds.y = self->y;
self->bounds.w = self->w - 32;
self->bounds.h = self->h;
return &self->bounds;
}
static void animate(void) static void animate(void)
{ {
if (tankCommander->dx != 0) if (tankCommander->dx != 0)

View File

@ -24,7 +24,6 @@ static void applyDamage(int damage);
static void walk(void); static void walk(void);
static void die(void); static void die(void);
static void animate(void); static void animate(void);
static SDL_Rect *getBounds(void);
static int canFire(Entity *target); static int canFire(Entity *target);
static void preFire(void); static void preFire(void);
@ -56,7 +55,6 @@ void initCannon(void)
u->applyDamage = applyDamage; u->applyDamage = applyDamage;
u->walk = walk; u->walk = walk;
u->die = die; u->die = die;
u->getBounds= getBounds;
u->canFire = canFire; u->canFire = canFire;
} }
@ -189,16 +187,6 @@ static void animate(void)
} }
static SDL_Rect *getBounds(void)
{
self->bounds.x = self->x + 36;
self->bounds.y = self->y;
self->bounds.w = 36;
self->bounds.h = self->h;
return &self->bounds;
}
static int canFire(Entity *target) static int canFire(Entity *target)
{ {
return abs(target->y - self->y) <= MAP_TILE_SIZE; return abs(target->y - self->y) <= MAP_TILE_SIZE;

View File

@ -25,7 +25,6 @@ static void reset(void);
static void action(void); static void action(void);
static void applyDamage(int damage); static void applyDamage(int damage);
static float bounce(float x); static float bounce(float x);
static SDL_Rect *getBounds(void);
static void tick(void); static void tick(void);
static void touch(Entity *other); static void touch(Entity *other);
static void animate(void); static void animate(void);
@ -64,7 +63,6 @@ void initEntity(Entity *e)
e->touch = touch; e->touch = touch;
e->applyDamage = applyDamage; e->applyDamage = applyDamage;
e->bounce = bounce; e->bounce = bounce;
e->getBounds = getBounds;
e->getCurrentSprite = getCurrentSprite; e->getCurrentSprite = getCurrentSprite;
e->load = load; e->load = load;
@ -88,44 +86,30 @@ static void action(void)
{ {
} }
static SDL_Rect *getBounds(void)
{
self->bounds.x = self->x;
self->bounds.y = self->y;
self->bounds.w = self->w;
self->bounds.h = self->h;
return &self->bounds;
}
static void animate(void) static void animate(void)
{ {
Sprite *s; Sprite *s;
SDL_Rect *r;
s = self->sprite[self->facing];
if (self->spriteTime != -1) if (self->spriteTime != -1)
{ {
self->spriteTime--; if (--self->spriteTime <= 0)
if (self->spriteTime <= 0)
{ {
s = self->sprite[self->facing]; if (++self->spriteFrame >= s->numFrames)
self->spriteFrame++;
if (self->spriteFrame >= s->numFrames)
{ {
self->spriteFrame = 0; self->spriteFrame = 0;
} }
self->spriteTime = self->sprite[self->facing]->times[self->spriteFrame]; self->spriteTime = self->sprite[self->facing]->times[self->spriteFrame];
self->w = s->w;
self->h = s->h;
} }
} }
}
r = &self->sprite[self->facing]->frames[self->spriteFrame]->rect;
void setEntitySize(Entity *e)
{ self->w = r->w;
self->h = r->h;
} }
static float bounce(float x) static float bounce(float x)

View File

@ -45,8 +45,6 @@ void initWeaponPickup(Entity *e)
i->sprite[0] = i->sprite[1] = i->sprite[2] = getSprite("Weapon"); i->sprite[0] = i->sprite[1] = i->sprite[2] = getSprite("Weapon");
i->spriteFrame = i->weaponType; i->spriteFrame = i->weaponType;
i->spriteTime = -1; i->spriteTime = -1;
setEntitySize(e);
if (i->provided) if (i->provided)
{ {

View File

@ -25,7 +25,6 @@ extern void setGameplayMessage(int type, char *format, ...);
extern void initConsumable(Entity *e); extern void initConsumable(Entity *e);
extern Sprite *getSprite(char *name); extern Sprite *getSprite(char *name);
extern void pickupItem(void); extern void pickupItem(void);
extern void setEntitySize(Entity *e);
extern int touchedPlayer(Entity *e); extern int touchedPlayer(Entity *e);
extern Entity *self; extern Entity *self;

View File

@ -52,12 +52,6 @@ Entity *initDoor(void)
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("Door"); s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("Door");
if (s->closedX == -1 && s->closedY == -1)
{
s->closedX = (int) s->x;
s->closedY = (int) s->y;
}
s->init = init; s->init = init;
s->tick = tick; s->tick = tick;
s->touch = touch; s->touch = touch;
@ -132,7 +126,7 @@ static void tick(void)
s = (Structure*)self; s = (Structure*)self;
s->dx = s->dy = 0; s->dx = s->dy = 0;
if (isOpening()) if (isOpening())
{ {
getSlope(s->tx, s->ty, s->x, s->y, &s->dx, &s->dy); getSlope(s->tx, s->ty, s->x, s->y, &s->dx, &s->dy);
@ -286,7 +280,7 @@ static void load(cJSON *root)
s->tx = cJSON_GetObjectItem(root, "tx")->valueint; s->tx = cJSON_GetObjectItem(root, "tx")->valueint;
s->ty = cJSON_GetObjectItem(root, "ty")->valueint; s->ty = cJSON_GetObjectItem(root, "ty")->valueint;
s->speed = cJSON_GetObjectItem(root, "speed")->valueint; s->speed = cJSON_GetObjectItem(root, "speed")->valueint;
s->state = cJSON_GetObjectItem(root, "state")->valueint; s->state = lookup(cJSON_GetObjectItem(root, "state")->valuestring);
} }
static void save(cJSON *root) static void save(cJSON *root)
@ -300,7 +294,7 @@ static void save(cJSON *root)
cJSON_AddNumberToObject(root, "tx", s->tx); cJSON_AddNumberToObject(root, "tx", s->tx);
cJSON_AddNumberToObject(root, "ty", s->ty); cJSON_AddNumberToObject(root, "ty", s->ty);
cJSON_AddNumberToObject(root, "speed", s->speed); cJSON_AddNumberToObject(root, "speed", s->speed);
cJSON_AddNumberToObject(root, "state", s->state); cJSON_AddStringToObject(root, "state", getLookupName("DOOR_", s->state));
cJSON_AddStringToObject(root, "requiredKey", s->requiredItem); cJSON_AddStringToObject(root, "requiredKey", s->requiredItem);
if (strcmp(s->sprite[0]->name, "Door") != 0) if (strcmp(s->sprite[0]->name, "Door") != 0)

View File

@ -28,6 +28,8 @@ extern void setGameplayMessage(int type, char *format, ...);
extern int hasItem(char *name); extern int hasItem(char *name);
extern void removeItem(char *name); extern void removeItem(char *name);
extern Structure *createStructure(void); extern Structure *createStructure(void);
extern char *getLookupName(const char *prefix, long num);
extern long lookup(const char *name);
extern Entity *self; extern Entity *self;
extern Dev dev; extern Dev dev;

View File

@ -23,7 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void tick(void); static void tick(void);
static void action(void); static void action(void);
static void touch(Entity *other); static void touch(Entity *other);
static SDL_Rect *getBounds(void);
Entity *initExit(void) Entity *initExit(void)
{ {
@ -55,7 +54,6 @@ Entity *initExit(void)
s->tick = tick; s->tick = tick;
s->action = action; s->action = action;
s->touch = touch; s->touch = touch;
s->getBounds = getBounds;
return (Entity*)s; return (Entity*)s;
} }
@ -127,13 +125,3 @@ static void touch(Entity *other)
s->bobTouching = 2; s->bobTouching = 2;
} }
} }
static SDL_Rect *getBounds(void)
{
self->bounds.x = self->x + 64;
self->bounds.y = self->y;
self->bounds.w = 2;
self->bounds.h = self->h;
return &self->bounds;
}

View File

@ -140,7 +140,7 @@ static void load(cJSON *root)
s->tx = cJSON_GetObjectItem(root, "tx")->valueint; s->tx = cJSON_GetObjectItem(root, "tx")->valueint;
s->ty = cJSON_GetObjectItem(root, "ty")->valueint; s->ty = cJSON_GetObjectItem(root, "ty")->valueint;
s->speed = cJSON_GetObjectItem(root, "speed")->valueint; s->speed = cJSON_GetObjectItem(root, "speed")->valueint;
s->state = cJSON_GetObjectItem(root, "state")->valueint; s->state = lookup(cJSON_GetObjectItem(root, "state")->valuestring);
s->startX = cJSON_GetObjectItem(root, "startX")->valueint; s->startX = cJSON_GetObjectItem(root, "startX")->valueint;
s->startY = cJSON_GetObjectItem(root, "startY")->valueint; s->startY = cJSON_GetObjectItem(root, "startY")->valueint;
s->waitTime = cJSON_GetObjectItem(root, "waitTime")->valueint; s->waitTime = cJSON_GetObjectItem(root, "waitTime")->valueint;
@ -157,7 +157,7 @@ static void save(cJSON *root)
cJSON_AddNumberToObject(root, "tx", s->tx); cJSON_AddNumberToObject(root, "tx", s->tx);
cJSON_AddNumberToObject(root, "ty", s->ty); cJSON_AddNumberToObject(root, "ty", s->ty);
cJSON_AddNumberToObject(root, "speed", s->speed); cJSON_AddNumberToObject(root, "speed", s->speed);
cJSON_AddNumberToObject(root, "state", s->state); cJSON_AddStringToObject(root, "state", getLookupName("LIFT_", s->state));
cJSON_AddNumberToObject(root, "startX", s->startX); cJSON_AddNumberToObject(root, "startX", s->startX);
cJSON_AddNumberToObject(root, "startY", s->startY); cJSON_AddNumberToObject(root, "startY", s->startY);
cJSON_AddNumberToObject(root, "waitTime", s->waitTime); cJSON_AddNumberToObject(root, "waitTime", s->waitTime);

View File

@ -27,5 +27,7 @@ extern void observeActivation(Entity *e);
extern int isOnScreen(Entity *e); extern int isOnScreen(Entity *e);
extern void setGameplayMessage(int type, char *format, ...); extern void setGameplayMessage(int type, char *format, ...);
extern Structure *createStructure(void); extern Structure *createStructure(void);
extern char *getLookupName(const char *prefix, long num);
extern long lookup(const char *name);
extern Entity *self; extern Entity *self;

View File

@ -133,7 +133,6 @@ struct Entity {
int isOnScreen; int isOnScreen;
int isMissionTarget; int isMissionTarget;
int observationTime; int observationTime;
SDL_Rect bounds;
Entity *riding; Entity *riding;
Entity *owner; Entity *owner;
unsigned long flags; unsigned long flags;
@ -145,7 +144,6 @@ struct Entity {
void (*die)(void); void (*die)(void);
void (*animate)(void); void (*animate)(void);
void (*walk)(void); void (*walk)(void);
void (*setSize)(void);
float (*bounce)(float x); float (*bounce)(float x);
void (*teleport)(float tx, float ty); void (*teleport)(float tx, float ty);
void (*activate)(int active); void (*activate)(int active);
@ -154,7 +152,6 @@ struct Entity {
SDL_Rect *(*getCurrentSprite)(void); SDL_Rect *(*getCurrentSprite)(void);
void (*load)(cJSON *root); void (*load)(cJSON *root);
void (*save)(cJSON *root); void (*save)(cJSON *root);
SDL_Rect *(*getBounds)(void);
Entity *next; Entity *next;
}; };

View File

@ -32,6 +32,12 @@ void initLookups(void)
addLookup("FACING_LEFT", FACING_LEFT); addLookup("FACING_LEFT", FACING_LEFT);
addLookup("FACING_RIGHT", FACING_RIGHT); addLookup("FACING_RIGHT", FACING_RIGHT);
addLookup("LIFT_GOTO_FINISH", LIFT_GOTO_FINISH);
addLookup("LIFT_GOTO_START", LIFT_GOTO_START);
addLookup("DOOR_OPEN", DOOR_OPEN);
addLookup("DOOR_CLOSED", DOOR_CLOSED);
} }
static void addLookup(const char *name, long value) static void addLookup(const char *name, long value)

View File

@ -97,11 +97,6 @@ float wrap(float value, float low, float high)
return value; return value;
} }
int lineIntersectsRect(SDL_Rect r, int x1, int y1, int x2, int y2)
{
return 0;
}
unsigned long hashcode(const char *str) unsigned long hashcode(const char *str)
{ {
unsigned long hash = 5381; unsigned long hash = 5381;

View File

@ -93,12 +93,10 @@ void doEntities(void)
{ {
self->alive = ALIVE_DEAD; self->alive = ALIVE_DEAD;
} }
self->setSize();
self->riding = NULL; self->riding = NULL;
if (self->w != 0 && self->h != 0 && (!(self->flags & (EF_TELEPORTING | EF_GONE)))) if (!(self->flags & (EF_TELEPORTING | EF_GONE)))
{ {
addToQuadtree(self, &world.quadtree); addToQuadtree(self, &world.quadtree);
} }
@ -141,11 +139,11 @@ void doEntities(void)
if (touched[i]) if (touched[i])
{ {
self->touch(touched[i]); self->touch(touched[i]);
}
if (touched[i]->isStatic)
if (touched[i]->isStatic) {
{ touched[i]->touch(self); /* for objects that never move */
touched[i]->touch(self); /* for objects that never move */ }
} }
} }
@ -162,7 +160,7 @@ void doEntities(void)
} }
} }
if ((!(self->flags & EF_FLICKER)) && flicker) if ((self->flags & EF_FLICKER) && flicker)
{ {
self->isOnScreen = 0; self->isOnScreen = 0;
} }
@ -196,8 +194,6 @@ void drawEntities(int plane)
for (self = world.entityHead.next ; self != NULL ; self = self->next) for (self = world.entityHead.next ; self != NULL ; self = self->next)
{ {
self->isOnScreen = 1;
draw = self->isOnScreen && !(self->flags & EF_GONE) && self->plane == plane; draw = self->isOnScreen && !(self->flags & EF_GONE) && self->plane == plane;
if (draw) if (draw)
@ -249,7 +245,7 @@ static void moveEntity(void)
if (!(self->flags & EF_WEIGHTLESS)) if (!(self->flags & EF_WEIGHTLESS))
{ {
self->dy += GRAVITY_POWER; self->dy += GRAVITY_POWER;
self->dy =limit(self->dy, -25, 25); self->dy = limit(self->dy, -25, 25);
if (self->dy > 0 && self->dy < 1) if (self->dy > 0 && self->dy < 1)
{ {
@ -260,7 +256,7 @@ static void moveEntity(void)
case ENV_WATER: case ENV_WATER:
self->flags &= ~EF_BOUNCES; self->flags &= ~EF_BOUNCES;
if ((self->flags & EF_SWIMS) == 0) if (!(self->flags & EF_SWIMS))
{ {
self->dy += GRAVITY_POWER; self->dy += GRAVITY_POWER;
self->dy = limit(self->dy, -2, 2); self->dy = limit(self->dy, -2, 2);
@ -282,7 +278,7 @@ static void moveEntity(void)
// Deal with x movement // Deal with x movement
position.x = self->x; position.x = self->x;
position.y = self->x; position.y = self->y;
position.x += self->dx; position.x += self->dx;
moveToOthers(self->dx, 0, &position); moveToOthers(self->dx, 0, &position);
moveToMap(self->dx, 0, &position); moveToMap(self->dx, 0, &position);
@ -630,7 +626,7 @@ static void moveToMap(float dx, float dy, PointF *position)
{ {
self->isOnGround = 1; self->isOnGround = 1;
} }
position->y = (my * MAP_TILE_SIZE) - adjY; position->y = (my * MAP_TILE_SIZE) - adjY;
self->dy = self->bounce(self->dy); self->dy = self->bounce(self->dy);
self->dy = limit(self->dy, JUMP_POWER, -JUMP_POWER); self->dy = limit(self->dy, JUMP_POWER, -JUMP_POWER);
@ -844,12 +840,9 @@ Entity *getRandomObjectiveEntity(void)
for (e = world.entityHead.next ; e != NULL ; e = e->next) for (e = world.entityHead.next ; e != NULL ; e = e->next)
{ {
if (e->isMissionTarget) if (e->isMissionTarget && rand() % 4 == 0)
{ {
if (rand() % 4 == 0) return e;
{
rtn = e;
}
} }
} }

View File

@ -362,7 +362,6 @@ static void spawnEnemies(void)
u = (Unit*) createEntity(name); u = (Unit*) createEntity(name);
u->animate(); u->animate();
u->setSize();
x /= MAP_TILE_SIZE; x /= MAP_TILE_SIZE;
y /= MAP_TILE_SIZE; y /= MAP_TILE_SIZE;