-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimationPlayer.cpp
68 lines (60 loc) · 1.72 KB
/
AnimationPlayer.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
60
61
62
63
64
65
66
67
68
#include "AnimationPlayer.h"
#include <Gamebuino-Meta.h>
#include <Rectangle.h>
#include <Sprite.h>
#include "Time.h"
void AnimationPlayer::set_animation(const Animation* animation_ptr, const bool reset_frame_position)
{
if (animation_ptr == this->animation_ptr)
return;
if (animation_ptr == nullptr)
{
stop();
}
else
{
this->animation_ptr = animation_ptr;
if (reset_frame_position)
this->start_frame = gametime::framecount;
}
}
inline const uint16_t* AnimationPlayer::get_current_frame() const
{
if (animation_ptr->get_repeat())
{
return animation_ptr->frames[((gametime::framecount - start_frame) >> animation_ptr->get_interval_pow2()) % animation_ptr->get_frame_count()];
}
auto frame = ((gametime::framecount - start_frame) >> animation_ptr->get_interval_pow2());
if (frame >= animation_ptr->get_frame_count())
{
frame = animation_ptr->get_frame_count()-1;
}
return animation_ptr->frames[frame];
}
Rectangle AnimationPlayer::get_cropped_sprite_bounds(const int x, const int y) const
{
if (animation_ptr) // only when player has animation to play
{
return Sprite::get_cropped_sprite_bounds(get_current_frame(), x, y, get_flip());
}
return {0, 0, -1, -1};
}
void AnimationPlayer::to_last_frame()
{
start_frame = 0;
}
bool AnimationPlayer::is_at_end() const
{
return (gametime::framecount - start_frame) >= animation_ptr->get_animation_duration();
}
void AnimationPlayer::stop()
{
animation_ptr = nullptr;
}
void AnimationPlayer::render(const Rectangle& part, const int anchor_x, const int anchor_y, uint16_t* const slice_buffer) const
{
if (animation_ptr) // only when player has animation to play
{
Sprite::render_cropped_sprite_part(get_current_frame(), part, anchor_x, anchor_y, slice_buffer, flags);
}
}