Change Animation names, stop input parse on attack

This commit is contained in:
Doyle Thai 2016-07-19 13:32:31 +10:00
parent b8b76cecd3
commit 403999b566
5 changed files with 73 additions and 68 deletions

View File

@ -414,22 +414,19 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
}
void asset_addAnimation(AssetManager *assetManager, i32 texId,
i32 animId, i32 *atlasIndexes, i32 numFrames,
i32 animId, i32 *frameIndex, i32 numFrames,
f32 frameDuration)
{
#ifdef DENGINE_DEBUG
ASSERT(assetManager && atlasIndexes)
ASSERT(!assetManager->anims[animId].atlasIndexes);
ASSERT(assetManager && frameIndex)
ASSERT(!assetManager->anims[animId].frameIndex);
#endif
Animation anim = {0};
// TODO(doyle): Do we need texture to be bound to animation?
anim.tex = asset_getTexture(assetManager, texId);
anim.atlas = asset_getTextureAtlas(assetManager, texId);
anim.atlasIndexes = PLATFORM_MEM_ALLOC(numFrames, i32);
for (i32 i = 0; i < numFrames; i++)
anim.atlasIndexes[i] = atlasIndexes[i];
anim.frameIndex = PLATFORM_MEM_ALLOC(numFrames, i32);
for (i32 i = 0; i < numFrames; i++) anim.frameIndex[i] = frameIndex[i];
anim.numFrames = numFrames;
anim.frameDuration = frameDuration;

View File

@ -229,8 +229,8 @@ void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity,
{
EntityAnim_ *entityAnim = &entity->anim[entity->currAnimId];
Animation *anim = entityAnim->anim;
i32 atlasIndex = anim->atlasIndexes[entityAnim->currFrame];
v4 animTexRect = anim->atlas->texRect[atlasIndex];
i32 frameIndex = anim->frameIndex[entityAnim->currFrame];
v4 animTexRect = anim->atlas->texRect[frameIndex];
if (entity->direction == direction_east)
{

View File

@ -320,6 +320,12 @@ INTERNAL inline void setActiveEntityAnim(Entity *entity,
entity->currAnimCyclesCompleted = 0;
}
INTERNAL inline v4 getEntityScreenRect(Entity entity)
{
v4 result = math_getRect(entity.pos, entity.hitboxSize);
return result;
}
INTERNAL void parseInput(GameState *state, const f32 dt)
{
/*
@ -338,55 +344,60 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
Entity *hero = &world->entities[world->heroIndex];
v2 ddPos = V2(0, 0);
// 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 (hero->stats->busyDuration <= 0)
{
ddPos.x = 1.0f;
hero->direction = direction_east;
}
// 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_LEFT])
{
ddPos.x = -1.0f;
hero->direction = direction_west;
}
if (state->keys[GLFW_KEY_UP])
{
ddPos.y = 1.0f;
}
if (state->keys[GLFW_KEY_DOWN])
{
ddPos.y = -1.0f;
}
if (ddPos.x != 0.0f && ddPos.y != 0.0f)
{
// NOTE(doyle): Cheese it and pre-compute the vector for diagonal using
// pythagoras theorem on a unit triangle
// 1^2 + 1^2 = c^2
ddPos = v2_scale(ddPos, 0.70710678118f);
}
// TODO(doyle): Revisit key input with state checking for last ended down
#if 0
if (state->keys[GLFW_KEY_SPACE] && !spaceBarWasDown)
{
if (!(hero->currAnimId == entityanimid_tackle &&
hero->currAnimCyclesCompleted == 0))
if (state->keys[GLFW_KEY_RIGHT])
{
spaceBarWasDown = TRUE;
ddPos.x = 1.0f;
hero->direction = direction_east;
}
if (state->keys[GLFW_KEY_LEFT])
{
ddPos.x = -1.0f;
hero->direction = direction_west;
}
if (state->keys[GLFW_KEY_UP])
{
ddPos.y = 1.0f;
}
if (state->keys[GLFW_KEY_DOWN])
{
ddPos.y = -1.0f;
}
if (ddPos.x != 0.0f && ddPos.y != 0.0f)
{
// NOTE(doyle): Cheese it and pre-compute the vector for diagonal
// using
// pythagoras theorem on a unit triangle
// 1^2 + 1^2 = c^2
ddPos = v2_scale(ddPos, 0.70710678118f);
}
// TODO(doyle): Revisit key input with state checking for last ended down
#if 0
if (state->keys[GLFW_KEY_SPACE] && !spaceBarWasDown)
{
if (!(hero->currAnimId == entityanimid_tackle &&
hero->currAnimCyclesCompleted == 0))
{
spaceBarWasDown = TRUE;
}
}
else if (!state->keys[GLFW_KEY_SPACE])
{
spaceBarWasDown = FALSE;
}
}
else if (!state->keys[GLFW_KEY_SPACE])
{
spaceBarWasDown = FALSE;
}
#endif
}
// NOTE(doyle): Clipping threshold for snapping velocity to 0
f32 epsilon = 0.5f;
@ -476,8 +487,8 @@ INTERNAL void updateEntityAnim(Entity *entity, f32 dt)
// TODO(doyle): Recheck why we have this twice
EntityAnim_ *entityAnim = &entity->anim[entity->currAnimId];
Animation anim = *entityAnim->anim;
i32 atlasIndex = anim.atlasIndexes[entityAnim->currFrame];
v4 texRect = anim.atlas->texRect[atlasIndex];
i32 frameIndex = anim.frameIndex[entityAnim->currFrame];
v4 texRect = anim.atlas->texRect[frameIndex];
entityAnim->currDuration -= dt;
if (entityAnim->currDuration <= 0.0f)
@ -485,10 +496,10 @@ INTERNAL void updateEntityAnim(Entity *entity, f32 dt)
if (++entityAnim->currFrame >= anim.numFrames)
entity->currAnimCyclesCompleted++;
entityAnim->currFrame = entityAnim->currFrame % anim.numFrames;
atlasIndex = entityAnim->anim->atlasIndexes[entityAnim->currFrame];
texRect = anim.atlas->texRect[atlasIndex];
entityAnim->currDuration = anim.frameDuration;
entityAnim->currFrame = entityAnim->currFrame % anim.numFrames;
frameIndex = entityAnim->anim->frameIndex[entityAnim->currFrame];
texRect = anim.atlas->texRect[frameIndex];
entityAnim->currDuration = anim.frameDuration;
}
// NOTE(doyle): If humanoid entity, let animation dictate render size which

View File

@ -60,9 +60,8 @@ typedef struct TexAtlas
typedef struct Animation
{
Texture *tex;
TexAtlas *atlas;
i32 *atlasIndexes;
i32 *frameIndex;
i32 numFrames;
f32 frameDuration;
} Animation;

View File

@ -78,6 +78,10 @@ typedef struct Entity
b32 collides;
// TODO(doyle): String based access
// TODO(doyle): Separate animation refs from curr animation state, entity
// only ever has one active current animation. ATM every entity animation
// has a currframe and duration .. either that or we stop resetting
// animation on entity context switch
EntityAnim_ anim[16];
i32 currAnimId;
u32 currAnimCyclesCompleted;
@ -85,10 +89,4 @@ typedef struct Entity
EntityStats *stats;
} Entity;
INTERNAL inline v4 getEntityScreenRect(Entity entity)
{
v4 result = math_getRect(entity.pos, entity.hitboxSize);
return result;
}
#endif