VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
Registry.hpp
1#pragma once
2#include <vector>
3#include <memory>
4#include <functional>
5#include "Types.hpp"
6#include "ComponentStorage.hpp"
7#include "VEX/VEX_export.h"
8
9namespace vex {
10
14class VEX_EXPORT ComponentType {
15public:
16 static uint32_t get_id_by_name(const char* name);
17
18 template <typename T>
19 static uint32_t get_id() {
20 static const uint32_t id = get_id_by_name(typeid(T).name());
21 return id;
22 }
23};
24
29class Registry {
30private:
32 std::vector<Entity> m_availableEntities;
36 std::vector<std::unique_ptr<IComponentStorage>> m_componentStorages;
37
43 template <typename T>
45 uint32_t typeId = ComponentType::get_id<T>();
46 if (typeId >= m_componentStorages.size()) {
47 m_componentStorages.resize(typeId + 1);
48 }
49 if (!m_componentStorages[typeId]) {
50 m_componentStorages[typeId] = std::make_unique<ComponentStorage<T>>();
51 }
52 return static_cast<ComponentStorage<T>*>(m_componentStorages[typeId].get());
53 }
54
55public:
60 template <typename T>
62 uint32_t typeId = ComponentType::get_id<T>();
63 if (typeId >= m_componentStorages.size() || !m_componentStorages[typeId]) {
64 return nullptr;
65 }
66 return static_cast<ComponentStorage<T>*>(m_componentStorages[typeId].get());
67 }
68
74 if (!m_availableEntities.empty()) {
75 Entity e = m_availableEntities.back();
76 m_availableEntities.pop_back();
77 return e;
78 }
79 return m_nextEntity++;
80 }
81
86 void destroy(Entity entity) {
87 for (auto& Storage : m_componentStorages) {
88 if (Storage) Storage->entity_destroyed(entity);
89 }
90 m_availableEntities.push_back(entity);
91 }
92
99 template<typename T, typename... Args>
100 T& add_or_replace(Entity entity, Args&&... args) {
101 return get_storage<T>()->add_or_replace(entity, std::forward<Args>(args)...);
102 }
103
107 template<typename T>
108 void remove(Entity entity) {
109 get_storage<T>()->remove(entity);
110 }
111
117 template<typename T>
118 T& get(Entity entity) {
119 return get_storage<T>()->get(entity);
120 }
121
126 template<typename T>
127 T* try_get(Entity entity) {
128 auto* arr = try_get_storage_internal<T>();
129 return (arr && arr->has(entity)) ? &arr->get(entity) : nullptr;
130 }
131
136 template<typename T>
137 bool has(Entity entity) {
138 auto* arr = try_get_storage_internal<T>();
139 return arr != nullptr && arr->has(entity);
140 }
141
145 template<typename T>
146 void on_create(std::function<void(Entity, T&)> cb) { get_storage<T>()->on_create(cb); }
147
151 template<typename T>
152 void on_destroy(std::function<void(Entity, T&)> cb) { get_storage<T>()->on_destroy(cb); }
153};
154
155}
Template-based storage container for components of a specific type.
Manages unique type IDs for component types in the ECS system.
Definition Registry.hpp:14
Central registry for managing entities and their components in the ECS system.
Definition Registry.hpp:29
std::vector< Entity > m_availableEntities
Pool of reusable entity IDs from destroyed entities.
Definition Registry.hpp:32
Entity m_nextEntity
Counter for generating new entity IDs when the pool is empty.
Definition Registry.hpp:34
void on_create(std::function< void(Entity, T &)> cb)
Registers a callback to be invoked when a component of type T is created.
Definition Registry.hpp:146
ComponentStorage< T > * get_storage()
Retrieves or creates the storage for component type T.
Definition Registry.hpp:44
bool has(Entity entity)
Checks if an entity has a component of type T.
Definition Registry.hpp:137
ComponentStorage< T > * try_get_storage_internal()
Attempts to retrieve the storage for component type T without creating it.
Definition Registry.hpp:61
T & add_or_replace(Entity entity, Args &&... args)
Adds or replaces a component for an entity.
Definition Registry.hpp:100
T & get(Entity entity)
Retrieves a component from an entity.
Definition Registry.hpp:118
void remove(Entity entity)
Removes a component from an entity.
Definition Registry.hpp:108
void destroy(Entity entity)
Destroys an entity and all its associated components.
Definition Registry.hpp:86
Entity create()
Creates a new entity.
Definition Registry.hpp:73
std::vector< std::unique_ptr< IComponentStorage > > m_componentStorages
Array of component storages indexed by component type ID.
Definition Registry.hpp:36
T * try_get(Entity entity)
Safely retrieves a component from an entity if it exists.
Definition Registry.hpp:127
void on_destroy(std::function< void(Entity, T &)> cb)
Registers a callback to be invoked when a component of type T is destroyed.
Definition Registry.hpp:152
uint32_t Entity
Type alias representing a unique entity identifier in the ECS system.
Definition Types.hpp:11