Loading...
Searching...
No Matches
Scene.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3
5#include "../ecs/Entity.h"
6#include "glm/mat4x4.hpp"
7#include "glm/detail/type_vec3.hpp"
8#include "nlohmann/json.hpp"
9
10static constexpr hellfire::EntityID INVALID_ENTITY = 0;
11
12namespace hellfire {
13 class CameraComponent;
14
15 using EntityID = uint32_t;
16
17 /**
18 * @brief Manages a collection of entities and their hierarchical relationships
19 *
20 * The Scene class represents a complete game scene containing entities,
21 * their parent-child relationships, cameras, and environmental settings.
22 * It handles entity lifecycle, hierarchy management, and scene updates.
23 */
24 class Scene {
25 public:
26 /**
27 * @brief Constructs a new Scene with an optional name
28 * @param name The name of the scene (default: "Unnamed")
29 */
30 Scene(std::string name = "Unnamed");
31
32 /**
33 * @brief Destructor
34 */
35 virtual ~Scene();
36
37 /**
38 * @brief Creates a new entity in the scene
39 * @param name The name for the new entity (default: "GameObject")
40 * @return The unique ID of the newly created entity
41 */
42 EntityID create_entity(const std::string &name = "GameObject");
43
44 /**
45 * @brief Destroys an entity and removes it from the scene
46 * @param id The ID of the entity to destroy
47 */
48 void destroy_entity(EntityID id);
49
50 /**
51 * @brief Retrieves an entity by its ID
52 * @param id The ID of the entity to retrieve
53 * @return Pointer to the entity, or nullptr if not found
54 */
55 Entity *get_entity(EntityID id);
56
57 /**
58 * @brief Retrieves an entity by its ID (const version)
59 * @param id The ID of the entity to retrieve
60 * @return Const pointer to the entity, or nullptr if not found
61 */
62 const Entity *get_entity(EntityID id) const;
63
64 /**
65 * @brief Checks if one entity is a descendant of another
66 * @param potential_descendant The ID of the potential descendant entity
67 * @param potential_ancestor The ID of the potential ancestor entity
68 * @return True of potential_descendant is a descendant of potential_ancestor
69 */
70 bool is_descendant(EntityID potential_descendant, EntityID potential_ancestor);
71
72 // Hierarchy management
73
74 /**
75 * @brief Sets the parent of an entity
76 * @param child_id The ID of the child entity
77 * @param parent_id The ID of the parent entity
78 */
79 void set_parent(EntityID child_id, EntityID parent_id);
80
81 /**
82 * @brief Makes an entity a root entity (removes parent)
83 * @param entity_id The ID of the entity to make root
84 */
85 void set_as_root(EntityID entity_id);
86
87 /**
88 * @brief Gets the parent ID of an entity
89 * @param entity_id The ID of the entity
90 * @return The parent entity ID, or INVALID_ENTITY if no parent
91 */
92 EntityID get_parent(EntityID entity_id) const;
93
94 /**
95 * @brief Checks if an entity has a parent
96 * @param entity_id The ID of the entity to check
97 * @return True if the entity has a parent
98 */
99 bool has_parent(EntityID entity_id) const;
100
101 /**
102 * @brief Gets all children of a parent entity
103 * @param parent_id The ID of the parent entity
104 * @return Vector of child entity IDs
105 */
106 std::vector<EntityID> get_children(EntityID parent_id) const;
107
108 /**
109 * @brief Gets all root entities in the scene
110 * @return Reference to the vector of root entity IDs
111 */
112 const std::vector<EntityID> &get_root_entities() const { return root_entities_; }
113
114 // Scene lifecycle
115
116 /**
117 * @brief Initializes the scene
118 * Called once when the scene is first loaded or created
119 */
120 virtual void initialize();
121
122 /**
123 * @brief Updates all entities in the scene
124 * @param delta_time Time elapsed since last update in seconds
125 */
126 virtual void update(float delta_time);
127
128 /**
129 * @brief Updates world transformation matrices for all entities
130 * Propagates transformations through the entity hierarchy
131 */
133
134 // Finding entities
135
136 /**
137 * @brief Finds an entity by its name
138 * @param name The name of the entity to find
139 * @return Pointer to the entity, or nullptr if not found
140 */
141 Entity *find_entity_by_name(const std::string &name);
142
143 /**
144 * @brief Finds all entities that have a specific component type
145 * @tparam T The component type to search for
146 * @return Vector of entity IDs that have specified component
147 */
148 template<typename T>
150
151 // Camera management
152
153 /**
154 * @brief Sets the default camera for the scene
155 * @param camera_id
156 */
157 void set_default_camera(EntityID camera_id);
158
160
162
164
165 size_t get_entity_count() const { return entities_.size(); }
166
167
168
169 // Scene properties
170 const std::string &get_name() const { return name_; }
171 void set_name(const std::string &name) { name_ = name; }
172 bool is_playing() const { return is_playing_; }
173 void set_playing(bool active) { is_playing_ = active; }
174 void set_source_filename(const std::filesystem::path &filename) { source_filename_ = filename; }
175 const std::filesystem::path &get_source_filename() const { return source_filename_; }
176 bool was_loaded_from_file() const { return !source_filename_.empty(); }
177 std::string generate_unique_name(const std::string& base_name);
178
179 void save();
180
181 SceneEnvironment* environment() const { return environment_.get(); }
182
184
185 private:
186 // All entities owned by scene
188
189 // Hierarchy management
193
194 // Scene state
195 EntityID next_id_ = 1;
197 std::string name_;
199 std::filesystem::path source_filename_;
201
203
204 // Helper methods
205 void update_hierarchy(EntityID entity_id, float delta_time);
206
207 void update_world_matrices_recursive(unsigned int entity_id, const glm::mat4 &parent_world);
208
209 void find_entities_recursive(EntityID entity_id, const std::function<bool(Entity *)> &predicate,
210 std::vector<EntityID> &results);
211 };
212
213 template<typename T>
215 std::vector<EntityID> results;
216 auto predicate = [](Entity *entity) { return entity->has_component<T>(); };
217
218 for (EntityID root_id: root_entities_) {
219 find_entities_recursive(root_id, predicate, results);
220 }
221
222 return results;
223 }
224}
#define MODEL_LOADER_DEBUG
static constexpr hellfire::EntityID INVALID_ENTITY
Definition Scene.h:10
Definition Entity.h:20
bool is_looking
Definition Entity.h:21
static std::shared_ptr< Texture > load_cached_texture(const std::string &path, TextureType type)
static void process_mesh_vertices(aiMesh *mesh, std::vector< Vertex > &vertices, std::vector< unsigned int > &indices)
static bool is_identity_transform(const aiMatrix4x4 &matrix)
static std::shared_ptr< Mesh > process_mesh(aiMesh *mesh, const aiScene *scene, const std::string &filepath)
static EntityID process_node(Scene *scene, aiNode *node, const aiScene *ai_scene, const std::string &filepath, EntityID parent_id=0)
static std::string create_material_key(const aiMaterial *ai_material, const std::string &filepath, unsigned int material_index)
static void load_material_textures(const aiMaterial *ai_material, Material &material, const aiScene *scene, const std::string &filepath)
static std::vector< std::string > get_texture_search_paths(const std::string &filepath)
static std::string create_mesh_key(aiMesh *mesh, const std::string &filepath)
static std::unordered_map< std::string, std::shared_ptr< Texture > > texture_cache
static bool try_load_external_texture_unified(const std::string &path_str, const std::string &filepath, TextureType type, Material &material)
static bool try_load_embedded_texture_unified(const std::string &path_str, const aiScene *scene, TextureType type, Material &material)
static bool try_load_embedded_texture(const std::string &path_str, const aiScene *scene, TextureType dcr_type, Material &material, const std::string &property_name)
static std::shared_ptr< Material > create_material(const aiMaterial *ai_material, const aiScene *scene, const std::string &filepath)
static std::unordered_map< std::string, std::shared_ptr< Mesh > > mesh_cache
static std::string capitalize_first(const std::string &str)
static std::unordered_map< std::string, std::shared_ptr< Material > > material_cache
static bool try_load_external_texture(const std::string &path_str, const std::string &filepath, TextureType dcr_type, Material &material, const std::string &property_name)
static void debug_scene_info(const aiScene *scene, const std::string &filepath)
static EntityID load_model(Scene *scene, const std::filesystem::path &filepath, unsigned int import_flags=0)
static void load_essential_material_properties(const aiMaterial *ai_material, Material &material)
static void preprocess_materials(const aiScene *scene, const std::string &filepath)
virtual ~Component()=default
virtual void on_added(Entity *owner)
Definition Component.h:16
virtual void on_removed()
Definition Component.h:17
Entity & get_owner() const
Definition Component.h:13
const std::string & get_name() const
Definition Entity.h:46
const TransformComponent * transform() const
Definition Entity.cpp:52
TransformComponent * transform()
Definition Entity.cpp:42
T * add_component(Args &&... args)
const std::vector< ScriptComponent * > & get_script_components() const
Definition Entity.cpp:11
void initialize_scripts() const
Definition Entity.cpp:16
void cleanup_scripts() const
Definition Entity.cpp:30
bool has_component() const
Definition Entity.h:57
bool remove_component()
std::string name_
Definition Entity.h:86
virtual ~Entity()=default
std::vector< ScriptComponent * > script_components_
Definition Entity.h:88
std::unordered_map< std::type_index, std::unique_ptr< Component > > components_
Definition Entity.h:87
void send_event_to_script(const std::string &event_name, void *data=nullptr)
void set_name(const std::string &name)
Definition Entity.h:47
T * get_component() const
Entity(const EntityID id, const std::string &name)
Definition Entity.h:37
void update_scripts(float delta_time) const
Definition Entity.cpp:22
Entity(const std::string &name)
Definition Entity.h:41
void broadcast_event(const std::string &event_name, void *data=nullptr) const
Definition Entity.cpp:36
EntityID id_
Definition Entity.h:85
uint32_t get_id() const
Definition Entity.h:45
static void bind_property(const Material::Property &property, uint32_t shader_program, int &texture_unit)
static void bind_property_to_shader(const Material::Property &property, uint32_t shader_program, int &texture_unit)
static void bind_material(const Material &material)
void set_roughness(float roughness)
Definition Material.h:176
void set_metallic(float metallic)
Definition Material.h:172
void set_opacity(float opacity)
Definition Material.h:180
void set_shininess(float shininess)
Definition Material.h:168
void set_mesh_asset(AssetID id)
void set_mesh(std::shared_ptr< Mesh > mesh)
AssetID get_mesh_asset() const
MeshComponent(std::shared_ptr< Mesh > mesh)
MeshInternalType internal_type
std::shared_ptr< Mesh > get_mesh() const
MeshSource get_source() const
std::shared_ptr< Mesh > mesh_
void set_source(const MeshSource source, MeshInternalType type=MeshInternalType::NONE)
std::shared_ptr< Material > get_material() const
std::shared_ptr< Material > material_
void set_material(const std::shared_ptr< Material > &material)
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:183
Scene(std::string name="Unnamed")
Constructs a new Scene with an optional name.
Definition Scene.cpp:9
const Entity * get_entity(EntityID id) const
Retrieves an entity by its ID (const version)
Definition Scene.cpp:95
std::vector< EntityID > root_entities_
Definition Scene.h:192
virtual void initialize()
Initializes the scene Called once when the scene is first loaded or created.
Definition Scene.cpp:162
void set_playing(bool active)
Definition Scene.h:173
size_t get_entity_count() const
Definition Scene.h:165
void set_as_root(EntityID entity_id)
Makes an entity a root entity (removes parent)
Definition Scene.cpp:143
std::string name_
Definition Scene.h:197
const std::string & get_name() const
Definition Scene.h:170
bool was_loaded_from_file() const
Definition Scene.h:176
virtual void update(float delta_time)
Updates all entities in the scene.
Definition Scene.cpp:170
EntityID get_parent(EntityID entity_id) const
Gets the parent ID of an entity.
Definition Scene.cpp:147
bool is_playing_
Definition Scene.h:198
bool has_parent(EntityID entity_id) const
Checks if an entity has a parent.
Definition Scene.cpp:152
std::unique_ptr< SceneEnvironment > environment_
Definition Scene.h:202
EntityID default_camera_entity_id_
Definition Scene.h:196
std::filesystem::path source_filename_
Definition Scene.h:199
void set_default_camera(EntityID camera_id)
Sets the default camera for the scene.
Definition Scene.cpp:191
std::unordered_map< EntityID, std::unique_ptr< Entity > > & get_all_entities()
Definition Scene.h:183
std::unordered_map< EntityID, EntityID > parent_map_
Definition Scene.h:190
CameraComponent * get_default_camera() const
Definition Scene.cpp:198
std::unordered_map< EntityID, std::unique_ptr< Entity > > entities_
Definition Scene.h:187
void set_parent(EntityID child_id, EntityID parent_id)
Sets the parent of an entity.
Definition Scene.cpp:114
std::unordered_map< std::string, int > name_counters_
Definition Scene.h:200
void update_world_matrices()
Updates world transformation matrices for all entities Propagates transformations through the entity ...
Definition Scene.cpp:177
void set_source_filename(const std::filesystem::path &filename)
Definition Scene.h:174
virtual ~Scene()
Destructor.
Definition Scene.cpp:34
std::unordered_map< EntityID, std::vector< EntityID > > children_map_
Definition Scene.h:191
const std::filesystem::path & get_source_filename() const
Definition Scene.h:175
EntityID next_id_
Definition Scene.h:195
std::vector< EntityID > find_entities_with_component()
Finds all entities that have a specific component type.
Definition Scene.h:214
std::string generate_unique_name(const std::string &base_name)
Definition Scene.cpp:215
Entity * get_entity(EntityID id)
Retrieves an entity by its ID.
Definition Scene.cpp:90
void destroy_entity(EntityID id)
Destroys an entity and removes it from the scene.
Definition Scene.cpp:55
EntityID get_default_camera_entity_id() const
Definition Scene.h:159
const std::vector< EntityID > & get_root_entities() const
Gets all root entities in the scene.
Definition Scene.h:112
std::vector< EntityID > get_children(EntityID parent_id) const
Gets all children of a parent entity.
Definition Scene.cpp:157
std::vector< EntityID > get_camera_entities() const
Definition Scene.cpp:204
SceneEnvironment * environment() const
Definition Scene.h:181
void set_name(const std::string &name)
Definition Scene.h:171
void find_entities_recursive(EntityID entity_id, const std::function< bool(Entity *)> &predicate, std::vector< EntityID > &results)
Definition Scene.cpp:290
bool is_playing() const
Definition Scene.h:172
bool is_descendant(EntityID potential_descendant, EntityID potential_ancestor)
Checks if one entity is a descendant of another.
Definition Scene.cpp:100
void update_world_matrices_recursive(unsigned int entity_id, const glm::mat4 &parent_world)
Definition Scene.cpp:268
void update_hierarchy(EntityID entity_id, float delta_time)
Definition Scene.cpp:255
EntityID create_entity(const std::string &name="GameObject")
Creates a new entity in the scene.
Definition Scene.cpp:38
const glm::vec3 & get_rotation() const
glm::mat4 get_rotation_matrix() const
Definition Transform3D.h:69
const glm::mat4 & get_world_matrix() const
Definition Transform3D.h:94
glm::vec3 rotation_in_degrees_
glm::mat4 get_translation_matrix() const
Definition Transform3D.h:79
void match_orientation(const Transform3D &other)
void set_position(const float x, const float y, const float z)
Definition Transform3D.h:36
const glm::vec3 & get_position() const
Definition Transform3D.h:29
void set_position(const glm::vec3 &new_position)
Definition Transform3D.h:31
void extract_euler_angles_(const glm::mat4 &rotation_matrix, float &x, float &y, float &z)
float get_rotation_angle() const
Definition Transform3D.h:59
void set_scale_matrix(const glm::mat4 &scale_matrix)
Definition Transform3D.h:87
void set_translation_matrix(const glm::mat4 &translation_matrix)
Definition Transform3D.h:73
glm::mat4 translation_matrix_
glm::mat4 get_scale_matrix() const
Definition Transform3D.h:83
void look_at(const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0.0f, 1.0f, 0.0f))
const glm::vec3 & get_rotation_axis() const
Definition Transform3D.h:60
glm::vec3 get_scale() const
Definition Transform3D.h:52
void update_world_matrix(const glm::mat4 &parent_world_matrix)
const glm::mat4 & get_local_matrix() const
Definition Transform3D.h:93
void set_rotation(const glm::vec3 &angles)
void set_rotation_quaternion(const glm::quat &q)
void set_rotation_matrix(const glm::mat4 &rotation_matrix)
Definition Transform3D.h:63
glm::vec3 & get_scale()
Definition Transform3D.h:53
void set_scale(const glm::vec3 &new_scale)
Definition Transform3D.h:42
glm::vec3 & get_position()
Definition Transform3D.h:28
void look_at(const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0, 1, 0))
const glm::vec3 & get_position() const
void set_rotation(const glm::vec3 &eulers)
void update_world_matrix(const glm::mat4 &parent_world_matrix)
void set_position(float x, float y, float z)
const glm::vec3 & get_world_position() const
glm::mat4 get_rotation_matrix() const
void set_scale(float x, float y, float z)
const glm::vec3 & get_rotation() const
void set_rotation(float x, float y, float z)
glm::mat4 get_translation_matrix() const
void set_scale(const glm::vec3 &scale)
void set_scale(glm::vec3 &scale)
const glm::mat4 & get_world_matrix() const
const glm::mat4 & get_local_matrix() const
void set_position(const glm::vec3 &position)
glm::mat4 aiMatrix4x4ToGlm(const aiMatrix4x4 &from)
TextureType
Definition Texture.h:13
constexpr AssetID INVALID_ASSET_ID
Definition Vertex.h:5
static constexpr unsigned int PREVIEW
Definition ModelLoader.h:34
static constexpr unsigned int OPTIMIZED
Definition ModelLoader.h:64
static constexpr unsigned int HIGH_QUALITY
Definition ModelLoader.h:55
static constexpr unsigned int RUNTIME
Definition ModelLoader.h:42