FECS
Loading...
Searching...
No Matches
entity_manager.h
Go to the documentation of this file.
1
5
6#pragma once
7#include <fecs/core/types.h>
8#include <vector>
9
10namespace FECS
11{
12 namespace Manager
13 {
21 {
22 public:
27 {
28 }
29
34 void Reserve(std::uint32_t amount)
35 {
36 m_Versions.reserve(amount);
37 m_FreeList.reserve(amount);
38 }
39
49 {
50 std::uint32_t idx;
51
52 // Reuse from freelist if possible
53 if (m_FreeList.empty())
54 {
55 idx = static_cast<std::uint32_t>(m_Versions.size());
56 m_Versions.push_back(0);
57 }
58 else
59 {
60 idx = m_FreeList.back();
61 m_FreeList.pop_back();
62 }
63 return FECS::BuildEntityIndex(idx, m_Versions[idx]);
64 }
65
74 {
75 std::uint32_t idx = FECS::GetEntityIndex(e);
76 m_Versions[idx]++;
77 m_FreeList.push_back(idx);
78 }
79
88 bool IsAlive(Entity e) const
89 {
90 std::uint32_t idx = FECS::GetEntityIndex(e);
91 std::uint32_t ver = FECS::GetEntityVersion(e);
92 return (idx < m_Versions.size() && m_Versions[idx] == ver);
93 }
94
95 private:
96 std::vector<std::uint32_t> m_Versions;
97 std::vector<std::uint32_t> m_FreeList;
98 };
99 }
100}
bool IsAlive(Entity e) const
Checks whether an entity is still valid (alive).
Definition entity_manager.h:88
void Destroy(Entity e)
Destroys and entity and recycles its index.
Definition entity_manager.h:73
EntityManager()
Constructs an empty EntityManager.
Definition entity_manager.h:26
Entity Create()
Creates a new Entity.
Definition entity_manager.h:48
void Reserve(std::uint32_t amount)
Pre-allocates memory for a number of entities.
Definition entity_manager.h:34
Defines core types and constants for the FECS ECS system.
Entity BuildEntityIndex(std::uint32_t index, std::uint32_t version)
Combines an entity index and version into a single 32-bit entity ID.
Definition types.h:63
std::uint32_t GetEntityIndex(Entity e)
Extracts the index portion from an entity ID.
Definition types.h:73
std::uint32_t Entity
Type alias for entity IDs (32-bit unsigned integer)
Definition types.h:28
std::uint32_t GetEntityVersion(Entity e)
Extracts the verison portion from an entity ID.
Definition types.h:83