diff --git a/src/combat/combat.c b/src/combat/combat.c index 6322bb3..437c4aa 100644 --- a/src/combat/combat.c +++ b/src/combat/combat.c @@ -61,7 +61,7 @@ static int isBlockedByMap(Entity *src, Entity *dest) mapBlock.w = 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; } @@ -75,7 +75,7 @@ static int isBlockedByMap(Entity *src, Entity *dest) static int isBlockedByEntities(Entity *src, Entity *dest) { Entity **candidates, *e; - SDL_Rect losBounds; + SDL_Rect losBounds, eBounds; int i; losBounds.x = (int) MIN(src->x, dest->x); @@ -92,7 +92,12 @@ static int isBlockedByEntities(Entity *src, Entity *dest) 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; } diff --git a/src/combat/combat.h b/src/combat/combat.h index ed91f43..465572a 100644 --- a/src/combat/combat.h +++ b/src/combat/combat.h @@ -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 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 int lineRectIntersection(int x1, int y1, int x2, int y2, SDL_Rect *r); extern World world; diff --git a/src/entities/boss/tankCommander.c b/src/entities/boss/tankCommander.c index 64f4142..4a96042 100644 --- a/src/entities/boss/tankCommander.c +++ b/src/entities/boss/tankCommander.c @@ -29,7 +29,6 @@ static void die1(void); static void die2(void); static void attack(void); static void applyDamage(int amount); -static SDL_Rect *getBounds(void); static void preFire(void); static void selectWeapon(void); static void moveTowardsPlayer(void); @@ -62,7 +61,6 @@ void initTankCommander(Entity *e) b->tick = tick; b->die = die1; b->applyDamage = applyDamage; - b->getBounds = getBounds; brakingTimer = 0; @@ -348,23 +346,3 @@ static void applyDamage(int amount) 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; -} diff --git a/src/entities/boss/tankTrack.c b/src/entities/boss/tankTrack.c index 0ddf8a6..51e50c2 100644 --- a/src/entities/boss/tankTrack.c +++ b/src/entities/boss/tankTrack.c @@ -23,7 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static Entity *tankCommander; static void tick(void); static void touch(Entity *other); -static SDL_Rect *getBounds(void); static void animate(void); static void (*superAnimate)(void); @@ -43,7 +42,6 @@ void initTankTrack(Entity *e, Entity *tank) e->tick = tick; e->touch = touch; - e->getBounds = getBounds; e->animate = animate; 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) { if (tankCommander->dx != 0) diff --git a/src/entities/cannons/cannon.c b/src/entities/cannons/cannon.c index 0b03526..54a6d19 100644 --- a/src/entities/cannons/cannon.c +++ b/src/entities/cannons/cannon.c @@ -24,7 +24,6 @@ static void applyDamage(int damage); static void walk(void); static void die(void); static void animate(void); -static SDL_Rect *getBounds(void); static int canFire(Entity *target); static void preFire(void); @@ -56,7 +55,6 @@ void initCannon(void) u->applyDamage = applyDamage; u->walk = walk; u->die = die; - u->getBounds= getBounds; 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) { return abs(target->y - self->y) <= MAP_TILE_SIZE; diff --git a/src/entities/entity.c b/src/entities/entity.c index cabde7d..cede9dd 100644 --- a/src/entities/entity.c +++ b/src/entities/entity.c @@ -25,7 +25,6 @@ static void reset(void); static void action(void); static void applyDamage(int damage); static float bounce(float x); -static SDL_Rect *getBounds(void); static void tick(void); static void touch(Entity *other); static void animate(void); @@ -64,7 +63,6 @@ void initEntity(Entity *e) e->touch = touch; e->applyDamage = applyDamage; e->bounce = bounce; - e->getBounds = getBounds; e->getCurrentSprite = getCurrentSprite; 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) { Sprite *s; + SDL_Rect *r; + + s = self->sprite[self->facing]; if (self->spriteTime != -1) { - self->spriteTime--; - - if (self->spriteTime <= 0) + if (--self->spriteTime <= 0) { - s = self->sprite[self->facing]; - - self->spriteFrame++; - - if (self->spriteFrame >= s->numFrames) + if (++self->spriteFrame >= s->numFrames) { self->spriteFrame = 0; } self->spriteTime = self->sprite[self->facing]->times[self->spriteFrame]; - self->w = s->w; - self->h = s->h; } } -} - -void setEntitySize(Entity *e) -{ + + r = &self->sprite[self->facing]->frames[self->spriteFrame]->rect; + + self->w = r->w; + self->h = r->h; } static float bounce(float x) diff --git a/src/entities/items/weaponPickup.c b/src/entities/items/weaponPickup.c index c627755..9ac915a 100644 --- a/src/entities/items/weaponPickup.c +++ b/src/entities/items/weaponPickup.c @@ -45,8 +45,6 @@ void initWeaponPickup(Entity *e) i->sprite[0] = i->sprite[1] = i->sprite[2] = getSprite("Weapon"); i->spriteFrame = i->weaponType; i->spriteTime = -1; - - setEntitySize(e); if (i->provided) { diff --git a/src/entities/items/weaponPickup.h b/src/entities/items/weaponPickup.h index fdacde6..0be11af 100644 --- a/src/entities/items/weaponPickup.h +++ b/src/entities/items/weaponPickup.h @@ -25,7 +25,6 @@ extern void setGameplayMessage(int type, char *format, ...); extern void initConsumable(Entity *e); extern Sprite *getSprite(char *name); extern void pickupItem(void); -extern void setEntitySize(Entity *e); extern int touchedPlayer(Entity *e); extern Entity *self; diff --git a/src/entities/structures/door.c b/src/entities/structures/door.c index 7c44c07..b46246e 100644 --- a/src/entities/structures/door.c +++ b/src/entities/structures/door.c @@ -52,12 +52,6 @@ Entity *initDoor(void) 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->tick = tick; s->touch = touch; @@ -132,7 +126,7 @@ static void tick(void) s = (Structure*)self; s->dx = s->dy = 0; - + if (isOpening()) { 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->ty = cJSON_GetObjectItem(root, "ty")->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) @@ -300,7 +294,7 @@ static void save(cJSON *root) cJSON_AddNumberToObject(root, "tx", s->tx); cJSON_AddNumberToObject(root, "ty", s->ty); 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); if (strcmp(s->sprite[0]->name, "Door") != 0) diff --git a/src/entities/structures/door.h b/src/entities/structures/door.h index 2f1d791..af447a8 100644 --- a/src/entities/structures/door.h +++ b/src/entities/structures/door.h @@ -28,6 +28,8 @@ extern void setGameplayMessage(int type, char *format, ...); extern int hasItem(char *name); extern void removeItem(char *name); extern Structure *createStructure(void); +extern char *getLookupName(const char *prefix, long num); +extern long lookup(const char *name); extern Entity *self; extern Dev dev; diff --git a/src/entities/structures/exit.c b/src/entities/structures/exit.c index a10e310..46dcd60 100644 --- a/src/entities/structures/exit.c +++ b/src/entities/structures/exit.c @@ -23,7 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void tick(void); static void action(void); static void touch(Entity *other); -static SDL_Rect *getBounds(void); Entity *initExit(void) { @@ -55,7 +54,6 @@ Entity *initExit(void) s->tick = tick; s->action = action; s->touch = touch; - s->getBounds = getBounds; return (Entity*)s; } @@ -127,13 +125,3 @@ static void touch(Entity *other) 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; -} diff --git a/src/entities/structures/lift.c b/src/entities/structures/lift.c index d4e139b..b29e216 100644 --- a/src/entities/structures/lift.c +++ b/src/entities/structures/lift.c @@ -140,7 +140,7 @@ static void load(cJSON *root) s->tx = cJSON_GetObjectItem(root, "tx")->valueint; s->ty = cJSON_GetObjectItem(root, "ty")->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->startY = cJSON_GetObjectItem(root, "startY")->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, "ty", s->ty); 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, "startY", s->startY); cJSON_AddNumberToObject(root, "waitTime", s->waitTime); diff --git a/src/entities/structures/lift.h b/src/entities/structures/lift.h index 9fab32b..a40cdf8 100644 --- a/src/entities/structures/lift.h +++ b/src/entities/structures/lift.h @@ -27,5 +27,7 @@ extern void observeActivation(Entity *e); extern int isOnScreen(Entity *e); extern void setGameplayMessage(int type, char *format, ...); extern Structure *createStructure(void); +extern char *getLookupName(const char *prefix, long num); +extern long lookup(const char *name); extern Entity *self; diff --git a/src/structs.h b/src/structs.h index b4ee781..39fa02f 100644 --- a/src/structs.h +++ b/src/structs.h @@ -133,7 +133,6 @@ struct Entity { int isOnScreen; int isMissionTarget; int observationTime; - SDL_Rect bounds; Entity *riding; Entity *owner; unsigned long flags; @@ -145,7 +144,6 @@ struct Entity { void (*die)(void); void (*animate)(void); void (*walk)(void); - void (*setSize)(void); float (*bounce)(float x); void (*teleport)(float tx, float ty); void (*activate)(int active); @@ -154,7 +152,6 @@ struct Entity { SDL_Rect *(*getCurrentSprite)(void); void (*load)(cJSON *root); void (*save)(cJSON *root); - SDL_Rect *(*getBounds)(void); Entity *next; }; diff --git a/src/system/lookup.c b/src/system/lookup.c index 6721e77..30f0ea9 100644 --- a/src/system/lookup.c +++ b/src/system/lookup.c @@ -32,6 +32,12 @@ void initLookups(void) addLookup("FACING_LEFT", FACING_LEFT); 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) diff --git a/src/util/maths.c b/src/util/maths.c index b7ea34b..e8d9331 100644 --- a/src/util/maths.c +++ b/src/util/maths.c @@ -97,11 +97,6 @@ float wrap(float value, float low, float high) 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 hash = 5381; diff --git a/src/world/entities.c b/src/world/entities.c index 3e30caa..0ed3d78 100644 --- a/src/world/entities.c +++ b/src/world/entities.c @@ -93,12 +93,10 @@ void doEntities(void) { self->alive = ALIVE_DEAD; } - - self->setSize(); 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); } @@ -141,11 +139,11 @@ void doEntities(void) if (touched[i]) { self->touch(touched[i]); - } - - if (touched[i]->isStatic) - { - touched[i]->touch(self); /* for objects that never move */ + + if (touched[i]->isStatic) + { + 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; } @@ -196,8 +194,6 @@ void drawEntities(int plane) for (self = world.entityHead.next ; self != NULL ; self = self->next) { - self->isOnScreen = 1; - draw = self->isOnScreen && !(self->flags & EF_GONE) && self->plane == plane; if (draw) @@ -249,7 +245,7 @@ static void moveEntity(void) if (!(self->flags & EF_WEIGHTLESS)) { 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) { @@ -260,7 +256,7 @@ static void moveEntity(void) case ENV_WATER: self->flags &= ~EF_BOUNCES; - if ((self->flags & EF_SWIMS) == 0) + if (!(self->flags & EF_SWIMS)) { self->dy += GRAVITY_POWER; self->dy = limit(self->dy, -2, 2); @@ -282,7 +278,7 @@ static void moveEntity(void) // Deal with x movement position.x = self->x; - position.y = self->x; + position.y = self->y; position.x += self->dx; moveToOthers(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; } - + position->y = (my * MAP_TILE_SIZE) - adjY; self->dy = self->bounce(self->dy); 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) { - if (e->isMissionTarget) + if (e->isMissionTarget && rand() % 4 == 0) { - if (rand() % 4 == 0) - { - rtn = e; - } + return e; } } diff --git a/src/world/world.c b/src/world/world.c index a76c612..71b4bda 100644 --- a/src/world/world.c +++ b/src/world/world.c @@ -362,7 +362,6 @@ static void spawnEnemies(void) u = (Unit*) createEntity(name); u->animate(); - u->setSize(); x /= MAP_TILE_SIZE; y /= MAP_TILE_SIZE;