VEGO-Engine  0.1
Loading...
Searching...
No Matches
Entity.h
1#pragma once
2
3#include <array>
4#include <memory>
5#include <bitset>
6#include <vector>
7
8#include "ColliderComponent.h"
9#include "ECS.h"
10#include "Constants.h"
11
12// TODO: remove here if possible
13// temporary fix: addComponent function template doesnt know TransformComponent -> error undefined type
14#include "InputComponent.h"
15#include "TransformComponent.h"
16#include "SpriteComponent.h"
17
18class Manager;
19class Component;
20
21using ComponentBitSet = std::bitset<MAX_COMPONENTS>;
22using GroupBitSet = std::bitset<MAX_GROUPS>;
23using ComponentArray = std::array<Component*, MAX_COMPONENTS>;
24
34class Entity
35{
36public:
37
55
59 explicit Entity(Manager& mManager) :
60 manager(mManager) { };
61
62 void update(uint_fast16_t diffTime) const;
63
64 bool isActive() const { return this->active; }
67 void destroy() {
68 this->active = false;
70 this->getComponent<ColliderComponent>().removeCollision();
71 }
72 }
73
74 bool hasGroup(Group mGroup);
75 void addGroup(Group mGroup);
76 void delGroup(Group mGroup);
79 std::bitset<MAX_GROUPS> getGroupBitSet();
80
82 Manager& getManager() { return manager; };
83
84 template <typename T> bool hasComponent() const
85 {
86 return componentBitSet[getComponentTypeID<T>()];
87 }
88
91 template <typename T, typename...TArgs> T& addComponent(TArgs&&...mArgs)
92 {
93 T* c(new T(std::forward<TArgs>(mArgs)...));
94 c->entity = this;
95 std::shared_ptr<Component> uPtr{ c };
96 this->components.at(getComponentTypeID<T>()) = std::move(uPtr);
97
98 componentArray[getComponentTypeID<T>()] = c;
99 componentBitSet[getComponentTypeID<T>()] = true;
100
101 c->init();
102 return *c;
103 };
104
105 template <typename T> T& getComponent() const
106 {
107 auto ptr(componentArray[getComponentTypeID<T>()]);
108 return *static_cast<T*>(ptr);
109 }
110
111 template <typename T> std::shared_ptr<T> getComponentAsPointer() const
112 {
113 return std::static_pointer_cast<T>(components.at(getComponentTypeID<T>()));
114 }
115
116private:
117 Manager& manager;
118 bool active = true;
119 std::array<std::shared_ptr<Component>, MAX_COMPONENTS> components;
120
121 ComponentArray componentArray = {};
122 ComponentBitSet componentBitSet;
123 GroupBitSet groupBitSet;
124};
Definition Component.h:8
void destroy()
Definition Entity.h:67
void addGroup(Group mGroup)
Definition Entity.cpp:19
GroupLabel
Used for rendering order (last is highest) or retrieving entities of group.
Definition Entity.h:46
@ PROJECTILE
Definition Entity.h:51
@ POWERUPS
Definition Entity.h:53
@ MAPTILES
Entity using TileComponent.
Definition Entity.h:47
@ HEARTS
Definition Entity.h:52
@ COLLIDERS
Fixed collider entity, e.g. a wall.
Definition Entity.h:50
@ PLAYERS
Primary entity in player controll.
Definition Entity.h:48
@ ENEMIES
Definition Entity.h:49
bool hasComponent() const
Definition Entity.h:84
Entity(Manager &mManager)
Definition Entity.h:59
Manager & getManager()
Definition Entity.h:82
bool hasGroup(Group mGroup)
Definition Entity.cpp:14
T & getComponent() const
<
Definition Entity.h:105
std::bitset< MAX_GROUPS > getGroupBitSet()
Definition Entity.cpp:30
void delGroup(Group mGroup)
Definition Entity.cpp:25
T & addComponent(TArgs &&...mArgs)
Adds specified type as component and calls Component::init()
Definition Entity.h:91
bool isActive() const
Definition Entity.h:64
void update(uint_fast16_t diffTime) const
Call each frame to update all components.
Definition Entity.cpp:7
Is responsible for managing all entities.
Definition Manager.h:23