diff --git a/src/entities/entity.c b/src/entities/entity.c index 6d48daf..cabde7d 100644 --- a/src/entities/entity.c +++ b/src/entities/entity.c @@ -53,6 +53,9 @@ void initEntity(Entity *e) e->thinkTime = 0; + e->spriteFrame = -1; + e->spriteTime = 0; + e->init = init; e->reset = reset; e->action = action; @@ -109,7 +112,7 @@ static void animate(void) self->spriteFrame++; - if (self->spriteFrame > s->numFrames) + if (self->spriteFrame >= s->numFrames) { self->spriteFrame = 0; } @@ -162,7 +165,7 @@ static void touch(Entity *other) static SDL_Rect *getCurrentSprite(void) { - return &self->sprite[self->facing]->frames[0]->rect; + return &self->sprite[self->facing]->frames[self->spriteFrame]->rect; } static void load(cJSON *root) diff --git a/src/structs.h b/src/structs.h index 32919d7..bbbcd6d 100644 --- a/src/structs.h +++ b/src/structs.h @@ -449,6 +449,7 @@ typedef struct { int isReturnVisit; int missionCompleteTimer; int betweenTimer; + int mapAnimTimer; Quadtree quadtree; Entity entityHead, *entityTail; Particle particleHead, *particleTail; diff --git a/src/test/atlasTest.c b/src/test/atlasTest.c index 93096a1..e34a6b5 100644 --- a/src/test/atlasTest.c +++ b/src/test/atlasTest.c @@ -20,15 +20,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "atlasTest.h" -static Entity *track; static void logic(void); static void draw(void); -static int timeout; -static void trackRandomEntity(void); + +static Texture *background; void initAtlasTest(void) { initGame(); + initHub(); app.delegate.logic = &logic; @@ -36,45 +36,55 @@ void initAtlasTest(void) loadWorld("data/maps/beachApproach.json"); + initWorld(); + initMap(); initEntities(); - timeout = FPS; + background = getTexture(world.background); - track = &world.entityHead; - - cameraTrack(track); + cameraTrack(findEntity("Transmitter")); } static void logic(void) { + doWorld(); + doEntities(); doParticles(); - if (--timeout <= 0) + if (app.keyboard[SDL_SCANCODE_UP]) { - trackRandomEntity(); + camera.y -= CAMERA_SCROLL_SPEED; } + + if (app.keyboard[SDL_SCANCODE_DOWN]) + { + camera.y += CAMERA_SCROLL_SPEED; + } + + if (app.keyboard[SDL_SCANCODE_LEFT]) + { + camera.x -= CAMERA_SCROLL_SPEED; + } + + if (app.keyboard[SDL_SCANCODE_RIGHT]) + { + camera.x += CAMERA_SCROLL_SPEED; + } + + clipCamera(); } static void draw(void) { + blitScaled(background->texture, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0); + + drawEntities(PLANE_BACKGROUND); + drawMap(); - drawEntities(); -} - -static void trackRandomEntity(void) -{ - track = track->next; - - if (track == NULL) - { - track = (Entity*)world.bob; - } - - cameraTrack(track); - timeout = FPS; + drawEntities(PLANE_FOREGROUND); } diff --git a/src/test/atlasTest.h b/src/test/atlasTest.h index ac9ccf4..2479142 100644 --- a/src/test/atlasTest.h +++ b/src/test/atlasTest.h @@ -20,16 +20,25 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" +#define CAMERA_SCROLL_SPEED 16 + +extern void initWorld(void); extern void initMap(void); extern void initHub(void); extern void initGame(void); extern void initEntities(void); extern void loadWorld(char *filename); extern void drawMap(void); -extern void drawEntities(void); +extern void drawEntities(int plane); extern void cameraTrack(Entity *e); extern void doEntities(void); extern void doParticles(void); +extern void doWorld(void); +extern Entity *findEntity(char *name); +extern void clipCamera(void); +extern void blitScaled(SDL_Texture *texture, int x, int y, int w, int h, int center); +extern Texture *getTexture(const char *filename); extern App app; +extern Camera camera; extern World world; diff --git a/src/world/camera.c b/src/world/camera.c index f294a1d..2e3d4d4 100644 --- a/src/world/camera.c +++ b/src/world/camera.c @@ -20,7 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "camera.h" -static void clip(void); +void clipCamera(void); void cameraTrack(Entity *e) { @@ -30,7 +30,7 @@ void cameraTrack(Entity *e) camera.x -= (SCREEN_WIDTH / 2); camera.y -= (SCREEN_HEIGHT / 2); - clip(); + clipCamera(); } float cameraChase(Entity *e, int maxSpeed) @@ -77,12 +77,12 @@ float cameraChase(Entity *e, int maxSpeed) camera.x = (int) x; camera.y = (int) y; - clip(); + clipCamera(); return dist; } -void clip(void) +void clipCamera(void) { camera.x = (int) limit(camera.x, world.map.bounds.x, world.map.bounds.w); camera.y = (int) limit(camera.y, world.map.bounds.y, world.map.bounds.h); diff --git a/src/world/entities.c b/src/world/entities.c index 5752077..b4c3cef 100644 --- a/src/world/entities.c +++ b/src/world/entities.c @@ -31,25 +31,30 @@ void doEntities(void) { for (self = world.entityHead.next ; self != NULL ; self = self->next) { - self->tick(); - if (--self->thinkTime <= 0) { self->action(); } + + self->tick(); + + self->animate(); } } -void drawEntities(void) +void drawEntities(int plane) { int x, y; for (self = world.entityHead.next ; self != NULL ; self = self->next) { - x = (-camera.x + self->x); - y = (-camera.y + self->y); - - blitRect(atlasTexture->texture, x, y, self->getCurrentSprite(), 0); + if (self->plane == plane) + { + x = (-camera.x + self->x); + y = (-camera.y + self->y); + + blitRect(atlasTexture->texture, x, y, self->getCurrentSprite(), 0); + } } } @@ -94,3 +99,18 @@ void teleport(Entity *e, float tx, float ty) addTeleportStars(e); } + +Entity *findEntity(char *name) +{ + Entity *e; + + for (e = world.entityHead.next ; e != NULL ; e = e->next) + { + if (strcmp(e->name, name) == 0) + { + return e; + } + } + + return NULL; +} diff --git a/src/world/map.c b/src/world/map.c index 58f1c04..b34f077 100644 --- a/src/world/map.c +++ b/src/world/map.c @@ -26,7 +26,6 @@ static void loadTileset(void); static void loadDecals(void); static SDL_Rect *loadTile(char *filename); -static int animTimer; static int MAX_Y; static Texture *atlasTexture; static SDL_Rect *tiles[MAP_TILE_MAX]; @@ -41,8 +40,6 @@ void initMap(void) world.map.bounds.w = 0; world.map.bounds.h = 0; - animTimer = 0; - atlasTexture = getTexture("gfx/atlas/atlas.png"); loadMapData(); @@ -77,7 +74,7 @@ void drawMap(void) { tile = world.map.data[mx][my]; - if (animTimer == 0) + if (world.mapAnimTimer == 0) { if (tile >= MAP_TILE_ANIMATED_WATER && tile < MAP_TILE_ANIMATED_SLIME) { @@ -97,6 +94,11 @@ void drawMap(void) if (tile != MAP_TILE_AIR) { + if (tile >= MAP_TILE_SOLID && tile < MAP_TILE_NON_SOLID) + { + blitRect(atlasTexture->texture, x + 2, y + 2, tiles[MAP_TILE_OUTSIDE], 0); + } + blitRect(atlasTexture->texture, x, y, tiles[tile], 0); decal = world.map.decal[mx][my]; @@ -120,14 +122,6 @@ void drawMap(void) } } -void animateMap(void) -{ - if (--animTimer < 0) - { - animTimer = 4; - } -} - int isWithinMap(int x, int y) { return (x >= 0 && y >= 0 && x < MAP_WIDTH && y < MAP_HEIGHT); diff --git a/src/world/world.c b/src/world/world.c index 8eb7e03..acd22f6 100644 --- a/src/world/world.c +++ b/src/world/world.c @@ -20,6 +20,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "world.h" +static Texture *background; + +void initWorld(void) +{ + background = getTexture(world.background); +} + +void doWorld(void) +{ + if (--world.mapAnimTimer < 0) + { + world.mapAnimTimer = 4; + } +} + void observeActivation(Entity *e) { } diff --git a/src/world/world.h b/src/world/world.h index 8ad0dc9..47828c9 100644 --- a/src/world/world.h +++ b/src/world/world.h @@ -19,3 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../common.h" + +extern Texture *getTexture(const char *filename); + +extern World world;