Saga
Saga Game Engine
Loading...
Searching...
No Matches
componentContainer.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <unordered_map>
5#include <memory>
6#include "../Entity/entity.h"
7
8namespace Saga {
9
14public:
18 virtual ~IComponentContainer() = default;
19
24 virtual void onEntityDestroyed(Entity entity) = 0;
25
30 virtual int getLastReallocated() = 0;
31};
32
38template <typename Component>
40public:
46
56 template <typename... Args>
57 Component* emplace(const Entity entity, Args &&...args);
58
59 typename std::vector<Component>::iterator begin();
60 typename std::vector<Component>::iterator end();
61 typename std::vector<Component>::const_iterator begin() const;
62 typename std::vector<Component>::const_iterator end() const;
63
70 Component* getComponent(const Entity entity);
71
79 bool hasComponent(const Entity entity);
80
88 Entity getEntity(Component* component);
89
96 void removeComponent(const Entity entity);
97
101 inline int getActiveCnt() { return cnt; }
102
109 int getLastReallocated() override { return lastReallocated; }
110private:
111 // lets goo
112 std::vector<Component> components;
113 std::unordered_map<Entity, size_t> componentMap;
114 std::unordered_map<size_t, Entity> entityMap;
115 int cnt = 0;
116 int lastReallocated = 0;
117
118 void onEntityDestroyed(Entity entity) override;
122 void tryRepack();
123};
124
125} // namespace Saga
126
127#include "componentContainer.inl"
Container for a specific Component type.
Definition: componentContainer.h:39
Entity getEntity(Component *component)
Get an Entity with this specific component.
Definition: componentContainer.inl:54
int getLastReallocated() override
Get the last time the component container resize the vector containing all the components.
Definition: componentContainer.h:109
bool hasComponent(const Entity entity)
Determine if an entity has this type of Component attached.
Definition: componentContainer.inl:49
int getActiveCnt()
Definition: componentContainer.h:101
std::vector< Component >::iterator begin()
Definition: componentContainer.inl:11
Component * emplace(const Entity entity, Args &&...args)
Emplace a component onto an entity. This created.
Definition: componentContainer.inl:24
Component * getComponent(const Entity entity)
Get a pointer to a Component attached to an entity.
Definition: componentContainer.inl:42
virtual ~ComponentContainer()
Destroy the Component Container object.
Definition: componentContainer.h:45
void removeComponent(const Entity entity)
Remove a component from an entity. This also allows the slot used for the component to be free to use...
Definition: componentContainer.inl:72
std::vector< Component >::iterator end()
Definition: componentContainer.inl:14
Generic container of components.
Definition: componentContainer.h:13
virtual int getLastReallocated()=0
Get the last time the container was changed. Useful for cacheing references from the container.
virtual ~IComponentContainer()=default
Destroy the IComponentContainer object.
virtual void onEntityDestroyed(Entity entity)=0
Handler for when an entity is deleted. This should remove the entity from the Container,...
Definition: app.cpp:8