Loading...
Searching...
No Matches
SceneManager.cpp
Go to the documentation of this file.
1#include "SceneManager.h"
2#include "Scene.h"
4#include "hellfire/ecs/CameraComponent.h"
5#include <fstream>
6#include <iostream>
7
8#include "hellfire/assets/SceneAssetResolver.h"
9#include "hellfire/ecs/ComponentRegistration.h"
10#include "hellfire/serializers/SceneSerializer.h"
11#include "hellfire/utilities/ServiceLocator.h"
12
13namespace hellfire {
15 // Serialization of scene components
17 }
18
20 clear();
21 }
22
24 return scenes_;
25 }
26
29 }
30
31 Scene *SceneManager::create_scene(const std::string &name) {
32 auto *scene = new Scene(name);
33 scenes_.push_back(scene);
34 return scene;
35 }
36
37 Scene *SceneManager::load_scene(const std::filesystem::path &filename) {
38 // Check if already loaded
39 for (Scene *scene: scenes_) {
40 if (scene->get_source_filename() == filename) {
41 std::cout << "Scene already loaded: " << filename << "\n";
42 return scene;
43 }
44 }
45
46 // Open the file for reading
47 std::ifstream file(filename);
48 if (!file.is_open()) {
49 std::cerr << "Failed to open scene file: " << filename << std::endl;
50 return nullptr;
51 }
52
53 // Create and empty scene and deserialize components
54 Scene* new_scene = create_scene();
55 if (!Serializer<Scene>::deserialize(file, new_scene)) {
56 std::cerr << "Failed to deserialize scene: " << filename << std::endl;
57 destroy_scene(new_scene);
58 return nullptr;
59 }
60
61
62 // Resolve asset references
63 if (auto* asset_manager = ServiceLocator::get_service<AssetManager>()) {
64 SceneAssetResolver resolver(*asset_manager);
65 resolver.resolve(*new_scene);
66 } else {
67 std::cerr << "Warning: No AssetManager available, assets not resolved for scene: "
68 << filename << std::endl;
69 }
70
71 // Track scene asset ID for hot-reload / saving
72 if (auto* asset_registry = ServiceLocator::get_service<AssetRegistry>()) {
73 if (const auto scene_uuid = asset_registry->get_uuid_by_path(filename)) {
74 scene_asset_ids_[new_scene] = *scene_uuid;
75 }
76 }
77
78 new_scene->set_source_filename(filename);
79 return new_scene;
80 }
81
82 Scene* SceneManager::load_scene(AssetID asset_id, const std::filesystem::path& filename) {
83 Scene* scene = load_scene(filename);
84 if (scene) {
85 scene_asset_ids_[scene] = asset_id;
86 }
87 return scene;
88 }
89
90 std::optional<AssetID> SceneManager::get_scene_asset_id(Scene* scene) const {
91 auto it = scene_asset_ids_.find(scene);
92 if (it != scene_asset_ids_.end()) {
93 return it->second;
94 }
95 return std::nullopt;
96 }
97
98 std::optional<AssetID> SceneManager::get_active_scene_asset_id() const {
99 return get_scene_asset_id(active_scene_);
100 }
101
103 if (!scene) return false;
104
105 std::filesystem::path filepath = scene->get_source_filename();
106 if (filepath.empty()) {
107 std::cerr << "Scene has no source filename, use save_scene_as()" << std::endl;
108 return false;
109 }
110
111 return save_scene_as(filepath.string(), scene);
112 }
113
114 bool SceneManager::save_scene_as(const std::string &filename, Scene *scene) {
115 if (!scene) scene = get_active_scene();
116 if (!scene) return false;
117
118 std::ofstream file(filename);
119 if (!file.is_open()) {
120 std::cerr << "Failed to open file for writing: " << filename << std::endl;
121 return false;
122 }
123
124 if (!Serializer<Scene>::serialize(file, scene)) {
125 std::cerr << "Failed to serialize scene: " << filename << std::endl;
126 return false;
127 }
128
129 scene->set_source_filename(filename);
130
131 // Register with asset registry if not already
132 if (auto* asset_registry = ServiceLocator::get_service<AssetRegistry>()) {
133 if (!asset_registry->get_uuid_by_path(filename)) {
134 AssetID new_id = asset_registry->register_asset(filename, AssetType::SCENE);
135 scene_asset_ids_[scene] = new_id;
136 }
137 }
138
139 std::cout << "Scene saved: " << filename << std::endl;
140 return true;
141 }
142
143
144 void SceneManager::update(float delta_time) {
145 if (active_scene_) {
146 active_scene_->update(delta_time);
147 }
148 }
149
151 for (Scene* scene : scenes_) {
152 delete scene;
153 }
154 scenes_.clear();
155 scene_asset_ids_.clear();
156 active_scene_ = nullptr;
157 }
158
160 if (!scene) return;
161
162 // Remove from asset ID map
163 scene_asset_ids_.erase(scene);
164
165 // Remove from scenes vector
166 auto it = std::find(scenes_.begin(), scenes_.end(), scene);
167 if (it != scenes_.end()) {
168 scenes_.erase(it);
169 }
170
171 // Clear active if this was it
172 if (active_scene_ == scene) {
173 active_scene_ = nullptr;
174 }
175
176 delete scene;
177 }
178
179
180 EntityID SceneManager::find_entity_by_name(const std::string &name) {
181 if (active_scene_) {
183 return entity ? entity->get_id() : 0;
184 }
185 return 0;
186 }
187
188 void SceneManager::set_active_camera(EntityID camera_id) const {
189 if (active_scene_) {
191 }
192 }
193
195 if (active_scene_) {
197 }
198 return nullptr;
199 }
200
202 if (active_scene_) {
203 return active_scene_->get_camera_entities();
204 }
205 return {};
206 }
207
208 void SceneManager::set_active_scene(Scene *scene, const bool should_play) {
209 if (scene == active_scene_) return;
210
211 active_scene_ = scene;
212 if (scene) {
213 scene->set_playing(should_play);
216 }
217 }
218 }
219}
uint32_t get_id() const
Definition Entity.h:45
void destroy_scene(Scene *scene)
CameraComponent * get_active_camera() const
bool save_scene(Scene *scene)
Scene * create_scene(const std::string &name="GameScene")
Scene * load_scene(const std::filesystem::path &filename)
std::optional< AssetID > get_scene_asset_id(Scene *scene) const
void set_active_camera(EntityID camera) const
std::optional< AssetID > get_active_scene_asset_id() const
SceneActivatedCallback scene_activated_callback_
Scene * get_active_scene() const
bool save_scene_as(const std::string &filename, Scene *scene)
std::vector< Scene * > scenes_
std::vector< Scene * > get_scenes()
std::unordered_map< Scene *, AssetID > scene_asset_ids_
EntityID find_entity_by_name(const std::string &name)
std::vector< EntityID > get_camera_entities() const
Scene * load_scene(AssetID asset_id, const std::filesystem::path &filename)
void update(float delta_time)
Manages a collection of entities and their hierarchical relationships.
Definition Scene.h:24
Entity * find_entity_by_name(const std::string &name)
Finds an entity by its name.
Definition Scene.cpp:174
Scene(std::string name="Unnamed")
Constructs a new Scene with an optional name.
Definition Scene.cpp:9
void set_playing(bool active)
Definition Scene.h:173
virtual void update(float delta_time)
Updates all entities in the scene.
Definition Scene.cpp:161
void set_default_camera(EntityID camera_id)
Sets the default camera for the scene.
Definition Scene.cpp:182
CameraComponent * get_default_camera() const
Definition Scene.cpp:189
void set_source_filename(const std::filesystem::path &filename)
Definition Scene.h:174
const std::filesystem::path & get_source_filename() const
Definition Scene.h:175
void register_all_components()
static bool serialize(std::ostream &output, const Scene *scene)
static bool deserialize(std::istream &input, Scene *scene)