EntityManager class is responsible for managing entities within the ECS.It stores and tracks entities in a vector, allows for their retrieval, and performs operations like adding and erasing entities.
namespace ecs {
class EntityManager {
public:
EntityManager() = default;
EntityManager(const std::size_t &id)
{
_entitys.push_back(id);
}
EntityManager(const Entity &id)
{
_entitys.push_back(id);
}
class EntityManagerError : std::exception {
public:
EntityManagerError(std::string message) : _message(message) {}
~EntityManagerError() = default;
const char *what() const noexcept
{
return _message.c_str();
}
private:
std::string _message;
};
void addEntity(const Entity &entity)
{
_entitys.push_back(entity);
}
Entity operator[](const std::size_t &idx)
{
if (idx > _entitys.size()) {
throw EntityManagerError(ENTITY_MANAGER_ERROR_OUT_OF_RANGE);
}
return _entitys[idx];
}
void erase(const std::size_t &id)
{
for (std::size_t i = 0; i < _entitys.size(); ++i) {
if (_entitys[i].getId() == id) {
_entitys.erase(_entitys.begin(), _entitys.begin() + i);
continue;
}
_entitys[i] = i + 1;
}
}
std::size_t size() const
{
return _entitys.size();
}
Entity lastEntity() const
{
return _entitys[_entitys.size()];
}
private:
std::vector<Entity> _entitys;
};
}; // namespace ecs
EntityManager()
EntityManager.EntityManager(const std::size_t &id)
EntityManager with an initial entity based on a size/ID.EntityManager(const Entity &id)
EntityManager with an initial entity based on an Entity object.void addEntity(const Entity &entity)
Entity object to the manager by appending it to the internal vector of entities.Entity operator[](const std::size_t &idx)
EntityManagerError if the index is out of range.void erase(const std::size_t &id)
std::size_t size() const
EntityManager.Entity lastEntity() const
EntityManagerError : public std::exception
EntityManager.EntityManagerError(std::string message): Initializes the error with a custom message.~EntityManagerError() = default: Default destructor.const char *what() const noexcept:
std::exception::what() method to provide detailed information about the error.std::vector<Entity> _entitys:
Entity objects.