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
Scene.cpp
Go to the documentation of this file.
1
7
8#include <entt/locator/locator.hpp>
9#include <sol/sol.hpp>
10
11#include "Scene.hpp"
12
13namespace galaxy
14{
15 Scene::Scene(const std::string& name) noexcept
16 : State {name}
17 {
18 /*
19 auto& w = core::ServiceLocator<core::Window>::ref();
20 m_camera.set_viewport(w.frame_width(), w.frame_height());
21 auto& nui = core::ServiceLocator<ui::NuklearUI>::ref();
22 m_dispatcher.sink<events::WindowResized>().connect<&Scene::on_window_resized>(this);
23 m_dispatcher.sink<events::MousePressed>().connect<&ui::NuklearUI::on_mouse_pressed>(nui);
24 m_dispatcher.sink<events::MouseWheel>().connect<&ui::NuklearUI::on_mouse_wheel>(nui);
25 m_dispatcher.sink<events::KeyChar>().connect<&ui::NuklearUI::on_key_char>(nui);
26 m_dispatcher.sink<events::KeyPress>().connect<&ui::NuklearUI::on_key_press>(nui);
27 m_dispatcher.sink<events::ContentScale>().connect<&ui::NuklearUI::on_content_scale>(nui);
28 */
29 }
30
32 {
33 clear();
34 }
35
37 {
38 }
39
41 {
42 if (entt::locator<sol::state>::has_value())
43 {
44 entt::locator<sol::state>::value().collect_garbage();
45 }
46 }
47
48 void Scene::update(Registry& registry)
49 {
50 // graphics::Renderer::ref().flush();
51 // m_registry.update(m_b2world);
52 // m_b2world.Step(GALAXY_DT, m_velocity_iterations, m_position_iterations);
53 for (auto&& system : m_systems)
54 {
55 system->update(registry);
56 }
57 }
58
60 {
61 /*
62 graphics::Renderer::ref().begin_post();
63
64 // Scene specific.
65 graphics::Renderer::ref().submit_camera(m_camera);
66 graphics::Renderer::ref().draw();
67
68 graphics::Renderer::ref().end_post();
69 graphics::Renderer::ref().begin_default();
70 graphics::Renderer::ref().render_post();
71 graphics::Renderer::ref().end_default();
72
73 // Scene specific.
74 auto& nui = core::ServiceLocator<ui::NuklearUI>::ref();
75
76 nui.new_frame();
77 update_ui();
78 nui.render();
79
80 graphics::Renderer::ref().end_default();
81 */
82 }
83
85 {
86 m_systems.clear();
87 }
88
89 void Scene::add_system(const std::string& system)
90 {
91 auto& sf = entt::locator<SystemFactory>::value();
92 sf.create_system(system, m_systems);
93 }
94
95 /*void Scene::update_ui()
96 {
97 const auto view = m_registry.m_entt.view<components::GUI>(entt::exclude<flags::Disabled>);
98 for (auto&& [entity, gui] : view.each())
99 {
100 if (gui.m_update.valid())
101 {
102 gui.m_update(gui.m_self);
103 }
104 }
105 }
106 bool Scene::load_world(const std::string& file)
107 {
108 if (m_world.load(file))
109 {
110 m_world.parse();
111 return true;
112 }
113
114 return false;
115 }
116
117 nlohmann::json Scene::serialize()
118 {
119 json["camera"] = m_camera.serialize();
120 json["physics"] = nlohmann::json::object();
121 auto& physics = json.at("physics");
122
123 auto gravity = m_b2world.GetGravity();
124 physics["gravity"]["x"] = gravity.x;
125 physics["gravity"]["y"] = gravity.y;
126
127 physics["allow_sleeping"] = m_b2world.GetAllowSleeping();
128 physics["allow_autoclearforces"] = m_b2world.GetAutoClearForces();
129 physics["velocity_iterations"] = m_velocity_iterations;
130 physics["position_iterations"] = m_position_iterations;
131 json["name"] = m_name;
132 json["ldtk_world"] = m_world.file();
133 json["current_map"] = m_world.get_active() ? m_world.get_active()->name() : "";
134 nlohmann::json json = "{}"_json;
135 json["camera"] = m_camera.serialize();
136 json["entities"] = nlohmann::json::array();
137
138 auto& em = core::ServiceLocator<meta::EntityMeta>::ref();
139
140 for (const auto& [entity] : m_registry.m_entt.view<entt::entity>(entt::exclude<flags::DenySerialization>).each())
141 {
142 json["entities"].push_back(em.serialize_entity(entity, m_registry.m_entt));
143 }
144
145 json["physics"] = nlohmann::json::object();
146 auto& physics = json.at("physics");
147
148 auto gravity = m_b2world.GetGravity();
149 physics["gravity"]["x"] = gravity.x;
150 physics["gravity"]["y"] = gravity.y;
151
152 physics["allow_sleeping"] = m_b2world.GetAllowSleeping();
153 physics["allow_autoclearforces"] = m_b2world.GetAutoClearForces();
154 physics["velocity_iterations"] = m_velocity_iterations;
155 physics["position_iterations"] = m_position_iterations;
156 json["name"] = m_name;
157 json["ldtk_world"] = m_world.file();
158 json["current_map"] = m_world.get_active() ? m_world.get_active()->name() : "";
159
160 nlohmann::json json = "{}"_json;
161 json["name"] = m_name;
162
163 json["systems"] = nlohmann::json::object();
164 for (auto i = 0; i < m_systems.size(); i++)
165 {
166 json["systems"][std::to_string(i)] = m_systems[i]->id();
167 }
168
169 return json;
170 }
171
172 void Scene::deserialize(const nlohmann::json& json)
173 {
174 m_name = json.at("name");
175
176 const auto& systems = json.at("systems");
177 m_systems.reserve(systems.size());
178 for (const auto& [index, name] : systems.items())
179 {
180 add_system(name.get<std::string>());
181 }
182
183 m_camera.deserialize(json.at("camera"));
184 auto& em = core::ServiceLocator<meta::EntityMeta>::ref();
185
186 const auto& physics = json.at("physics");
187 const auto& gravity = physics.at("gravity");
188
189 m_b2world.SetGravity({gravity.at("x"), gravity.at("y")});
190 m_b2world.SetAllowSleeping(physics.at("allow_sleeping"));
191 m_b2world.SetAutoClearForces(physics.at("allow_autoclearforces"));
192 m_velocity_iterations = physics.at("velocity_iterations");
193 m_position_iterations = physics.at("position_iterations");
194
195 if (load_world(json.at("ldtk_world")))
196 {
197 m_world.set_active(json.at("current_map"));
198 }
199 m_camera.deserialize(json.at("camera"));
200 auto& em = core::ServiceLocator<meta::EntityMeta>::ref();
201
202 m_registry.clear();
203
204 const auto& physics = json.at("physics");
205 const auto& gravity = physics.at("gravity");
206
207 m_b2world.SetGravity({gravity.at("x"), gravity.at("y")});
208 m_b2world.SetAllowSleeping(physics.at("allow_sleeping"));
209 m_b2world.SetAutoClearForces(physics.at("allow_autoclearforces"));
210 m_velocity_iterations = physics.at("velocity_iterations");
211 m_position_iterations = physics.at("position_iterations");
212
213 const auto& entity_json = json.at("entities");
214 for (const auto& data : entity_json)
215 {
216 const auto entity = em.deserialize_entity(data, m_registry.m_entt);
217
218 if (!m_registry.m_entt.all_of<components::Tag>(entity))
219 {
220 auto& tag = m_registry.m_entt.emplace<components::Tag>(entity);
221 tag.m_tag = "Untagged";
222 }
223 }
224
225 m_name = json.at("name");
226
227 if (load_world(json.at("ldtk_world")))
228 {
229 m_world.set_active(json.at("current_map"));
230 }
231 }
232
233 */
234} // namespace galaxy
Wrapper around entt::registry to expand functionality.
Definition Registry.hpp:19
void on_push() override
When scene is pushed to the stack.
Definition Scene.cpp:36
SystemStack m_systems
List of systems to process.
Definition Scene.hpp:93
void on_pop() override
When scene is popped from the stack.
Definition Scene.cpp:40
void clear()
Remove all scene data.
Definition Scene.cpp:84
virtual ~Scene()
Destructor.
Definition Scene.cpp:31
void add_system(const std::string &system)
Add a system to operate on entities in this scene.
Definition Scene.cpp:89
void render()
Render scene.
Definition Scene.cpp:59
void update(Registry &registry)
Process events and updates.
Definition Scene.cpp:48
Scene()=delete
Constructor.
A state to use in a finite state machine.
Definition State.hpp:19
Animated.cpp galaxy.
Definition Animated.cpp:16