Abstract add entity and animation into functions
This commit is contained in:
parent
c9fc34aee1
commit
fa7947fb90
@ -83,10 +83,11 @@ void renderer_debugString(Renderer *const renderer, Font *const font,
|
|||||||
void renderer_entity(Renderer *renderer, Entity *entity, f32 dt, f32 rotate,
|
void renderer_entity(Renderer *renderer, Entity *entity, f32 dt, f32 rotate,
|
||||||
v3 color)
|
v3 color)
|
||||||
{
|
{
|
||||||
|
// TODO(doyle): Batch into render groups
|
||||||
if ((entity->pos.x < renderer->size.w && entity->pos.x >= 0) &&
|
if ((entity->pos.x < renderer->size.w && entity->pos.x >= 0) &&
|
||||||
(entity->pos.y < renderer->size.h && entity->pos.y >= 0))
|
(entity->pos.y < renderer->size.h && entity->pos.y >= 0))
|
||||||
{
|
{
|
||||||
SpriteAnim *anim = &entity->anim[entity->currAnimIndex];
|
EntityAnim *anim = &entity->anim[entity->currAnimIndex];
|
||||||
v4 texRect = anim->rect[anim->currRectIndex];
|
v4 texRect = anim->rect[anim->currRectIndex];
|
||||||
|
|
||||||
anim->currDuration -= dt;
|
anim->currDuration -= dt;
|
||||||
|
@ -6,6 +6,45 @@
|
|||||||
//choose to load assets outside of WorldTraveller!
|
//choose to load assets outside of WorldTraveller!
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
INTERNAL Entity *addEntity(World *world, v2 pos, v2 size,
|
||||||
|
enum Direction direction, Texture *tex, b32 collides)
|
||||||
|
{
|
||||||
|
|
||||||
|
#ifdef WT_DEBUG
|
||||||
|
ASSERT(tex && world);
|
||||||
|
ASSERT(world->freeEntityIndex < world->maxEntities);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Entity entity = {0};
|
||||||
|
entity.pos = pos;
|
||||||
|
entity.size = size;
|
||||||
|
entity.direction = direction;
|
||||||
|
entity.tex = tex;
|
||||||
|
entity.collides = collides;
|
||||||
|
|
||||||
|
world->entities[world->freeEntityIndex++] = entity;
|
||||||
|
Entity *result = &world->entities[world->freeEntityIndex-1];
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
INTERNAL void addAnim(Entity *entity, v4 *rects, i32 numRects, f32 duration)
|
||||||
|
{
|
||||||
|
|
||||||
|
#ifdef WT_DEBUG
|
||||||
|
ASSERT(rects && numRects >= 0)
|
||||||
|
ASSERT(entity->freeAnimIndex < ARRAY_COUNT(entity->anim));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
EntityAnim result = {0};
|
||||||
|
result.rect = rects;
|
||||||
|
result.numRects = numRects;
|
||||||
|
result.duration = duration;
|
||||||
|
result.currDuration = duration;
|
||||||
|
|
||||||
|
entity->anim[entity->freeAnimIndex++] = result;
|
||||||
|
}
|
||||||
|
|
||||||
void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
||||||
{
|
{
|
||||||
AssetManager *assetManager = &state->assetManager;
|
AssetManager *assetManager = &state->assetManager;
|
||||||
@ -58,63 +97,54 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
ASSERT(worldDimensionInTiles.x * worldDimensionInTiles.y <
|
ASSERT(worldDimensionInTiles.x * worldDimensionInTiles.y <
|
||||||
world->maxEntities);
|
world->maxEntities);
|
||||||
#endif
|
#endif
|
||||||
|
v2 pos = V2(CAST(f32) x * state->tileSize,
|
||||||
world->texType = texlist_terrain;
|
|
||||||
Entity *entity = &world->entities[world->freeEntityIndex++];
|
|
||||||
entity->pos =
|
|
||||||
V2(CAST(f32) x * state->tileSize,
|
|
||||||
CAST(f32) y * state->tileSize);
|
CAST(f32) y * state->tileSize);
|
||||||
entity->dPos = V2(0.0f, 0.0f);
|
v2 size =
|
||||||
entity->size =
|
|
||||||
V2(CAST(f32) state->tileSize, CAST(f32) state->tileSize);
|
V2(CAST(f32) state->tileSize, CAST(f32) state->tileSize);
|
||||||
entity->tex = asset_getTexture(assetManager, world->texType);
|
enum Direction dir = direction_null;
|
||||||
entity->collides = FALSE;
|
Texture *tex = asset_getTexture(assetManager, world->texType);
|
||||||
entity->freeAnimIndex = 0;
|
b32 collides = FALSE;
|
||||||
entity->currAnimIndex = 0;
|
Entity *tile = addEntity(world, pos, size, dir, tex, collides);
|
||||||
|
|
||||||
SpriteAnim worldAnimIdle = {NULL, 1, 0, 1.0f, 1.0f};
|
f32 duration = 1.0f;
|
||||||
worldAnimIdle.rect = (v4 *)calloc(1, sizeof(v4));
|
i32 numRects = 1;
|
||||||
worldAnimIdle.rect[0] = atlas->texRect[terraincoords_ground];
|
v4 *animRects = CAST(v4 *)calloc(numRects, sizeof(v4));
|
||||||
|
animRects[0] = atlas->texRect[terraincoords_ground];
|
||||||
entity->anim[entity->freeAnimIndex++] = worldAnimIdle;
|
addAnim(tile, animRects, numRects, duration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
World *const world = &state->world[state->currWorldIndex];
|
World *const world = &state->world[state->currWorldIndex];
|
||||||
|
|
||||||
/* Init hero */
|
/* Init hero entity */
|
||||||
Entity heroEnt = {V2(0.0f, 0.0f),
|
|
||||||
V2(0.0f, 0.0f),
|
|
||||||
V2(58.0f, 98.0f),
|
|
||||||
direction_east,
|
|
||||||
asset_getTexture(assetManager, texlist_hero),
|
|
||||||
TRUE,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0};
|
|
||||||
|
|
||||||
SpriteAnim heroAnimIdle = {NULL, 1, 0, 1.0f, 1.0f};
|
|
||||||
// TODO(doyle): Get rid of
|
|
||||||
heroAnimIdle.rect = (v4 *)calloc(1, sizeof(v4));
|
|
||||||
heroAnimIdle.rect[0] = V4(746.0f, 1018.0f, 804.0f, 920.0f);
|
|
||||||
heroEnt.anim[heroEnt.freeAnimIndex++] = heroAnimIdle;
|
|
||||||
|
|
||||||
SpriteAnim heroAnimWalk = {NULL, 3, 0, 0.10f, 0.10f};
|
|
||||||
// TODO(doyle): Get rid of
|
|
||||||
heroAnimWalk.rect = (v4 *)calloc(heroAnimWalk.numRects, sizeof(v4));
|
|
||||||
heroAnimWalk.rect[0] = V4(641.0f, 1018.0f, 699.0f, 920.0f);
|
|
||||||
heroAnimWalk.rect[1] = heroAnimIdle.rect[0];
|
|
||||||
heroAnimWalk.rect[2] = V4(849.0f, 1018.0f, 904.0f, 920.0f);
|
|
||||||
heroEnt.anim[heroEnt.freeAnimIndex++] = heroAnimWalk;
|
|
||||||
heroEnt.currAnimIndex = 0;
|
|
||||||
|
|
||||||
world->heroIndex = world->freeEntityIndex;
|
world->heroIndex = world->freeEntityIndex;
|
||||||
world->entities[world->freeEntityIndex++] = heroEnt;
|
|
||||||
Entity *hero = &world->entities[world->heroIndex];
|
v2 pos = V2(0.0f, 0.0f);
|
||||||
|
v2 size = V2(58.0f, 98.0f);
|
||||||
|
enum Direction dir = direction_east;
|
||||||
|
Texture *tex = asset_getTexture(assetManager, texlist_hero);
|
||||||
|
b32 collides = TRUE;
|
||||||
|
Entity *hero = addEntity(world, pos, size, dir, tex, collides);
|
||||||
|
|
||||||
|
/* Add idle animation */
|
||||||
|
f32 duration = 1.0f;
|
||||||
|
i32 numRects = 1;
|
||||||
|
v4 *heroIdleRects = CAST(v4 *) calloc(numRects, sizeof(v4));
|
||||||
|
heroIdleRects[0] = V4(746.0f, 1018.0f, 804.0f, 920.0f);
|
||||||
|
addAnim(hero, heroIdleRects, numRects, duration);
|
||||||
|
|
||||||
|
/* Add walking animation */
|
||||||
|
duration = 0.10f;
|
||||||
|
numRects = 3;
|
||||||
|
v4 *heroWalkRects = CAST(v4 *) calloc(numRects, sizeof(v4));
|
||||||
|
heroWalkRects[0] = V4(641.0f, 1018.0f, 699.0f, 920.0f);
|
||||||
|
heroWalkRects[1] = V4(746.0f, 1018.0f, 804.0f, 920.0f);
|
||||||
|
heroWalkRects[2] = V4(849.0f, 1018.0f, 904.0f, 920.0f);
|
||||||
|
addAnim(hero, heroWalkRects, numRects, duration);
|
||||||
|
|
||||||
Texture *heroSheet = hero->tex;
|
Texture *heroSheet = hero->tex;
|
||||||
v2 sheetSize = V2(CAST(f32)heroSheet->width, CAST(f32)heroSheet->height);
|
v2 sheetSize = V2(CAST(f32) heroSheet->width, CAST(f32) heroSheet->height);
|
||||||
if (sheetSize.x != sheetSize.y)
|
if (sheetSize.x != sheetSize.y)
|
||||||
{
|
{
|
||||||
printf(
|
printf(
|
||||||
@ -124,23 +154,20 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Create a NPC */
|
/* Create a NPC */
|
||||||
SpriteAnim npcAnim = {NULL, 2, 0, 0.3f, 0.3f};
|
pos = V2(300.0f, 300.0f);
|
||||||
// TODO(doyle): Get rid of
|
size = hero->size;
|
||||||
npcAnim.rect = (v4 *)calloc(2, sizeof(v4));
|
dir = direction_null;
|
||||||
npcAnim.rect[0] = V4(944.0f, 918.0f, 1010.0f, 816.0f);
|
tex = hero->tex;
|
||||||
npcAnim.rect[1] = V4(944.0f, 812.0f, 1010.0f, 710.0f);
|
collides = TRUE;
|
||||||
|
Entity *npc = addEntity(world, pos, size, dir, tex, collides);
|
||||||
|
|
||||||
Entity npcEnt = {V2(300.0f, 300.0f),
|
/* Add npc waving animation */
|
||||||
V2(0.0f, 0.0f),
|
duration = 0.30f;
|
||||||
hero->size,
|
numRects = 2;
|
||||||
direction_null,
|
v4 *npcWavingRects = CAST(v4 *) calloc(numRects, sizeof(v4));
|
||||||
hero->tex,
|
npcWavingRects[0] = V4(944.0f, 918.0f, 1010.0f, 816.0f);
|
||||||
TRUE,
|
npcWavingRects[1] = V4(944.0f, 812.0f, 1010.0f, 710.0f);
|
||||||
0,
|
addAnim(npc, npcWavingRects, numRects, duration);
|
||||||
0,
|
|
||||||
0};
|
|
||||||
npcEnt.anim[npcEnt.freeAnimIndex++] = npcAnim;
|
|
||||||
world->entities[world->freeEntityIndex++] = npcEnt;
|
|
||||||
|
|
||||||
/* Init renderer */
|
/* Init renderer */
|
||||||
Renderer *renderer = &state->renderer;
|
Renderer *renderer = &state->renderer;
|
||||||
@ -242,7 +269,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
|
|||||||
// walking
|
// walking
|
||||||
if (hero->currAnimIndex == 1)
|
if (hero->currAnimIndex == 1)
|
||||||
{
|
{
|
||||||
SpriteAnim *currAnim = &hero->anim[hero->currAnimIndex];
|
EntityAnim *currAnim = &hero->anim[hero->currAnimIndex];
|
||||||
currAnim->currDuration = currAnim->duration;
|
currAnim->currDuration = currAnim->duration;
|
||||||
currAnim->currRectIndex = 0;
|
currAnim->currRectIndex = 0;
|
||||||
hero->currAnimIndex = 0;
|
hero->currAnimIndex = 0;
|
||||||
@ -250,7 +277,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
|
|||||||
}
|
}
|
||||||
else if (hero->currAnimIndex == 0)
|
else if (hero->currAnimIndex == 0)
|
||||||
{
|
{
|
||||||
SpriteAnim *currAnim = &hero->anim[hero->currAnimIndex];
|
EntityAnim *currAnim = &hero->anim[hero->currAnimIndex];
|
||||||
currAnim->currDuration = currAnim->duration;
|
currAnim->currDuration = currAnim->duration;
|
||||||
currAnim->currRectIndex = 0;
|
currAnim->currRectIndex = 0;
|
||||||
hero->currAnimIndex = 1;
|
hero->currAnimIndex = 1;
|
||||||
|
@ -14,7 +14,7 @@ enum Direction
|
|||||||
direction_null,
|
direction_null,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct SpriteAnim
|
typedef struct EntityAnim
|
||||||
{
|
{
|
||||||
v4 *rect;
|
v4 *rect;
|
||||||
i32 numRects;
|
i32 numRects;
|
||||||
@ -22,7 +22,7 @@ typedef struct SpriteAnim
|
|||||||
|
|
||||||
f32 duration;
|
f32 duration;
|
||||||
f32 currDuration;
|
f32 currDuration;
|
||||||
} SpriteAnim;
|
} EntityAnim;
|
||||||
|
|
||||||
typedef struct Entity
|
typedef struct Entity
|
||||||
{
|
{
|
||||||
@ -34,7 +34,7 @@ typedef struct Entity
|
|||||||
b32 collides;
|
b32 collides;
|
||||||
|
|
||||||
// TODO(doyle): String based access
|
// TODO(doyle): String based access
|
||||||
SpriteAnim anim[16];
|
EntityAnim anim[16];
|
||||||
i32 freeAnimIndex;
|
i32 freeAnimIndex;
|
||||||
i32 currAnimIndex;
|
i32 currAnimIndex;
|
||||||
} Entity;
|
} Entity;
|
||||||
|
Loading…
Reference in New Issue
Block a user