-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EntityManager - Entity Component System
Added Entity Manager on the Engine ECS ECS Refactoring
- Loading branch information
Berags
committed
May 4, 2022
1 parent
6523292
commit 3d8c257
Showing
6 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// Created by Jacopo Beragnoli on 04/05/22. | ||
// | ||
|
||
#ifndef MINIMINIMOTORWAYS_ENTITYMANAGER_H | ||
#define MINIMINIMOTORWAYS_ENTITYMANAGER_H | ||
|
||
#include <unordered_map> | ||
#include "Entity.h" | ||
#include "../FrameInfo.h" | ||
|
||
namespace Engine::ECS { | ||
class EntityManager { | ||
public: | ||
using Map = std::unordered_map<Engine::ECS::Entity::id_t, Engine::ECS::Entity>; | ||
static constexpr uint32_t MAX_ENTITIES = 64; | ||
|
||
// Creates a new Entity and adds it to availableEntities Map | ||
// Checks if number of living entities is greater than MAX_ENTITIES | ||
// and asserts if true | ||
Engine::ECS::Entity createNewEntity(); | ||
|
||
// Destroys an Entity from availableEntities Map | ||
// Calls onDestroy method on all Entity components | ||
// Updates numberOfLivingEntities to match availableEntities size | ||
void destroyEntity(Engine::ECS::Entity::id_t id); | ||
|
||
// Returns an Entity from a given id | ||
Engine::ECS::Entity &getEntity(Engine::ECS::Entity::id_t id); | ||
|
||
// Updates all Entities stored in availableEntities | ||
// i.e. calls method update() on each entity | ||
void updateEntities(Engine::FrameInfo &frameInfo); | ||
|
||
private: | ||
// Stores all living Entities | ||
Engine::ECS::EntityManager::Map availableEntities{}; | ||
|
||
// Number of living Entities, should always match availableEntities size | ||
uint32_t livingEntityCount{}; | ||
}; | ||
} | ||
|
||
|
||
#endif //MINIMINIMOTORWAYS_ENTITYMANAGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Created by Jacopo Beragnoli on 04/05/22. | ||
// | ||
|
||
#include "../../../include/engine/ecs/EntityManager.h" | ||
|
||
namespace Engine::ECS { | ||
Engine::ECS::Entity EntityManager::createNewEntity() { | ||
assert(livingEntityCount < Engine::ECS::EntityManager::MAX_ENTITIES && "Too many entities!"); | ||
|
||
auto entity = Engine::ECS::Entity::create(); | ||
availableEntities.emplace(livingEntityCount, entity); | ||
livingEntityCount++; | ||
return entity; | ||
} | ||
|
||
void EntityManager::destroyEntity(Engine::ECS::Entity::id_t id) { | ||
assert(id < Engine::ECS::EntityManager::MAX_ENTITIES && "Entity out of range!"); | ||
|
||
availableEntities.at(id).destroy(); | ||
availableEntities.erase(id); | ||
livingEntityCount--; | ||
} | ||
|
||
Engine::ECS::Entity &EntityManager::getEntity(Engine::ECS::Entity::id_t id) { | ||
assert(id < Engine::ECS::EntityManager::MAX_ENTITIES && "Entity out of range!"); | ||
|
||
return availableEntities.at(id); | ||
} | ||
|
||
void EntityManager::updateEntities(Engine::FrameInfo &frameInfo) { | ||
std::for_each(availableEntities.begin(), availableEntities.end(), [&](auto &item) { | ||
auto &entity = item.second; | ||
entity.update(frameInfo.frameTime); | ||
}); | ||
} | ||
} |