Switch animation indexing to enum, toggle anim on spacebar
This commit is contained in:
parent
ca6790cb1c
commit
62c5fe0629
@ -10,6 +10,12 @@ enum State
|
|||||||
state_win,
|
state_win,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
INTERNAL void attackEntity(Entity *attacker, Entity *defender)
|
||||||
|
{
|
||||||
|
// TODO(doyle): Use attacker stats in battle equations
|
||||||
|
defender->stats->health--;
|
||||||
|
}
|
||||||
|
|
||||||
INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, enum EntityType type,
|
INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, enum EntityType type,
|
||||||
enum Direction direction, Texture *tex, b32 collides)
|
enum Direction direction, Texture *tex, b32 collides)
|
||||||
{
|
{
|
||||||
@ -52,12 +58,13 @@ INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, enum EntityType type,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL void addAnim(Entity *entity, v4 *rects, i32 numRects, f32 duration)
|
INTERNAL void addAnim(Entity *entity, enum EntityAnimId animId, v4 *rects,
|
||||||
|
i32 numRects, f32 duration)
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef DENGINE_DEBUG
|
#ifdef DENGINE_DEBUG
|
||||||
ASSERT(rects && numRects >= 0)
|
ASSERT(rects && numRects >= 0)
|
||||||
ASSERT(entity->freeAnimIndex < ARRAY_COUNT(entity->anim));
|
ASSERT(animId < entityanimid_count);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
EntityAnim result = {0};
|
EntityAnim result = {0};
|
||||||
@ -66,7 +73,7 @@ INTERNAL void addAnim(Entity *entity, v4 *rects, i32 numRects, f32 duration)
|
|||||||
result.duration = duration;
|
result.duration = duration;
|
||||||
result.currDuration = duration;
|
result.currDuration = duration;
|
||||||
|
|
||||||
entity->anim[entity->freeAnimIndex++] = result;
|
entity->anim[animId] = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL void rendererInit(GameState *state, v2i windowSize)
|
INTERNAL void rendererInit(GameState *state, v2i windowSize)
|
||||||
@ -202,7 +209,7 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
i32 numRects = 1;
|
i32 numRects = 1;
|
||||||
v4 *animRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
v4 *animRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
||||||
animRects[0] = atlas->texRect[terraincoords_ground];
|
animRects[0] = atlas->texRect[terraincoords_ground];
|
||||||
addAnim(tile, animRects, numRects, duration);
|
addAnim(tile, entityanimid_idle, animRects, numRects, duration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -227,7 +234,8 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
i32 numRects = 1;
|
i32 numRects = 1;
|
||||||
v4 *heroIdleRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
v4 *heroIdleRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
||||||
heroIdleRects[0] = heroAtlas->texRect[herocoords_idle];
|
heroIdleRects[0] = heroAtlas->texRect[herocoords_idle];
|
||||||
addAnim(hero, heroIdleRects, numRects, duration);
|
addAnim(hero, entityanimid_idle, heroIdleRects, numRects, duration);
|
||||||
|
hero->currAnimIndex = entityanimid_idle;
|
||||||
|
|
||||||
/* Add walking animation */
|
/* Add walking animation */
|
||||||
duration = 0.10f;
|
duration = 0.10f;
|
||||||
@ -236,7 +244,15 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
heroWalkRects[0] = heroAtlas->texRect[herocoords_walkA];
|
heroWalkRects[0] = heroAtlas->texRect[herocoords_walkA];
|
||||||
heroWalkRects[1] = heroAtlas->texRect[herocoords_idle];
|
heroWalkRects[1] = heroAtlas->texRect[herocoords_idle];
|
||||||
heroWalkRects[2] = heroAtlas->texRect[herocoords_walkB];
|
heroWalkRects[2] = heroAtlas->texRect[herocoords_walkB];
|
||||||
addAnim(hero, heroWalkRects, numRects, duration);
|
addAnim(hero, entityanimid_walk, heroWalkRects, numRects, duration);
|
||||||
|
|
||||||
|
/* Add hero waving animation */
|
||||||
|
duration = 0.30f;
|
||||||
|
numRects = 2;
|
||||||
|
v4 *heroWaveRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
||||||
|
heroWaveRects[0] = heroAtlas->texRect[herocoords_waveA];
|
||||||
|
heroWaveRects[1] = heroAtlas->texRect[herocoords_waveB];
|
||||||
|
addAnim(hero, entityanimid_wave, heroWaveRects, numRects, duration);
|
||||||
|
|
||||||
/* Create a NPC */
|
/* Create a NPC */
|
||||||
pos = V2(hero->pos.x * 3, CAST(f32) state->tileSize);
|
pos = V2(hero->pos.x * 3, CAST(f32) state->tileSize);
|
||||||
@ -248,12 +264,13 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
Entity *npc = addEntity(world, pos, size, type, dir, tex, collides);
|
Entity *npc = addEntity(world, pos, size, type, dir, tex, collides);
|
||||||
|
|
||||||
/* Add npc waving animation */
|
/* Add npc waving animation */
|
||||||
duration = 0.30f;
|
duration = 0.30f;
|
||||||
numRects = 2;
|
numRects = 2;
|
||||||
v4 *npcWavingRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
v4 *npcWavingRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
||||||
npcWavingRects[0] = heroAtlas->texRect[herocoords_waveA];
|
npcWavingRects[0] = heroAtlas->texRect[herocoords_waveA];
|
||||||
npcWavingRects[1] = heroAtlas->texRect[herocoords_waveB];
|
npcWavingRects[1] = heroAtlas->texRect[herocoords_waveB];
|
||||||
addAnim(npc, npcWavingRects, numRects, duration);
|
addAnim(npc, entityanimid_wave, npcWavingRects, numRects, duration);
|
||||||
|
npc->currAnimIndex = entityanimid_wave;
|
||||||
|
|
||||||
/* Create a Mob */
|
/* Create a Mob */
|
||||||
pos = V2(renderer->size.w - (renderer->size.w / 3.0f),
|
pos = V2(renderer->size.w - (renderer->size.w / 3.0f),
|
||||||
@ -270,7 +287,8 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
numRects = 1;
|
numRects = 1;
|
||||||
v4 *mobIdleRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
v4 *mobIdleRects = PLATFORM_MEM_ALLOC(numRects, v4);
|
||||||
mobIdleRects[0] = heroIdleRects[0];
|
mobIdleRects[0] = heroIdleRects[0];
|
||||||
addAnim(mob, mobIdleRects, numRects, duration);
|
addAnim(mob, entityanimid_idle, mobIdleRects, numRects, duration);
|
||||||
|
mob->currAnimIndex = entityanimid_idle;
|
||||||
|
|
||||||
/* Add mob walking animation */
|
/* Add mob walking animation */
|
||||||
duration = 0.10f;
|
duration = 0.10f;
|
||||||
@ -279,8 +297,14 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
|
|||||||
mobWalkRects[0] = heroWalkRects[0];
|
mobWalkRects[0] = heroWalkRects[0];
|
||||||
mobWalkRects[1] = heroWalkRects[1];
|
mobWalkRects[1] = heroWalkRects[1];
|
||||||
mobWalkRects[2] = heroWalkRects[2];
|
mobWalkRects[2] = heroWalkRects[2];
|
||||||
addAnim(mob, mobWalkRects, numRects, duration);
|
addAnim(mob, entityanimid_walk, mobWalkRects, numRects, duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
INTERNAL inline void resetActiveEntityAnim(Entity *entity)
|
||||||
|
{
|
||||||
|
EntityAnim *currAnim = &entity->anim[entity->currAnimIndex];
|
||||||
|
currAnim->currDuration = currAnim->duration;
|
||||||
|
currAnim->currRectIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL void parseInput(GameState *state, const f32 dt)
|
INTERNAL void parseInput(GameState *state, const f32 dt)
|
||||||
@ -298,12 +322,12 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
World *const world = &state->world[state->currWorldIndex];
|
World *const world = &state->world[state->currWorldIndex];
|
||||||
Entity *hero = &world->entities[world->heroIndex];
|
Entity *hero = &world->entities[world->heroIndex];
|
||||||
v2 ddPos = V2(0, 0);
|
v2 ddPos = V2(0, 0);
|
||||||
|
|
||||||
if (state->keys[GLFW_KEY_SPACE])
|
// TODO(doyle): As we need to handle more key spam input, we want to track
|
||||||
{
|
// if a button ended down
|
||||||
}
|
LOCAL_PERSIST b32 spaceBarWasDown = FALSE;
|
||||||
|
|
||||||
if (state->keys[GLFW_KEY_RIGHT])
|
if (state->keys[GLFW_KEY_RIGHT])
|
||||||
{
|
{
|
||||||
@ -320,11 +344,30 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
|
|||||||
{
|
{
|
||||||
ddPos.y = 1.0f;
|
ddPos.y = 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state->keys[GLFW_KEY_DOWN])
|
if (state->keys[GLFW_KEY_DOWN])
|
||||||
{
|
{
|
||||||
ddPos.y = -1.0f;
|
ddPos.y = -1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (state->keys[GLFW_KEY_SPACE] && !spaceBarWasDown)
|
||||||
|
{
|
||||||
|
spaceBarWasDown = TRUE;
|
||||||
|
resetActiveEntityAnim(hero);
|
||||||
|
if (hero->currAnimIndex == entityanimid_idle)
|
||||||
|
{
|
||||||
|
hero->currAnimIndex = entityanimid_wave;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hero->currAnimIndex = entityanimid_idle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!state->keys[GLFW_KEY_SPACE])
|
||||||
|
{
|
||||||
|
spaceBarWasDown = FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
if (ddPos.x != 0.0f && ddPos.y != 0.0f)
|
if (ddPos.x != 0.0f && ddPos.y != 0.0f)
|
||||||
{
|
{
|
||||||
// NOTE(doyle): Cheese it and pre-compute the vector for diagonal using
|
// NOTE(doyle): Cheese it and pre-compute the vector for diagonal using
|
||||||
@ -337,26 +380,20 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
|
|||||||
f32 epsilon = 15.0f;
|
f32 epsilon = 15.0f;
|
||||||
v2 epsilonDpos = v2_sub(V2(epsilon, epsilon),
|
v2 epsilonDpos = v2_sub(V2(epsilon, epsilon),
|
||||||
V2(ABS(hero->dPos.x), ABS(hero->dPos.y)));
|
V2(ABS(hero->dPos.x), ABS(hero->dPos.y)));
|
||||||
|
|
||||||
if (epsilonDpos.x >= 0.0f && epsilonDpos.y >= 0.0f)
|
if (epsilonDpos.x >= 0.0f && epsilonDpos.y >= 0.0f)
|
||||||
{
|
{
|
||||||
hero->dPos = V2(0.0f, 0.0f);
|
hero->dPos = V2(0.0f, 0.0f);
|
||||||
// TODO(doyle): Change index to use some meaningful name like a string
|
if (hero->currAnimIndex == entityanimid_walk)
|
||||||
// or enum for referencing animations, in this case 0 is idle and 1 is
|
|
||||||
// walking
|
|
||||||
if (hero->currAnimIndex == 1)
|
|
||||||
{
|
{
|
||||||
EntityAnim *currAnim = &hero->anim[hero->currAnimIndex];
|
resetActiveEntityAnim(hero);
|
||||||
currAnim->currDuration = currAnim->duration;
|
hero->currAnimIndex = entityanimid_idle;
|
||||||
currAnim->currRectIndex = 0;
|
|
||||||
hero->currAnimIndex = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (hero->currAnimIndex == 0)
|
else if (hero->currAnimIndex == entityanimid_idle)
|
||||||
{
|
{
|
||||||
EntityAnim *currAnim = &hero->anim[hero->currAnimIndex];
|
resetActiveEntityAnim(hero);
|
||||||
currAnim->currDuration = currAnim->duration;
|
hero->currAnimIndex = entityanimid_walk;
|
||||||
currAnim->currRectIndex = 0;
|
|
||||||
hero->currAnimIndex = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
f32 heroSpeed = CAST(f32)(22.0f * METERS_TO_PIXEL); // m/s^2
|
f32 heroSpeed = CAST(f32)(22.0f * METERS_TO_PIXEL); // m/s^2
|
||||||
@ -513,23 +550,32 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
|
|||||||
|
|
||||||
RenderTex renderTex = {emptyTex, V4(0, 1, 1, 0)};
|
RenderTex renderTex = {emptyTex, V4(0, 1, 1, 0)};
|
||||||
renderer_rect(renderer, cameraBounds, heroCenter,
|
renderer_rect(renderer, cameraBounds, heroCenter,
|
||||||
V2(distance, 10.0f), 0, renderTex,
|
V2(distance, 5.0f), 0, renderTex,
|
||||||
V4(1, 0, 0, 0.5f));
|
V4(1, 0, 0, 0.25f));
|
||||||
|
|
||||||
/* Update action timer */
|
/* Update action timer */
|
||||||
#ifdef DENGINE_DEBUG
|
#ifdef DENGINE_DEBUG
|
||||||
ASSERT(entity->stats)
|
ASSERT(entity->stats)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
hero->stats->actionTimer -= dt * hero->stats->actionSpdMul;
|
hero->stats->actionTimer -= dt * hero->stats->actionSpdMul;
|
||||||
entity->stats->actionTimer -= dt * entity->stats->actionSpdMul;
|
entity->stats->actionTimer -= dt * entity->stats->actionSpdMul;
|
||||||
if (hero->stats->actionTimer <= 0)
|
|
||||||
|
if (hero->stats->health > 0)
|
||||||
{
|
{
|
||||||
hero->stats->actionTimer = hero->stats->actionRate;
|
if (hero->stats->actionTimer <= 0)
|
||||||
|
{
|
||||||
|
//attackEntity(hero, entity);
|
||||||
|
hero->stats->actionTimer = hero->stats->actionRate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entity->stats->actionTimer <= 0)
|
if (entity->stats->health > 0)
|
||||||
{
|
{
|
||||||
entity->stats->actionTimer = entity->stats->actionRate;
|
if (entity->stats->actionTimer <= 0)
|
||||||
|
{
|
||||||
|
entity->stats->actionTimer = entity->stats->actionRate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -25,6 +25,14 @@ enum EntityType
|
|||||||
entitytype_count,
|
entitytype_count,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum EntityAnimId
|
||||||
|
{
|
||||||
|
entityanimid_idle,
|
||||||
|
entityanimid_walk,
|
||||||
|
entityanimid_wave,
|
||||||
|
entityanimid_count,
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct EntityAnim
|
typedef struct EntityAnim
|
||||||
{
|
{
|
||||||
v4 *rect;
|
v4 *rect;
|
||||||
@ -59,7 +67,6 @@ typedef struct Entity
|
|||||||
|
|
||||||
// TODO(doyle): String based access
|
// TODO(doyle): String based access
|
||||||
EntityAnim anim[16];
|
EntityAnim anim[16];
|
||||||
i32 freeAnimIndex;
|
|
||||||
i32 currAnimIndex;
|
i32 currAnimIndex;
|
||||||
|
|
||||||
EntityStats *stats;
|
EntityStats *stats;
|
||||||
|
Loading…
Reference in New Issue
Block a user