Loading...
Searching...
No Matches
Entity.h
Go to the documentation of this file.
1//
2// Created by denzel on 07/08/2025.
3//
4#pragma once
5
6#include <iostream>
7#include <memory>
8#include <typeindex>
9#include <unordered_map>
10#include <vector>
11
12#include "Component.h"
13
14
15namespace hellfire {
17 class ScriptComponent;
18}
19
20class Test {
21 bool is_looking = true;
22};
23
24namespace hellfire {
25 using EntityID = uint32_t;
26
27 template<typename T>
28 concept ComponentType = std::derived_from<T, Component>;
29
30 template<typename T>
31 concept ScriptComponentType = std::derived_from<T, ScriptComponent>;
32
33 class Entity {
34 public:
35 virtual ~Entity() = default;
36
37 explicit Entity(const EntityID id, const std::string &name)
38 : id_(id), name_(name) {
39 }
40
41 explicit Entity(const std::string &name) : id_(0), name_(name) {
42 }
43
44 // Identification
45 [[nodiscard]] uint32_t get_id() const { return id_; }
46 [[nodiscard]] const std::string &get_name() const { return name_; }
47 void set_name(const std::string &name) { name_ = name; }
48
49 // Component management
50 template<ComponentType T, typename... Args>
51 T *add_component(Args &&... args);
52
53 template<ComponentType T>
54 [[nodiscard]] T *get_component() const;
55
56 template<ComponentType T>
57 bool has_component() const {
58 return components_.contains(std::type_index(typeid(T)));
59 }
60
61 template<ComponentType T>
63
64 // Script management
66
67 void initialize_scripts() const;
68
69 void update_scripts(float delta_time) const;
70
71 void cleanup_scripts() const;
72
73 // Event system (non-recursive)
74 void broadcast_event(const std::string &event_name, void *data = nullptr) const;
75
76 template<ScriptComponentType T>
77 void send_event_to_script(const std::string &event_name, void *data = nullptr);
78
79 // Convenience methods
81
82 [[nodiscard]] const TransformComponent *transform() const;
83
84 private:
85 EntityID id_;
86 std::string name_;
89 };
90
91#include "Entity.inl"
92} // namespace hellfire
#define MODEL_LOADER_DEBUG
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:177
void set_metallic(float metallic)
Definition Material.h:173
void set_opacity(float opacity)
Definition Material.h:181
void set_shininess(float shininess)
Definition Material.h:169
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
void set_parent(EntityID child_id, EntityID parent_id)
Sets the parent of an entity.
Definition Scene.cpp:105
void update_world_matrices()
Updates world transformation matrices for all entities Propagates transformations through the entity ...
Definition Scene.cpp:168
Entity * get_entity(EntityID id)
Retrieves an entity by its ID.
Definition Scene.cpp:81
EntityID create_entity(const std::string &name="GameObject")
Creates a new entity in the scene.
Definition Scene.cpp:29
void set_position(float x, float y, float z)
void set_rotation(float x, float y, float z)
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