R-Type 0.1
Loading...
Searching...
No Matches
Animation.hpp
1#pragma once
2
3#include <cstdint>
4#include <nlohmann/json.hpp>
5
6#include <Registry.hpp>
7
8namespace component {
17struct Animation {
18 float frameDuration{0.1f};
19 float elapsedTime{0.0f};
20
21 uint16_t currentFrame{0};
22 uint16_t totalFrames{1};
23
24 uint16_t frameWidth{0};
25 uint16_t frameHeight{0};
26
27 uint16_t startX{0};
28 uint16_t startY{0};
29
30 uint16_t columns{0};
31 uint16_t paddingX{0};
32 uint16_t paddingY{0};
33
34 bool loop{true};
35 bool playing{true};
36 bool reverse{false};
37};
38
45inline void animationFromJson(rtk::ecs::Registry &reg, std::size_t entity,
46 const nlohmann::json &data) {
47 component::Animation animation{};
48 animation.frameDuration = data.value("frameDuration", 0.1f);
49 animation.elapsedTime = data.value("elapsedTime", 0.0f);
50 animation.currentFrame =
51 data.value("currentFrame", static_cast<std::uint16_t>(0));
52 animation.totalFrames =
53 data.value("totalFrames", static_cast<std::uint16_t>(1));
54 animation.frameWidth =
55 data.value("frameWidth", static_cast<std::uint16_t>(0));
56 animation.frameHeight =
57 data.value("frameHeight", static_cast<std::uint16_t>(0));
58 animation.startX = data.value("startX", static_cast<std::uint16_t>(0));
59 animation.startY = data.value("startY", static_cast<std::uint16_t>(0));
60 animation.columns = data.value("columns", static_cast<std::uint16_t>(0));
61 animation.paddingX = data.value("paddingX", static_cast<std::uint16_t>(0));
62 animation.paddingY = data.value("paddingY", static_cast<std::uint16_t>(0));
63 animation.loop = data.value("loop", true);
64 animation.playing = data.value("playing", true);
65 animation.reverse = data.value("reverse", false);
66
67 reg.get_components<component::Animation>().insert_at(entity, animation);
68}
69
71} // namespace component
void animationFromJson(rtk::ecs::Registry &reg, std::size_t entity, const nlohmann::json &data)
Adds an Animation to an entity from its blueprint data.
Definition Animation.hpp:45
Sprite sheet animation played by changing the texture rectangle.
Definition Animation.hpp:17
uint16_t paddingX
Horizontal gap between frames.
Definition Animation.hpp:31
float elapsedTime
Time spent on the current frame.
Definition Animation.hpp:19
uint16_t startY
Y of the first frame in the sheet.
Definition Animation.hpp:28
uint16_t startX
X of the first frame in the sheet.
Definition Animation.hpp:27
uint16_t paddingY
Vertical gap between rows.
Definition Animation.hpp:32
bool reverse
Plays the frames backwards.
Definition Animation.hpp:36
bool playing
Advances frames while true.
Definition Animation.hpp:35
uint16_t frameHeight
Frame height in the sheet, in pixels.
Definition Animation.hpp:25
uint16_t currentFrame
Index of the visible frame.
Definition Animation.hpp:21
uint16_t columns
Frames per row, 0 for a single row.
Definition Animation.hpp:30
float frameDuration
Time each frame stays visible, in seconds.
Definition Animation.hpp:18
bool loop
Restarts after the last frame.
Definition Animation.hpp:34
uint16_t frameWidth
Frame width in the sheet, in pixels.
Definition Animation.hpp:24
uint16_t totalFrames
Number of frames in the animation.
Definition Animation.hpp:22