R-Type 0.1
Loading...
Searching...
No Matches
AnimatorSystem.hpp
1#pragma once
2
3#include "components/Animation.hpp"
4#include "components/Animator.hpp"
5#include <ISystem.hpp>
6
7namespace rtk::systems {
16class AnimatorSystem final : public rtk::ecs::ISystem {
17public:
18 AnimatorSystem() = default;
19 ~AnimatorSystem() = default;
20
24 void onStart(rtk::ecs::Registry &) override {}
25
31 void update(rtk::ecs::Registry &reg, float dt) override {
32 auto view = reg.view<component::animator, component::Animation>();
33
34 auto &animators = reg.get_components<component::animator>();
35 auto &animations = reg.get_components<component::Animation>();
36
37 for (auto entity : view) {
38 auto *anim = animations.get(entity);
39 auto *animator = animators.get(entity);
40
41 if (!anim || !animator)
42 continue;
43
44 auto stateIdx = static_cast<std::size_t>(animator->currentState);
45 if (stateIdx < animator->clips.size() &&
46 animator->currentState != animator->previousState) {
47 const auto &currentClip = animator->clips[stateIdx];
48
49 anim->startX = currentClip.startX;
50 anim->startY = currentClip.startY;
51 anim->totalFrames = currentClip.totalFrames;
52 anim->frameDuration = currentClip.frameDuration;
53 anim->loop = currentClip.loop;
54 anim->reverse = currentClip.reverse;
55 if (currentClip.frameWidth > 0) {
56 anim->frameWidth = currentClip.frameWidth;
57 }
58 if (currentClip.frameHeight > 0) {
59 anim->frameHeight = currentClip.frameHeight;
60 }
61 anim->currentFrame = 0;
62 anim->elapsedTime = 0.0f;
63 anim->playing = true;
64 animator->previousState = animator->currentState;
65 }
66 }
67 }
68
72 void onStop(rtk::ecs::Registry &) override {}
73};
74
76} // namespace rtk::systems
Loads the clip of the current animator state into the Animation.
Definition AnimatorSystem.hpp:16
void onStart(rtk::ecs::Registry &) override
Does nothing.
Definition AnimatorSystem.hpp:24
void update(rtk::ecs::Registry &reg, float dt) override
Restarts the animation with a new clip when the state changed.
Definition AnimatorSystem.hpp:31
void onStop(rtk::ecs::Registry &) override
Does nothing.
Definition AnimatorSystem.hpp:72
Sprite sheet animation played by changing the texture rectangle.
Definition Animation.hpp:17
State machine that picks the Animation clip of an entity.
Definition Animator.hpp:45