-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationHandler.cpp
59 lines (47 loc) · 1.5 KB
/
AnimationHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "AnimationHandler.h"
#include <iostream>
void AnimationHandler::AddAnim(Animation &anim)
{
m_Animations.push_back(anim);
}
int AnimationHandler::Update(const float dt)
{
if ((m_CurrentAnim >= m_Animations.size()) || (m_CurrentAnim < 0))
{
return m_CurrentFrame;
}
float duration = m_Animations[m_CurrentAnim].Duration;
// Stop at the last frame
if (m_CurrentFrame > m_Animations[m_CurrentAnim].GetLength())
{
return m_CurrentFrame;
}
// Check if the anim has progressed to a new frame and if so, change the next frame
if (int((m_T + dt) / duration) > int(m_T / duration))
{
// Calculate the frame number
m_CurrentFrame = int((m_T + dt) / duration);
// Adjust for looping
m_CurrentFrame %= m_Animations[m_CurrentAnim].GetLength();
std::cout << "CurrentAnim: " << m_CurrentAnim << " CurrentFrame: " << m_CurrentFrame << " m_T: " << m_T << "\n";
}
// Increment the time elapsed
m_T += dt;
// Adjust for looping
if (m_T > duration * m_Animations[m_CurrentAnim].GetLength())
{
m_T = 0.0f;
}
return m_CurrentFrame;
}
void AnimationHandler::ChangeAnim(unsigned int animNum)
{
// Do not change the animation if the animation is currently active or the new animation does not exist.
if ((m_CurrentAnim == animNum) || (animNum >= m_Animations.size()) || (animNum < 0))
{
return;
}
// Set the current animation
m_CurrentAnim = animNum;
m_T = 0.0f;
}