-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.cpp
More file actions
53 lines (46 loc) · 1.36 KB
/
Animation.cpp
File metadata and controls
53 lines (46 loc) · 1.36 KB
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
#include "precomp.h"
#include "tmpl8math.h"
#include "game.h"
#include "sprite.h"
#include "Animation.h"
void Animation::Init(Game* context, int begin, int length, float fps, Playback playback) noexcept {
m_game = context;
m_length = length;
m_fps = fps;
m_begin = begin;
m_index = 0;
m_playback = playback;
}
void Animation::Start() {
m_index = 0;
m_timer.reset();
}
void Animation::Start(float addtime) {
m_timer.reset(static_cast<double>(addtime));
}
Sprite* Animation::GetCurrent() {
return m_game->m_sheet.GetSpriteWithID(GetCurrentID());
}
int Animation::GetCurrentIDUnscoped() const {
return m_begin + static_cast<int>(m_timer.elapsed() * static_cast<double>(m_fps));
}
int Animation::GetCurrentID() {
switch (m_playback) {
case Playback::Pingpong:
m_index = pingpong(static_cast<int>(m_timer.elapsed() * m_fps), m_length);
break;
case Playback::Repeat:
m_index = static_cast<int>(repeat(m_timer.elapsed() * m_fps, static_cast<float>(m_length)));
break;
case Playback::Clamp:
m_index = static_cast<int>(clamp(m_timer.elapsed() * m_fps, 0.0f, static_cast<float>(m_length)));
break;
default:
THROW("Unknown animation playback type\n");
}
return m_begin + m_index;
}
bool Animation::IsOver() const {
ASSERT_VAR(m_fps, m_fps > 10e-2, "Animation FPS must be larger than 0.01\n");
return m_timer.elapsed() >= (TO_FLOAT(m_length) / m_fps);
}