#ifndef OBJECTS_ENTITIES_HPP_ #define OBJECTS_ENTITIES_HPP_ #include "../typedefs.hpp" #include "../physics/Hitbox.hpp" #include #include #include #include struct entity_funcs_set { bool init : 1; bool on_despawn : 1; bool on_grounded : 1; bool on_fall : 1; }; struct EntityDef; struct EntityId { entityid_t uid; const EntityDef& def; }; struct Transform { glm::vec3 pos; glm::vec3 size; glm::mat3 rot; glm::mat4 combined; void refresh(); }; struct Rigidbody { bool enabled = true; Hitbox hitbox; }; struct Scripting { entity_funcs_set funcsset; scriptenv env; }; class Level; class Assets; class LineBatch; class ModelBatch; class Frustum; class Rig; class Entities; class Entity { Entities& entities; entityid_t id; entt::registry& registry; const entt::entity entity; public: Entity(Entities& entities, entityid_t id, entt::registry& registry, const entt::entity entity) : entities(entities), id(id), registry(registry), entity(entity) {} entityid_t getID() const { return id; } bool isValid() const { return registry.valid(entity); } const EntityDef& getDef() const { return registry.get(entity).def; } Transform& getTransform() const { return registry.get(entity); } Rigidbody& getRigidbody() const { return registry.get(entity); } Scripting& getScripting() const { return registry.get(entity); } entityid_t getUID() const { return registry.get(entity).uid; } entt::entity getHandler() const { return entity; } void destroy(); }; class Entities { entt::registry registry; Level* level; std::unordered_map entities; entityid_t nextID = 1; public: Entities(Level* level); void clean(); void updatePhysics(float delta); void update(); void renderDebug(LineBatch& batch); void render(Assets* assets, ModelBatch& batch, Frustum& frustum); entityid_t spawn(EntityDef& def, glm::vec3 pos); std::optional get(entityid_t id) { const auto& found = entities.find(id); if (found != entities.end() && registry.valid(found->second)) { return Entity(*this, id, registry, found->second); } return std::nullopt; } void despawn(entityid_t id); inline size_t size() const { return entities.size(); } }; #endif // OBJECTS_ENTITIES_HPP_