Add simple EntityManager for updating entities

This commit is contained in:
SeanOMik 2022-09-16 11:50:33 -04:00
parent 04cf19d7bd
commit 3f39e27174
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
4 changed files with 77 additions and 12 deletions

View File

@ -1,6 +1,7 @@
#include "simpleengine/camera.h" #include "simpleengine/camera.h"
#include "simpleengine/ecs/component/model_componenet.h" #include "simpleengine/ecs/component/model_componenet.h"
#include "simpleengine/ecs/entity.h" #include "simpleengine/ecs/entity.h"
#include "simpleengine/entity_manager.h"
#include "simpleengine/gfx/light.h" #include "simpleengine/gfx/light.h"
#include "simpleengine/gfx/material.h" #include "simpleengine/gfx/material.h"
#include "simpleengine/gfx/model.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); se::gfx::Material material(white_texture, 1.f, 0.f, 0.f, 0.f, 0.f);
auto entity = std::make_shared<simpleengine::Entity>(); // Create a model component
se::gfx::Model model(cube_vertices, cube_indicies, std::optional<se::gfx::Material>(material)); se::gfx::Model model(cube_vertices, cube_indicies, std::optional<se::gfx::Material>(material));
model.calculate_normals(); model.calculate_normals();
se::ModelComponent model_component(model); se::ModelComponent model_component(model);
// Create the entity and add the model component to it.
auto entity = std::make_shared<simpleengine::Entity>();
entity->add_component(model_component); entity->add_component(model_component);
entity->translate(3.5f, 0.f, 0.f); entity->translate(3.5f, 0.f, 0.f);
/* auto cube = std::make_shared<se::gfx::Model>(cube_vertices, cube_indicies, // Create a renderer and submit the entity into it.
std::optional<se::gfx::Material>(material));
cube->calculate_normals();
cube->translate(3.5f, 0.f, 0.f); */
//game.add_event(cube);
auto renderer = std::make_shared<se::gfx::Renderer>(game.get_window(), core_shader); auto renderer = std::make_shared<se::gfx::Renderer>(game.get_window(), core_shader);
renderer->enable_debug(); renderer->enable_debug();
renderer->submit_entity(entity); renderer->submit_entity(entity);
game.add_renderable(renderer); game.add_renderable(renderer);
/* renderer->add_model(white_texture, cube);
game.add_event(renderer); */
/* auto r_event = std::make_shared<RendererEvent>(renderer); // Create an EntityManager, and submit the entity into it.
game.add_event(r_event); */ auto ecs_manager = std::make_shared<se::EntityManager>();
ecs_manager->submit_entity(entity);
game.add_event(ecs_manager);
auto camera = std::make_shared<se::Camera>(game.get_window(), core_shader, 70, glm::vec3(0, 0, 0)); auto camera = std::make_shared<se::Camera>(game.get_window(), core_shader, 70, glm::vec3(0, 0, 0));
game.add_event(camera); game.add_event(camera);

View File

@ -18,6 +18,7 @@ namespace simpleengine {
* *
*/ */
class Entity : public simpleengine::Event, public simpleengine::Transformable { class Entity : public simpleengine::Event, public simpleengine::Transformable {
// TODO: Don't extend from Event, create own destroy function
private: private:
static uint32_t incrementing_handle; static uint32_t incrementing_handle;
uint32_t handle; uint32_t handle;
@ -58,6 +59,8 @@ namespace simpleengine {
for (auto& component : components) { for (auto& component : components) {
component->update(delta_time); component->update(delta_time);
} }
rotate_y(delta_time * 10);
} }
template<typename T> template<typename T>

View File

@ -0,0 +1,25 @@
#pragma once
#include "ecs/entity.h"
#include "event/event.h"
#include <memory>
#include <unordered_map>
#include <vector>
namespace simpleengine {
class EntityManager : public simpleengine::Event {
public:
std::unordered_map<uint32_t, std::shared_ptr<simpleengine::Entity>> entities;
EntityManager();
virtual void submit_entity(std::shared_ptr<simpleengine::Entity> entity);
virtual bool withdraw_entity(std::shared_ptr<simpleengine::Entity> entity);
virtual void initialize();
virtual void destroy() override;
virtual void update(const float& delta_time) override;
};
}

40
src/entity_manager.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "entity_manager.h"
simpleengine::EntityManager::EntityManager() {
}
void simpleengine::EntityManager::submit_entity(std::shared_ptr<simpleengine::Entity> entity) {
entities.emplace(entity->get_handle(), entity);
}
bool simpleengine::EntityManager::withdraw_entity(std::shared_ptr<simpleengine::Entity> 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);
}
}