le2d v0.4.3
2D game engine framework
 
Loading...
Searching...
No Matches
animator.hpp
Go to the documentation of this file.
1#pragma once
4
5namespace le {
6namespace anim {
8template <typename PayloadT, typename SamplerT = SamplerFloor<PayloadT>>
9class Animator {
10 public:
11 using Payload = PayloadT;
14
15 [[nodiscard]] auto has_animation() const -> bool { return get_animation() != nullptr; }
16
17 [[nodiscard]] auto get_animation() const -> AnimationT const* { return m_animation; }
18
19 void set_animation(AnimationT const* animation) {
20 m_animation = animation;
21 elapsed = {};
22 if (animation != nullptr) {
23 repeat = animation->repeat;
24 update_payload();
25 }
26 }
27
28 [[nodiscard]] auto get_duration() const -> kvf::Seconds { return has_animation() ? m_animation->get_timeline().duration : 0s; }
29
30 [[nodiscard]] auto get_progress() const -> float {
31 auto const duration = get_duration();
32 if (duration == 0s) { return 0.0f; }
33 return elapsed / duration;
34 }
35
36 void tick(kvf::Seconds dt) {
37 if (!has_animation()) { return; }
38 auto const duration = get_duration();
39 if (!repeat && elapsed > duration) { return; }
40 elapsed += dt;
41 if (repeat && elapsed > duration) { elapsed = {}; }
42 update_payload();
43 }
44
45 [[nodiscard]] auto get_payload() const -> Payload const& { return m_payload; }
46
47 kvf::Seconds elapsed{};
48 bool repeat{true};
49
50 private:
51 void update_payload() { m_payload = SamplerT{}.sample(m_animation->get_timeline().keyframes, elapsed); }
52
53 AnimationT const* m_animation{};
54 Payload m_payload{};
55};
56} // namespace anim
57
60} // namespace le
Class template for Animation types.
Definition animation.hpp:12
bool repeat
Definition animation.hpp:26
auto get_timeline() const -> Timeline< Payload > const &
Definition animation.hpp:16
Class template for Animator types.
Definition animator.hpp:9
bool repeat
Definition animator.hpp:48
SamplerT Sampler
Definition animator.hpp:12
auto get_progress() const -> float
Definition animator.hpp:30
Animation< PayloadT > AnimationT
Definition animator.hpp:13
void tick(kvf::Seconds dt)
Definition animator.hpp:36
void set_animation(AnimationT const *animation)
Definition animator.hpp:19
auto get_duration() const -> kvf::Seconds
Definition animator.hpp:28
kvf::Seconds elapsed
Definition animator.hpp:47
PayloadT Payload
Definition animator.hpp:11
auto get_payload() const -> Payload const &
Definition animator.hpp:45
auto has_animation() const -> bool
Definition animator.hpp:15
auto get_animation() const -> AnimationT const *
Definition animator.hpp:17
Concept for Animation Sampler.
Definition sampler.hpp:12
Definition texture.hpp:9
Definition animation.hpp:8