-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaniamtion.cpp
56 lines (45 loc) · 1.06 KB
/
aniamtion.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
# include "animation.h"
# include "util.h"
void Animation::reset() {
timer = 0;
idx_frame = 0;
}
void Animation::set_atlas(Atlas* new_atlas) {
reset();
atlas = new_atlas;
}
void Animation::set_loop(bool flag) {
is_loop = flag;
}
void Animation::set_interval(int ms) {
interval = ms;
}
int Animation::get_idx_frame() {
return idx_frame;
}
IMAGE* Animation::get_frame() {
return atlas->get_image(idx_frame);
}
bool Animation::check_finished() {
if (is_loop) return false;
return (idx_frame == atlas->get_size() - 1);
}
void Animation::on_update(int delta){
timer += delta;
if (timer >= interval) {
timer = 0;
idx_frame++;
if (idx_frame >= atlas->get_size()-1) {
idx_frame = is_loop ? 0 : atlas->get_size() - 1;
if (!is_loop && callback) {
callback();
}
}
}
}
void Animation::on_draw(const Camera& camera, int x, int y) const {
ut::putimage_alpha(camera, x, y, atlas->get_image(idx_frame));
}
void Animation::set_callback(std::function<void()> callback){
this->callback = callback;
}