4#include <unordered_map>
58 for (
auto& [
id, storage] :
storages) {
63 storage->removeEntity(e);
67 for (
auto& cb : callbacksIt->second) {
80 auto aliveCopy =
entities.getAlive();
81 for (
auto id : aliveCopy) {
94 const auto& alive =
entities.getAlive();
95 return std::find(alive.begin(), alive.end(), e.
getId()) != alive.end();
128 if (storage && storage->has(e)) {
148 if (!storage || !storage->has(e))
return nullptr;
149 return &storage->get(e);
161 if (!ptr)
throw std::runtime_error(
"Component missing!");
174 return storage && storage->has(e);
204 template<
typename... Components>
249 return std::tuple_cat(std::make_tuple(e), std::forward_as_tuple(*
registry.get<Components>(e)...));
263 static const std::vector<Entity> empty;
264 const std::vector<Entity>& ents =
smallest ?
smallest->getEntities() : empty;
278 template<
typename... Components>
282 return View<Components...>(*
this, smallest);
289 std::unordered_map<std::size_t, std::unique_ptr<IStorage>>
storages;
303 storages[id] = std::make_unique<ComponentStorage<T>>();
316 if (it ==
storages.end())
return nullptr;
324 template<
typename... Components>
342 template<
typename First,
typename Second,
typename... Rest>
348 return (a->
size() < b->
size()) ? a : b;
#define ENGINE_API
Definition EngineAPI.hpp:10
std::bitset< MAX_COMPONENTS > ComponentMask
Bitmask representing component registration status.
Definition Entity.hpp:55
ENGINE_API std::unordered_map< std::type_index, std::size_t > & g_componentTypeMap()
Retrieves the global map of component types to unique numeric IDs.
Definition Registry.cpp:3
ENGINE_API std::size_t registerComponentType(std::type_index idx)
Registers or retrieves the unique ID for a component type.
Definition Registry.cpp:8
Template class storing components contiguously in memory for optimal cache usage.
Definition ComponentStorage.hpp:41
Lightweight identifier representing a game object in the Entity Component System.
Definition Entity.hpp:11
static constexpr IdType INVALID_ENTITY
Constant representing a null or invalid entity.
Definition Entity.hpp:16
IdType getId() const
Retrieve the raw identifier of the entity.
Definition Entity.hpp:32
Manages entity allocation, deallocation, component masks, and lifetime tracking.
Definition EntityManager.hpp:13
Zero-allocation filtered view over entities containing a specific set of components.
Definition Registry.hpp:205
IStorage * smallest
Definition Registry.hpp:270
Iterator end()
Returns ending view iterator.
Definition Registry.hpp:262
View(Registry ®, IStorage *sm)
Construct a new View object.
Definition Registry.hpp:212
Registry & registry
Definition Registry.hpp:269
Iterator begin()
Returns beginning view iterator.
Definition Registry.hpp:254
ComponentStorage< T > * getStorage()
Retrieves storage class for component type.
Definition Registry.hpp:313
std::function< void(Entity)> ComponentAddedCallback
Callback function type invoked when a component is added.
Definition Registry.hpp:38
bool isValid(Entity e) const
Checks if an entity is alive and valid.
Definition Registry.hpp:92
bool has(Entity e)
Checks if an entity possesses a component of type T.
Definition Registry.hpp:172
T * get(Entity e)
Retrieves a pointer to a component of type T on an entity.
Definition Registry.hpp:146
std::unordered_map< std::size_t, std::vector< ComponentAddedCallback > > componentAddedCallbacks
Callbacks for component insertions.
Definition Registry.hpp:291
void remove(Entity e)
Removes a component of type T from an entity.
Definition Registry.hpp:126
T & getRef(Entity e)
Retrieves a reference to a component of type T on an entity. Throws if missing.
Definition Registry.hpp:159
void ensureStorage()
Ensures a component storage exists.
Definition Registry.hpp:300
std::unordered_map< std::size_t, std::vector< ComponentRemovedCallback > > componentRemovedCallbacks
Callbacks for component removals.
Definition Registry.hpp:293
EntityManager entities
Entity lifetime manager.
Definition Registry.hpp:287
void subscribeToAdded(ComponentAddedCallback cb)
Subscribes a callback to when a component of type T is added.
Definition Registry.hpp:183
void ensureStorages()
Ensures all given component type storages exist.
Definition Registry.hpp:325
std::unordered_map< std::size_t, std::unique_ptr< IStorage > > storages
Map of component storage classes keyed by type ID.
Definition Registry.hpp:289
std::function< void(Entity)> ComponentRemovedCallback
Callback function type invoked when a component is removed.
Definition Registry.hpp:40
void clear()
Destroys all entities and clears all component storages.
Definition Registry.hpp:79
Entity create()
Creates a new entity.
Definition Registry.hpp:46
auto view()
Generates a filtered view of entities containing the requested components.
Definition Registry.hpp:279
Registry()=default
Construct a new Registry object.
IStorage * getSmallestStorage()
Base case for retrieving smallest storage from parameter pack.
Definition Registry.hpp:333
void destroy(Entity e)
Destroys an entity, removing all its components.
Definition Registry.hpp:52
T & emplace(Entity e, T &&comp)
Emplaces a component on an entity.
Definition Registry.hpp:106
void subscribeToRemoved(ComponentRemovedCallback cb)
Subscribes a callback to when a component of type T is removed.
Definition Registry.hpp:194
IStorage * getSmallestStorage()
Recursively determines smallest storage among requested components.
Definition Registry.hpp:343
Polymorphic base interface for all component storage types, allowing untyped manipulation of componen...
Definition ComponentStorage.hpp:12
virtual size_t size() const =0
Returns the number of component instances stored.
Iterator over the entities matching the View filters.
Definition Registry.hpp:218
auto operator*() const
Dereferences the iterator to a tuple containing the entity and its component references.
Definition Registry.hpp:247
void advance()
Helper to advance iterator to next matching entity.
Definition Registry.hpp:224
void operator++()
Move iterator forward.
Definition Registry.hpp:238
size_t index
Definition Registry.hpp:221
Registry & registry
Definition Registry.hpp:219
const std::vector< Entity > & entities
Definition Registry.hpp:220
bool operator!=(const Iterator &other) const
Check iterator inequality.
Definition Registry.hpp:235