R-Type 0.1
Loading...
Searching...
No Matches
Animator.hpp
1#pragma once
2
3#include <Registry.hpp>
4#include <array>
5#include <cstdint>
6#include <nlohmann/json.hpp>
7#include <string>
8
9namespace component {
18enum class animState : uint8_t {
19 IDLE = 0,
20 UP = 1,
21 DOWN = 2,
22 DIE = 3,
23 SHOOT = 4,
24 COUNT = 5,
25 NONE = 255
26};
27
32 uint16_t startX{0};
33 uint16_t startY{0};
34 uint16_t totalFrames{1};
35 float frameDuration{0.1f};
36 bool loop{true};
37 bool reverse{false};
38 uint16_t frameWidth{0};
39 uint16_t frameHeight{0};
40};
41
45struct animator {
49 std::array<animationClip, static_cast<std::size_t>(animState::COUNT)> clips{};
50};
51
57inline animState stringToAnimState(const std::string &stateStr) {
58 if (stateStr == "UP")
59 return animState::UP;
60 if (stateStr == "DOWN")
61 return animState::DOWN;
62 if (stateStr == "DIE")
63 return animState::DIE;
64 if (stateStr == "SHOOT")
65 return animState::SHOOT;
66 return animState::IDLE;
67}
68
75inline void animatorFromJson(rtk::ecs::Registry &reg, std::size_t entity,
76 const nlohmann::json &data) {
78
79 if (data.contains("currentState")) {
80 if (data["currentState"].is_string()) {
81 anim.currentState =
82 stringToAnimState(data["currentState"].get<std::string>());
83 } else if (data["currentState"].is_number()) {
84 anim.currentState =
85 static_cast<animState>(data["currentState"].get<int>());
86 }
87 }
89
90 if (data.contains("clips") && data["clips"].is_object()) {
91 for (const auto &[stateName, clipData] : data["clips"].items()) {
92 animState state = stringToAnimState(stateName);
93 std::size_t index = static_cast<std::size_t>(state);
94
95 anim.clips[index].startX = clipData.value<uint16_t>("startX", 0);
96 anim.clips[index].startY = clipData.value<uint16_t>("startY", 0);
97 anim.clips[index].totalFrames =
98 clipData.value<uint16_t>("totalFrames", 1);
99 anim.clips[index].frameDuration =
100 clipData.value<float>("frameDuration", 0.1f);
101 anim.clips[index].loop = clipData.value("loop", true);
102 anim.clips[index].reverse = clipData.value("reverse", false);
103 anim.clips[index].frameWidth = clipData.value<uint16_t>("frameWidth", 0);
104 anim.clips[index].frameHeight =
105 clipData.value<uint16_t>("frameHeight", 0);
106 }
107 }
108
109 reg.get_components<component::animator>().insert_at(entity, anim);
110}
111
113} // namespace component
animState stringToAnimState(const std::string &stateStr)
Converts a state name to an animState.
Definition Animator.hpp:57
animState
Animation states an animator can switch between.
Definition Animator.hpp:18
void animatorFromJson(rtk::ecs::Registry &reg, std::size_t entity, const nlohmann::json &data)
Adds an animator to an entity from its blueprint data.
Definition Animator.hpp:75
@ COUNT
Number of states.
@ IDLE
Default state.
@ NONE
No state, forces the next clip to load.
@ DOWN
Moving down.
Sprite sheet frames played for one animation state.
Definition Animator.hpp:31
uint16_t totalFrames
Number of frames.
Definition Animator.hpp:34
bool loop
Restarts after the last frame.
Definition Animator.hpp:36
uint16_t frameHeight
Frame height, 0 keeps the current one.
Definition Animator.hpp:39
bool reverse
Plays the frames backwards.
Definition Animator.hpp:37
uint16_t frameWidth
Frame width, 0 keeps the current one.
Definition Animator.hpp:38
uint16_t startY
Y of the first frame in the sheet.
Definition Animator.hpp:33
uint16_t startX
X of the first frame in the sheet.
Definition Animator.hpp:32
float frameDuration
Time per frame, in seconds.
Definition Animator.hpp:35
State machine that picks the Animation clip of an entity.
Definition Animator.hpp:45
animState previousState
Definition Animator.hpp:47
animState currentState
State to display.
Definition Animator.hpp:46
std::array< animationClip, static_cast< std::size_t >(animState::COUNT)> clips
One clip per state.
Definition Animator.hpp:49