From 3f39e271742341f7d4aca04497ca55842981ca6e Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 16 Sep 2022 11:50:33 -0400 Subject: [PATCH] Add simple EntityManager for updating entities --- examples/dev_testing/src/main.cpp | 21 ++++++-------- include/simpleengine/ecs/entity.h | 3 ++ include/simpleengine/entity_manager.h | 25 +++++++++++++++++ src/entity_manager.cpp | 40 +++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 include/simpleengine/entity_manager.h create mode 100644 src/entity_manager.cpp diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index 93b5150..c3ab15d 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -1,6 +1,7 @@ #include "simpleengine/camera.h" #include "simpleengine/ecs/component/model_componenet.h" #include "simpleengine/ecs/entity.h" +#include "simpleengine/entity_manager.h" #include "simpleengine/gfx/light.h" #include "simpleengine/gfx/material.h" #include "simpleengine/gfx/model.h" @@ -169,30 +170,26 @@ int main(int argc, char *argv[]) { se::gfx::Material material(white_texture, 1.f, 0.f, 0.f, 0.f, 0.f); - auto entity = std::make_shared(); + // Create a model component se::gfx::Model model(cube_vertices, cube_indicies, std::optional(material)); model.calculate_normals(); - se::ModelComponent model_component(model); + // Create the entity and add the model component to it. + auto entity = std::make_shared(); entity->add_component(model_component); entity->translate(3.5f, 0.f, 0.f); - /* auto cube = std::make_shared(cube_vertices, cube_indicies, - std::optional(material)); - cube->calculate_normals(); - cube->translate(3.5f, 0.f, 0.f); */ - //game.add_event(cube); - + // Create a renderer and submit the entity into it. auto renderer = std::make_shared(game.get_window(), core_shader); renderer->enable_debug(); renderer->submit_entity(entity); game.add_renderable(renderer); - /* renderer->add_model(white_texture, cube); - game.add_event(renderer); */ - /* auto r_event = std::make_shared(renderer); - game.add_event(r_event); */ + // Create an EntityManager, and submit the entity into it. + auto ecs_manager = std::make_shared(); + ecs_manager->submit_entity(entity); + game.add_event(ecs_manager); auto camera = std::make_shared(game.get_window(), core_shader, 70, glm::vec3(0, 0, 0)); game.add_event(camera); diff --git a/include/simpleengine/ecs/entity.h b/include/simpleengine/ecs/entity.h index 8a2c8b0..bd061a1 100644 --- a/include/simpleengine/ecs/entity.h +++ b/include/simpleengine/ecs/entity.h @@ -18,6 +18,7 @@ namespace simpleengine { * */ class Entity : public simpleengine::Event, public simpleengine::Transformable { + // TODO: Don't extend from Event, create own destroy function private: static uint32_t incrementing_handle; uint32_t handle; @@ -58,6 +59,8 @@ namespace simpleengine { for (auto& component : components) { component->update(delta_time); } + + rotate_y(delta_time * 10); } template diff --git a/include/simpleengine/entity_manager.h b/include/simpleengine/entity_manager.h new file mode 100644 index 0000000..de7fe2d --- /dev/null +++ b/include/simpleengine/entity_manager.h @@ -0,0 +1,25 @@ +#pragma once + +#include "ecs/entity.h" +#include "event/event.h" + +#include +#include +#include + +namespace simpleengine { + class EntityManager : public simpleengine::Event { + public: + std::unordered_map> entities; + + EntityManager(); + + virtual void submit_entity(std::shared_ptr entity); + virtual bool withdraw_entity(std::shared_ptr entity); + + virtual void initialize(); + virtual void destroy() override; + + virtual void update(const float& delta_time) override; + }; +} \ No newline at end of file diff --git a/src/entity_manager.cpp b/src/entity_manager.cpp new file mode 100644 index 0000000..53e4c8e --- /dev/null +++ b/src/entity_manager.cpp @@ -0,0 +1,40 @@ +#include "entity_manager.h" + +simpleengine::EntityManager::EntityManager() { + +} + +void simpleengine::EntityManager::submit_entity(std::shared_ptr entity) { + entities.emplace(entity->get_handle(), entity); +} + +bool simpleengine::EntityManager::withdraw_entity(std::shared_ptr entity) { + auto it = entities.find(entity->get_handle()); + + if (it != entities.end()) { + it->second->destroy(); + entities.erase(it); + + return true; + } + + return false; +} + +void simpleengine::EntityManager::initialize() { + +} + +void simpleengine::EntityManager::destroy() { + std::cout << "Destroy entity manager!" << std::endl; + + for (auto& [handle, entity] : entities) { + entity->destroy(); + } +} + +void simpleengine::EntityManager::update(const float& delta_time) { + for (auto& [handle, entity] : entities) { + entity->update(delta_time); + } +} \ No newline at end of file