diff --git a/src/entities/blobs/bob.c b/src/entities/blobs/bob.c index 4ca4c2e..09c8697 100644 --- a/src/entities/blobs/bob.c +++ b/src/entities/blobs/bob.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "bob.h" +static SDL_Rect *getCurrentSprite(void); static void load(cJSON *root); static void save(cJSON *root); @@ -35,6 +36,7 @@ void initBob(void) u->sprite[FACING_RIGHT] = getSprite("BobRight"); u->sprite[FACING_DIE] = getSprite("BobSpin"); + u->getCurrentSprite = getCurrentSprite; u->load = load; u->save = save; } @@ -49,6 +51,16 @@ int numCarriedItems(void) return 0; } +static SDL_Rect *getCurrentSprite(void) +{ + if (world.bob->alive == ALIVE_ALIVE && world.bob->stunTimer <= 0) + { + return &world.bob->sprite[world.bob->facing]->frames[0]->rect; + } + + return &world.bob->sprite[FACING_DIE]->frames[0]->rect; +} + static void load(cJSON *root) { world.bob->x = cJSON_GetObjectItem(root, "x")->valueint; diff --git a/src/entities/boss/blobBoss.c b/src/entities/boss/blobBoss.c index d49f6fe..4acfa1b 100644 --- a/src/entities/boss/blobBoss.c +++ b/src/entities/boss/blobBoss.c @@ -24,7 +24,7 @@ static void activate(int activate); static void walk(void); static void tick(void); static void changeEnvironment(void); -static Sprite *getCurrentSprite(void); +static SDL_Rect *getCurrentSprite(void); static void animate(void); static void applyDamage(int amount); static void moveTowardsPlayer(void); @@ -171,7 +171,7 @@ static void die1(void) b->action = die2; } -static Sprite *getCurrentSprite(void) +static SDL_Rect *getCurrentSprite(void) { Boss *b; @@ -179,10 +179,10 @@ static Sprite *getCurrentSprite(void) if (b->stunTimer > 0 || b->health <= 0) { - return b->sprite[FACING_DIE]; + return &b->sprite[FACING_DIE]->frames[0]->rect; } - return b->sprite[b->facing]; + return &b->sprite[b->facing]->frames[0]->rect; } static void animate(void) diff --git a/src/entities/boss/eyeDroidCommander.c b/src/entities/boss/eyeDroidCommander.c index 30061b2..90ebac2 100644 --- a/src/entities/boss/eyeDroidCommander.c +++ b/src/entities/boss/eyeDroidCommander.c @@ -33,7 +33,7 @@ static void attackMissile(void); static void preFire(void); static void attack(void); static void applyDamage(int amount); -static Sprite *getCurrentSprite(void); +static SDL_Rect *getCurrentSprite(void); static int brakingTimer; static Sprite *aimedSprite; @@ -382,12 +382,12 @@ static void die2() } } -static Sprite *getCurrentSprite(void) +static SDL_Rect *getCurrentSprite(void) { if (self->health <= 0) { - return self->sprite[FACING_DIE]; + return &self->sprite[FACING_DIE]->frames[0]->rect; } - return self->sprite[self->facing]; + return &self->sprite[self->facing]->frames[0]->rect; } diff --git a/src/entities/entity.c b/src/entities/entity.c index c898072..b7bf4dc 100644 --- a/src/entities/entity.c +++ b/src/entities/entity.c @@ -28,6 +28,7 @@ static void tick(void); static void touch(Entity *other); static void load(cJSON *root); static void save(cJSON *root); +static SDL_Rect *getCurrentSprite(void); void initEntity(Entity *e) { @@ -55,6 +56,7 @@ void initEntity(Entity *e) e->applyDamage = applyDamage; e->bounce = bounce; e->getBounds = getBounds; + e->getCurrentSprite = getCurrentSprite; e->load = load; e->save = save; @@ -134,6 +136,11 @@ static void touch(Entity *other) { } +static SDL_Rect *getCurrentSprite(void) +{ + return &self->sprite[self->facing]->frames[0]->rect; +} + static void load(cJSON *root) { SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Entity [name=%s, type=%d, x=%d, y=%d] cannot be loaded", self->name, self->type, (int)self->x, (int)self->y); diff --git a/src/entities/unit.c b/src/entities/unit.c index 24a6bc0..23af7c2 100644 --- a/src/entities/unit.c +++ b/src/entities/unit.c @@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. void unitTick(void); static void attack(void); static int canFire(Entity *target); +static SDL_Rect *getCurrentSprite(void); static void preFire(void); static void load(cJSON *root); static void save(cJSON *root); @@ -57,6 +58,7 @@ Unit *createUnit(void) u->preFire = preFire; u->attack = attack; u->canFire = canFire; + u->getCurrentSprite = getCurrentSprite; u->load = load; u->save = save; @@ -224,6 +226,16 @@ static int canFire(Entity *target) return 0; } +static SDL_Rect *getCurrentSprite(void) +{ + if (self->alive == ALIVE_ALIVE) + { + return &self->sprite[self->facing]->frames[0]->rect; + } + + return &self->sprite[FACING_DIE]->frames[0]->rect; +} + static void load(cJSON *root) { Unit *u; diff --git a/src/structs.h b/src/structs.h index 6886f2c..183eeee 100644 --- a/src/structs.h +++ b/src/structs.h @@ -144,7 +144,7 @@ struct Entity { void (*activate)(int active); void (*applyDamage)(int amount); void (*changeEnvironment)(void); - Sprite *(*getCurrentSprite)(void); + SDL_Rect *(*getCurrentSprite)(void); void (*load)(cJSON *root); void (*save)(cJSON *root); SDL_Rect *(*getBounds)(void); diff --git a/src/world/entities.c b/src/world/entities.c index 08e92ad..fee3733 100644 --- a/src/world/entities.c +++ b/src/world/entities.c @@ -29,14 +29,13 @@ void initEntities(void) void drawEntities(void) { - Entity *e; int x, y; - for (e = world.entityHead.next ; e != NULL ; e = e->next) + for (self = world.entityHead.next ; self != NULL ; self = self->next) { - x = (-camera.x + e->x); - y = (-camera.y + e->y); + x = (-camera.x + self->x); + y = (-camera.y + self->y); - blitRect(atlasTexture->texture, x, y, &e->sprite[0]->frames[0]->rect, 0); + blitRect(atlasTexture->texture, x, y, self->getCurrentSprite(), 0); } } diff --git a/src/world/entities.h b/src/world/entities.h index 5493117..9187443 100644 --- a/src/world/entities.h +++ b/src/world/entities.h @@ -23,5 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern Texture *getTexture(const char *filename); extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center); +extern Entity *self; extern Camera camera; extern World world;