ddnet/src/game/client/animstate.cpp

98 lines
2.7 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include <base/math.h>
#include <game/generated/client_data.h>
#include <game/generated/protocol.h>
2010-05-29 07:25:38 +00:00
#include "animstate.h"
2011-06-01 18:19:12 +00:00
static void AnimSeqEval(CAnimSequence *pSeq, float Time, CAnimKeyframe *pFrame)
{
2010-05-29 07:25:38 +00:00
if(pSeq->m_NumFrames == 0)
{
2010-05-29 07:25:38 +00:00
pFrame->m_Time = 0;
pFrame->m_X = 0;
pFrame->m_Y = 0;
pFrame->m_Angle = 0;
}
2010-05-29 07:25:38 +00:00
else if(pSeq->m_NumFrames == 1)
{
2010-05-29 07:25:38 +00:00
*pFrame = pSeq->m_aFrames[0];
}
else
{
2019-04-26 19:36:49 +00:00
//time = maximum(0.0f, minimum(1.0f, time / duration)); // TODO: use clamp
2011-06-01 18:19:12 +00:00
CAnimKeyframe *pFrame1 = 0;
CAnimKeyframe *pFrame2 = 0;
2010-05-29 07:25:38 +00:00
float Blend = 0.0f;
// TODO: make this smarter.. binary search
for(int i = 1; i < pSeq->m_NumFrames; i++)
{
if(pSeq->m_aFrames[i - 1].m_Time <= Time && pSeq->m_aFrames[i].m_Time >= Time)
{
pFrame1 = &pSeq->m_aFrames[i - 1];
2010-05-29 07:25:38 +00:00
pFrame2 = &pSeq->m_aFrames[i];
Blend = (Time - pFrame1->m_Time) / (pFrame2->m_Time - pFrame1->m_Time);
break;
}
}
if(pFrame1 && pFrame2)
{
2010-05-29 07:25:38 +00:00
pFrame->m_Time = Time;
pFrame->m_X = mix(pFrame1->m_X, pFrame2->m_X, Blend);
pFrame->m_Y = mix(pFrame1->m_Y, pFrame2->m_Y, Blend);
pFrame->m_Angle = mix(pFrame1->m_Angle, pFrame2->m_Angle, Blend);
}
}
}
2011-06-01 18:19:12 +00:00
static void AnimAddKeyframe(CAnimKeyframe *pSeq, CAnimKeyframe *pAdded, float Amount)
{
// AnimSeqEval fills m_X for any case, clang-analyzer assumes going into the
// final else branch with pSeq->m_NumFrames < 2, which is impossible.
pSeq->m_X += pAdded->m_X * Amount; // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult)
pSeq->m_Y += pAdded->m_Y * Amount;
pSeq->m_Angle += pAdded->m_Angle * Amount;
}
2010-05-29 07:25:38 +00:00
static void AnimAdd(CAnimState *pState, CAnimState *pAdded, float Amount)
{
2010-05-29 07:25:38 +00:00
AnimAddKeyframe(pState->GetBody(), pAdded->GetBody(), Amount);
AnimAddKeyframe(pState->GetBackFoot(), pAdded->GetBackFoot(), Amount);
AnimAddKeyframe(pState->GetFrontFoot(), pAdded->GetFrontFoot(), Amount);
AnimAddKeyframe(pState->GetAttach(), pAdded->GetAttach(), Amount);
}
2011-06-01 18:19:12 +00:00
void CAnimState::Set(CAnimation *pAnim, float Time)
{
2010-05-29 07:25:38 +00:00
AnimSeqEval(&pAnim->m_Body, Time, &m_Body);
AnimSeqEval(&pAnim->m_BackFoot, Time, &m_BackFoot);
AnimSeqEval(&pAnim->m_FrontFoot, Time, &m_FrontFoot);
AnimSeqEval(&pAnim->m_Attach, Time, &m_Attach);
}
2011-06-01 18:19:12 +00:00
void CAnimState::Add(CAnimation *pAnim, float Time, float Amount)
{
2010-05-29 07:25:38 +00:00
CAnimState Add;
Add.Set(pAnim, Time);
AnimAdd(this, &Add, Amount);
}
2010-05-29 07:25:38 +00:00
CAnimState *CAnimState::GetIdle()
{
2010-05-29 07:25:38 +00:00
static CAnimState State;
static bool Init = true;
2010-05-29 07:25:38 +00:00
if(Init)
{
2010-05-29 07:25:38 +00:00
State.Set(&g_pData->m_aAnimations[ANIM_BASE], 0);
State.Add(&g_pData->m_aAnimations[ANIM_IDLE], 0, 1.0f);
Init = false;
}
2010-05-29 07:25:38 +00:00
return &State;
}