Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
ComponentStorage.hpp
Go to the documentation of this file.
1#pragma once
2#include "Entity.hpp"
3#include <vector>
4#include <unordered_map>
5#include <stdexcept>
6
7// Base interface
12struct IStorage {
16 virtual ~IStorage() = default;
21 virtual size_t size() const = 0;
26 virtual void removeEntity(Entity e) = 0;
27
32 virtual const std::vector<Entity>& getEntities() const = 0;
33};
34
40template<typename T>
41class ComponentStorage : public IStorage {
42public:
48 void add(Entity e, T comp) {
49 auto it = entityToIndex.find(e);
50 if (it != entityToIndex.end()) {
51 data[it->second] = std::move(comp);
52 return;
53 }
54 entities.push_back(e);
55 data.push_back(std::move(comp));
56 entityToIndex[e] = data.size() - 1;
57 }
58
59
65 bool has(Entity e) const {
66 return entityToIndex.find(e) != entityToIndex.end();
67 }
68
73 void remove(Entity e) {
74 auto it = entityToIndex.find(e);
75 if (it == entityToIndex.end()) return;
76
77 size_t index = it->second;
78 size_t last = data.size() - 1;
79
80 // swap-remove
81 data[index] = data[last];
82 entities[index] = entities[last];
84
85 data.pop_back();
86 entities.pop_back();
87 entityToIndex.erase(e);
88 }
89
94 void removeEntity(Entity e) override {
95 remove(e);
96 }
97
103 T& get(Entity e) {
104 auto it = entityToIndex.find(e);
105 if (it == entityToIndex.end()) throw std::runtime_error("Component not found");
106 return data[it->second];
107 }
108
113 const std::vector<Entity>& getEntities() const override {
114 return entities;
115 }
116
121 size_t size() const override { return data.size(); }
122
127 auto begin() { return data.begin(); }
132 auto end() { return data.end(); }
137 auto entityBegin() { return entities.begin(); }
142 auto entityEnd() { return entities.end(); }
143
144private:
146 std::vector<T> data; // contiguous component data
148 std::vector<Entity> entities; // entities aligned with data
150 std::unordered_map<Entity, size_t> entityToIndex; // fast lookup
151};
Template class storing components contiguously in memory for optimal cache usage.
Definition ComponentStorage.hpp:41
void remove(Entity e)
Removes the component from an entity.
Definition ComponentStorage.hpp:73
void add(Entity e, T comp)
Adds a component to an entity.
Definition ComponentStorage.hpp:48
void removeEntity(Entity e) override
Polymorphic interface to remove entity component.
Definition ComponentStorage.hpp:94
std::vector< T > data
Contiguous array of component instances.
Definition ComponentStorage.hpp:146
size_t size() const override
Returns the size of the storage.
Definition ComponentStorage.hpp:121
const std::vector< Entity > & getEntities() const override
Retrieves all entities with this component.
Definition ComponentStorage.hpp:113
auto entityBegin()
Gets start iterator of stored entity list.
Definition ComponentStorage.hpp:137
auto begin()
Gets start iterator of stored component data.
Definition ComponentStorage.hpp:127
auto entityEnd()
Gets end iterator of stored entity list.
Definition ComponentStorage.hpp:142
bool has(Entity e) const
Checks if an entity possesses this component.
Definition ComponentStorage.hpp:65
T & get(Entity e)
Retrieves the component associated with an entity.
Definition ComponentStorage.hpp:103
auto end()
Gets end iterator of stored component data.
Definition ComponentStorage.hpp:132
std::unordered_map< Entity, size_t > entityToIndex
Mapping for quick entity component lookup.
Definition ComponentStorage.hpp:150
std::vector< Entity > entities
Entity mapping matching data array order.
Definition ComponentStorage.hpp:148
Lightweight identifier representing a game object in the Entity Component System.
Definition Entity.hpp:11
Polymorphic base interface for all component storage types, allowing untyped manipulation of componen...
Definition ComponentStorage.hpp:12
virtual void removeEntity(Entity e)=0
Removes the component associated with the given entity.
virtual ~IStorage()=default
Virtual destructor.
virtual const std::vector< Entity > & getEntities() const =0
Returns list of entities mapped to components.
virtual size_t size() const =0
Returns the number of component instances stored.
static ufbxi_noinline ufbxi_aa_node index
Definition ufbx.cpp:4444