galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Animation.cpp
Go to the documentation of this file.
1
7
8#include <nlohmann/json.hpp>
9
10#include "galaxy/error/Log.hpp"
11#include "galaxy/scripting/JSON.hpp"
12
13#include "Animation.hpp"
14
15namespace galaxy
16{
17 namespace graphics
18 {
20 : m_speed {1.0}
21 , m_index {0}
22 {
23 }
24
26 {
27 this->m_speed = a.m_speed;
28 this->m_name = std::move(a.m_name);
29 this->m_index = a.m_index;
30 this->m_frames = std::move(a.m_frames);
31 }
32
34 {
35 if (this != &a)
36 {
37 this->m_speed = a.m_speed;
38 this->m_name = std::move(a.m_name);
39 this->m_index = a.m_index;
40 this->m_frames = std::move(a.m_frames);
41 }
42
43 return *this;
44 }
45
47 {
48 this->m_speed = a.m_speed;
49 this->m_name = a.m_name;
50 this->m_index = a.m_index;
51 this->m_frames = a.m_frames;
52 }
53
55 {
56 if (this != &a)
57 {
58 this->m_speed = a.m_speed;
59 this->m_name = a.m_name;
60 this->m_index = a.m_index;
61 this->m_frames = a.m_frames;
62 }
63
64 return *this;
65 }
66
70
71 bool Animation::load(const std::string& file)
72 {
73 auto json = json::read(file);
74 if (!json.empty())
75 {
76 m_name = std::filesystem::path(file).filename().string();
77
78 set_frames(json);
79 return true;
80 }
81 else
82 {
83 GALAXY_LOG(GALAXY_ERROR, "Tried to set animation to empty json frames: {0}.", file);
84 return false;
85 }
86 }
87
88 void Animation::set_frames(const nlohmann::json& json)
89 {
90 for (const auto& obj : json)
91 {
92 Frame frame {
93 .m_bounds = {obj.at("x"), obj.at("y"), obj.at("width"), obj.at("height")},
94 .m_duration = obj.at("duration")
95 };
96
97 m_frames.emplace_back(frame);
98 }
99 }
100
101 void Animation::set_frames(const meta::vector<Frame>& frames)
102 {
104 }
105
107 {
108 m_index++;
109
110 if (m_index >= m_frames.size())
111 {
112 m_index = 0;
113 }
114 }
115
117 {
118 m_index--;
119
120 if (m_index < 0)
121 {
122 m_index = m_frames.size() - 1;
123 }
124 }
125
127 {
128 m_index = 0;
129 }
130
132 {
133 return m_frames[m_index];
134 }
135
136 std::size_t Animation::total() const
137 {
138 return m_frames.size();
139 }
140
141 std::size_t Animation::index() const
142 {
143 return m_index;
144 }
145
146 meta::vector<Frame>& Animation::frames()
147 {
148 return m_frames;
149 }
150 } // namespace graphics
151} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_ERROR
Definition Log.hpp:24
2D animation information.
Definition Animation.hpp:28
double m_speed
Speed of the animation. Multiplier, so 1.0f is regular speed.
void prev()
Go back to the previous frame.
bool load(const std::string &file)
Load a json file from VFS and set frames from that.
Definition Animation.cpp:71
std::size_t index() const
Get current frame index.
meta::vector< Frame > & frames()
Get the frames.
std::size_t m_index
Index of the current active frame.
Animation & operator=(Animation &&)
Move assignment operator.
Definition Animation.cpp:33
void set_frames(const nlohmann::json &json)
Loads frames from json object.
Definition Animation.cpp:88
std::string m_name
Animation name.
std::size_t total() const
Get total frames.
void restart()
Sets animation back to beginning.
meta::vector< Frame > m_frames
Frames belonging to this animation.
void next()
Advance to the next frame.
Frame & current()
Get currently active frame.
Animated.cpp galaxy.
Definition Animated.cpp:16
Single frame of an animation.
Definition Frame.hpp:21