Add simple EntityManager for updating entities
This commit is contained in:
parent
04cf19d7bd
commit
3f39e27174
|
@ -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<simpleengine::Entity>();
|
||||
// Create a model component
|
||||
se::gfx::Model model(cube_vertices, cube_indicies, std::optional<se::gfx::Material>(material));
|
||||
model.calculate_normals();
|
||||
|
||||
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->translate(3.5f, 0.f, 0.f);
|
||||
|
||||
/* auto cube = std::make_shared<se::gfx::Model>(cube_vertices, cube_indicies,
|
||||
std::optional<se::gfx::Material>(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<se::gfx::Renderer>(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<RendererEvent>(renderer);
|
||||
game.add_event(r_event); */
|
||||
// Create an EntityManager, and submit the entity into it.
|
||||
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));
|
||||
game.add_event(camera);
|
||||
|
|
|
@ -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<typename T>
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue