GPR5100 - Rollback
Loading...
Searching...
No Matches
component.h
1#pragma once
2
3#include "engine/globals.h"
4#include "engine/entity.h"
5#include "utils/assert.h"
6
7#include <cstdint>
8
9
10namespace core
11{
12using Component = std::uint32_t;
13enum class ComponentType : Component
14{
15 EMPTY = 1u,
16 POSITION = 1u << 1u,
17 SCALE = 1u << 2u,
18 ROTATION = 1u << 3u,
19 TRANSFORM = POSITION | SCALE | ROTATION,
20 SPRITE = 1u << 4u,
21 BODY2D = 1u << 5u,
22 BOX_COLLIDER2D = 1u << 6u,
23 OTHER_TYPE = 1u << 7u
24};
25
31template<typename T, Component C>
33{
34public:
35 ComponentManager(EntityManager& entityManager) : entityManager_(entityManager)
36 {
37 components_.resize(entityInitNmb);
38 }
39 virtual ~ComponentManager() = default;
40
41 ComponentManager(const ComponentManager&) = delete;
42 ComponentManager& operator=(ComponentManager&) = delete;
44 ComponentManager& operator=(ComponentManager&&) = delete;
45
50 virtual void AddComponent(Entity entity);
55 virtual void RemoveComponent(Entity entity);
61 [[nodiscard]] const T& GetComponent(Entity entity) const;
67 [[nodiscard]] T& GetComponent(Entity entity);
73 void SetComponent(Entity entity, const T& value);
78 [[nodiscard]] const std::vector<T>& GetAllComponents() const;
84 void CopyAllComponents(const std::vector<T>& components);
85protected:
86 EntityManager& entityManager_;
87 std::vector<T> components_;
88};
89
90template <typename T, Component C>
92{
93 gpr_assert(entity != INVALID_ENTITY, "Invalid Entity");
94 //Invalid entity would allocate too much memory
95 if (entity == INVALID_ENTITY)
96 return;
97 // Resize components array if too small
98 auto newSize = components_.size();
99 if (newSize == 0)
100 {
101 newSize = 2;
102 }
103 while (entity >= newSize)
104 {
105 newSize = newSize + newSize / 2;
106 }
107 components_.resize(newSize);
108
109 entityManager_.AddComponent(entity, C);
110}
111
112template <typename T, Component C>
114{
115 gpr_assert(entity != INVALID_ENTITY, "Invalid Entity");
116 gpr_warn(entityManager_.HasComponent(entity, C), "Entity has not the removing component");
117 entityManager_.RemoveComponent(entity, C);
118}
119
120template <typename T, Component C>
122{
123 gpr_assert(entity != INVALID_ENTITY, "Invalid Entity");
124 gpr_warn(entityManager_.HasComponent(entity, C), "Entity has not the requested component");
125 return components_[entity];
126}
127
128template <typename T, Component C>
130{
131 gpr_assert(entity != INVALID_ENTITY, "Invalid Entity");
132 gpr_warn(entityManager_.HasComponent(entity, C), "Entity has not the requested component");
133 return components_[entity];
134}
135
136template <typename T, Component C>
138{
139 gpr_assert(entity != INVALID_ENTITY, "Invalid Entity");
140 gpr_warn(entityManager_.HasComponent(entity, C), "Entity has not the requested component");
141 components_[entity] = value;
142}
143
144template <typename T, Component C>
145const std::vector<T>& ComponentManager<T, C>::GetAllComponents() const
146{
147 return components_;
148}
149
150template <typename T, Component C>
151void ComponentManager<T, C>::CopyAllComponents(const std::vector<T>& components)
152{
153 components_ = components;
154}
155} // namespace core
ComponentManager is a class that owns Component in a contiguous array. Component indexing is done wit...
Definition: component.h:33
void SetComponent(Entity entity, const T &value)
SetComponent is a method that sets a new value of the Component of an Entity.
Definition: component.h:137
virtual void RemoveComponent(Entity entity)
RemoveComponent is a method that unsets the flag C in the EntityManager.
Definition: component.h:113
T & GetComponent(Entity entity)
GetComponent is a method that gets a reference to a Component given the Entity.
Definition: component.h:129
const T & GetComponent(Entity entity) const
GetComponent is a method that gets a constant reference to a Component given the Entity.
Definition: component.h:121
const std::vector< T > & GetAllComponents() const
GetAllComponents is a method that returns the internal array of components.
Definition: component.h:145
virtual void AddComponent(Entity entity)
AddComponent is a method that sets the flag C in the EntityManager and resize if need the components_...
Definition: component.h:91
void CopyAllComponents(const std::vector< T > &components)
CopyAllComponents is a method that changes the internal components array by copying a newly provided ...
Definition: component.h:151
Manages the entities in an array using bitwise operations to know if it has components.
Definition: entity.h:35
constexpr Entity INVALID_ENTITY
INVALID_ENTITY is a constant that define an invalid Entity.
Definition: entity.h:26
std::uint32_t Entity
Entity is the type used to define an game world entity. An Entity is just an index,...
Definition: entity.h:17