Animation and drawing updates.

This commit is contained in:
Steve 2018-02-02 08:00:27 +00:00
parent aba616b085
commit 0872a3b82b
9 changed files with 105 additions and 49 deletions

View File

@ -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)

View File

@ -449,6 +449,7 @@ typedef struct {
int isReturnVisit;
int missionCompleteTimer;
int betweenTimer;
int mapAnimTimer;
Quadtree quadtree;
Entity entityHead, *entityTail;
Particle particleHead, *particleTail;

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -31,20 +31,24 @@ 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)
{
if (self->plane == plane)
{
x = (-camera.x + self->x);
y = (-camera.y + self->y);
@ -52,6 +56,7 @@ void drawEntities(void)
blitRect(atlasTexture->texture, x, y, self->getCurrentSprite(), 0);
}
}
}
void activateEntities(char *names, int activate)
{
@ -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;
}

View File

@ -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);

View File

@ -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)
{
}

View File

@ -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;