19 template <u
int16_t PoolSize>
24 m_FreeIndexes.reserve(PoolSize);
34 for (uint16_t i = 0; i < PoolSize; ++i) {
35 Entity entity = compositor(scene);
38 m_FreeIndexes.push_back(i);
41 m_Resetter = resetter;
51 if (m_Mask.count() > 0) {
52 uint16_t freeIndex = m_FreeIndexes.back();
53 m_FreeIndexes.pop_back();
54 m_Mask[freeIndex] =
false;
55 Entity entity = m_Pool[freeIndex];
57 return std::make_pair(entity, freeIndex);
60 return std::unexpected(
Core::CoriError<uint16_t>(
"Ran out of free indexes, all entities are currently busy.",
"Pool Capacity", PoolSize));
63 return std::unexpected(
Core::CoriError(
"Disposable entity pool is not initialized, call Init first!"));
74 if (index < m_FreeIndexes.size()) {
78 return std::unexpected(
Core::CoriError<uint16_t>(std::format(
"Index '{}' out of bounds.", index),
"Pool Capacity", PoolSize));
81 return std::unexpected(
Core::CoriError(
"Disposable entity pool is not initialized, call Init first!"));
89 if (!m_Mask[slot] && m_Initialized) {
90 m_FreeIndexes.push_back(slot);
92 Entity entity = m_Pool[slot];
110 [[nodiscard]] uint16_t
Size()
const {
111 return m_FreeIndexes.size();
118 for (uint16_t i = 0; i < PoolSize; ++i) {
124 std::vector<uint16_t> m_FreeIndexes;
125 std::array<Entity, PoolSize> m_Pool;
126 std::function<void(
Entity&)> m_Resetter;
127 std::bitset<PoolSize> m_Mask;
128 bool m_Initialized{
false };
Custom error class mainly used in std::expected.
This class utilizes std::variant to hold one of the possible exception types.
void Init(SceneHandle scene, std::function< Entity(SceneHandle &)> &&compositor, std::function< void(Entity &)> &&resetter)
Initializes the pool.
void FreeAll()
Frees all indexes at once.
uint16_t Size() const
Returns the amount of free entities currently present in the pool.
std::expected< Entity, Core::PossibleErrors< Core::CoriError< uint16_t >, Core::CoriError<> > > GetEntityAtIndex(const uint16_t index)
Retries the entity at a particular index, regardless if it's free or not.
void FreeIndex(const uint16_t slot)
Frees the entity at the given index, a resetter will be run for the entity at the specified index,...
uint16_t Capacity() const
Returns the total capacity of the pool, how many entities can it fit in total.
std::expected< std::pair< Entity, uint16_t >, Core::PossibleErrors< Core::CoriError< uint16_t >, Core::CoriError<> > > GetFreeEntity()
Finds a free entity in a pool (if any) in O(1) time. After retrieving the entity will be considered b...
Entities are the essential part of WorldSystem.
void SetActive(const bool state)
Changes the activity state of the entity.
A handle for the scene, checks for scene validity before any call to the scene, if scene is invalid a...
Anything connected to WorldSystem (ECS) is in this namespace.