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
Animated.cpp
Go to the documentation of this file.
1
7
8#include <nlohmann/json.hpp>
9
10#include "galaxy/core/ServiceLocator.hpp"
12
13#include "Animated.hpp"
14
15namespace galaxy
16{
17 namespace components
18 {
20 : Serializable {}
21 , m_paused {false}
22 , m_duration {0.0}
23 , m_anim {nullptr}
24 {
25 }
26
27 Animated::Animated(const nlohmann::json& json)
28 : Serializable {}
29 {
30 deserialize(json);
31 }
32
34 : Serializable {}
35 {
36 this->m_paused = a.m_paused;
37 this->m_duration = a.m_duration;
38 this->m_anim = a.m_anim;
39
40 a.m_anim = nullptr;
41 }
42
44 {
45 if (this != &a)
46 {
47 this->m_paused = a.m_paused;
48 this->m_duration = a.m_duration;
49 this->m_anim = a.m_anim;
50
51 a.m_anim = nullptr;
52 }
53
54 return *this;
55 }
56
58 {
59 m_anim = nullptr;
60 }
61
62 void Animated::set_animation(const std::string& animation)
63 {
64 auto& cache = core::ServiceLocator<resource::Animations>::ref();
65 m_anim = cache.get(animation);
66 }
67
68 nlohmann::json Animated::serialize()
69 {
70 nlohmann::json json = "{}"_json;
71
72 json["paused"] = m_paused;
73 json["duration"] = m_duration;
74
75 if (m_anim)
76 {
77 json["animation"] = m_anim->m_name;
78 }
79 else
80 {
81 json["no_animation"] = true;
82 }
83
84 return json;
85 }
86
87 void Animated::deserialize(const nlohmann::json& json)
88 {
89 m_paused = json.at("paused");
90 m_duration = json.at("duration");
91
92 if (!(json.count("no_animation") > 0))
93 {
94 set_animation(json.at("animation"));
95 }
96 }
97 } // namespace components
98} // namespace galaxy
Allows for an entity to be animated.
Definition Animated.hpp:22
virtual ~Animated()
Destructor.
Definition Animated.cpp:57
graphics::Animation * m_anim
Animation object.
Definition Animated.hpp:98
Animated & operator=(Animated &&)
Move assignment operator.
Definition Animated.cpp:43
void set_animation(const std::string &animation)
Sets the active animation.
Definition Animated.cpp:62
double m_duration
Time spent on current frame.
Definition Animated.hpp:93
bool m_paused
Is the animation paused.
Definition Animated.hpp:88
void deserialize(const nlohmann::json &json) override
Deserializes from object.
Definition Animated.cpp:87
nlohmann::json serialize() override
Serializes object.
Definition Animated.cpp:68
Timer.hpp galaxy.
Definition Async.hpp:17