#pragma once #include #include #include #include #include struct InfoComponent { std::string name, tag; }; struct TransformComponent { glm::vec3 position = glm::vec3(0); }; struct MeshAsset; struct MaterialAsset; struct MeshComponent { MeshAsset* mesh = nullptr; MaterialAsset* material = nullptr; }; enum class LightType { Point, Directional }; struct LightComponent { LightType type = LightType::Point; glm::vec3 color = glm::vec3(1); glm::mat4 matrix = glm::mat4(1.0f); }; struct CameraComponent { float fov = 75.0f, near = 0.1f, far = 100.0f; float focusDistance = 0.0f; float aperture = 0.0f; glm::vec3 target = glm::vec3(0); }; using EntityID = uint64_t; struct World; namespace ECS { // components inline std::map infos; inline std::map transforms; inline std::map meshes; inline std::map lights; inline std::map cameras; inline std::map worlds; inline EntityID lastID = 1; static inline EntityID createEntity(World* world) { EntityID newID = lastID++; worlds[newID] = world; return newID; } static inline void destroyEntity(const EntityID id) { } template static inline T* addComponent(const EntityID id) { T* t = new T(); if constexpr(std::is_same::value) { infos[id] = t; } else if constexpr(std::is_same::value) { transforms[id] = t; } else if constexpr(std::is_same::value) { meshes[id] = t; } else if constexpr(std::is_same::value) { lights[id] = t; } else if constexpr(std::is_same::value) { cameras[id] = t; } return t; } template static inline T* getComponent(const EntityID id) { if constexpr(std::is_same::value) { return infos[id]; } else if constexpr(std::is_same::value) { return transforms[id]; } else if constexpr(std::is_same::value) { return meshes[id]; } else if constexpr(std::is_same::value) { return lights[id]; } else if constexpr(std::is_same::value) { return cameras[id]; } } template static inline auto getWorldComponents(World* world) { std::vector entities; for (auto it = worlds.begin(); it != worlds.end(); ++it) { if(it->second == world) { entities.push_back(it->first); } } std::vector> components; for(auto ent : entities) { T* t = getComponent(ent); if(t != nullptr) components.push_back(std::tuple(ent, t)); } return components; } static inline std::vector getWorldEntities(World* world) { std::vector entities; for (auto it = worlds.begin(); it != worlds.end(); ++it) { if(it->second == world) entities.push_back(it->first); } return entities; } template static inline void removeComponent(const EntityID id) { delete transforms[id]; } };