Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_animation_controller_2d.hpp
1#pragma once
2
3#include <optional>
4#include <stdexcept>
5#include <string>
6#include <unordered_map>
7
8#include "structure/engine/2d/spk_animation_2d.hpp"
9#include "structure/engine/2d/spk_component_2d.hpp"
10#include "structure/engine/2d/spk_sprite_renderer_2d.hpp"
11#include "structure/system/time/spk_timer.hpp"
12#include "utils/spk_string_utils.hpp"
13
14namespace spk
15{
21 {
22 private:
23 struct AnimationSlot
24 {
25 const Animation2D *animation = nullptr;
26 bool loop = false;
27 };
28
29 spk::SpriteRenderer2D &_spriteRenderer;
30 const Animation2D *_defaultAnimation;
31 const Animation2D *_currentAnimation;
32 std::optional<std::wstring> _currentAnimationName;
33 bool _currentAnimationLoop = true;
34 bool _isUsingDefaultAnimation = false;
35
36 std::unordered_map<std::wstring, AnimationSlot> _registeredAnimations;
37
38 spk::Timer _animationTimer;
39 float _animationSpeed = 1.0f;
40
41 void _applyAnimationFrame(float p_ratio);
42 void _activateDefaultAnimation(bool p_forceRestart);
43 void _restartTimerWithCurrentAnimation();
44 void _onUpdateEvent(spk::UpdateEvent &p_event) override;
45
46 public:
52 AnimationController2D(const std::wstring &p_name, spk::SpriteRenderer2D &p_spriteRenderer);
53
59
64 void setDefaultAnimation(const Animation2D *p_animation);
69 const Animation2D *defaultAnimation() const;
70
75 void setAnimationSpeed(float p_speed);
80 float animationSpeed() const;
81
88 void addAnimation(const std::wstring &p_name, const Animation2D *p_animation, bool p_shouldLoop);
94 bool hasAnimation(const std::wstring &p_name) const;
95
100 void startAnimation(const std::wstring &p_name);
104 void resetToDefault();
105
109 void start() override;
110 };
111}
Stores a sequence of meshes and a duration to sample frames from.
Definition spk_animation_2d.hpp:18
float animationSpeed() const
Returns the animation playback speed multiplier.
Definition spk_animation_controller_2d.cpp:125
bool hasAnimation(const std::wstring &p_name) const
Checks whether an animation name is registered.
Definition spk_animation_controller_2d.cpp:140
void setDefaultAnimation(const Animation2D *p_animation)
Sets the default animation used when no other is active.
Definition spk_animation_controller_2d.cpp:105
const Animation2D * defaultAnimation() const
Returns the default animation.
Definition spk_animation_controller_2d.cpp:115
void resetToDefault()
Resets playback to the default animation.
Definition spk_animation_controller_2d.cpp:173
void startAnimation(const std::wstring &p_name)
Starts a named animation.
Definition spk_animation_controller_2d.cpp:145
void start() override
Starts the controller and initializes animation state.
Definition spk_animation_controller_2d.cpp:178
void addAnimation(const std::wstring &p_name, const Animation2D *p_animation, bool p_shouldLoop)
Registers an animation with a name.
Definition spk_animation_controller_2d.cpp:130
const spk::SpriteRenderer2D & spriteRenderer() const
Returns the sprite renderer targeted by this controller.
Definition spk_animation_controller_2d.cpp:100
AnimationController2D(const std::wstring &p_name, spk::SpriteRenderer2D &p_spriteRenderer)
Builds a controller tied to a sprite renderer.
Definition spk_animation_controller_2d.cpp:87
void setAnimationSpeed(float p_speed)
Sets the animation playback speed multiplier.
Definition spk_animation_controller_2d.cpp:120
2D specialization of a component with a 2D owner accessor.
Definition spk_component_2d.hpp:16
Renders textured Mesh2D sprites.
Definition spk_sprite_renderer_2d.hpp:17
Countdown helper tracking elapsed ratio and timeout state.
Definition spk_timer.hpp:24
Carries per-frame update timing and input state snapshot.
Definition spk_update_event.hpp:15