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:
47
51 explicit Entity(Manager& mManager) :
52 manager(mManager) { };
53
54 void update(uint_fast16_t diffTime) const;
55
56 bool isActive() const { return this->active; }
59 void destroy() {
60 this->active = false;
62 this->getComponent<ColliderComponent>().removeCollision();
63 }
64 }
65#ifndef DOXYGEN_SHOULD_SKIP_THIS
66 bool hasGroup(Group mGroup);
67 void addGroup(Group mGroup);
68 void delGroup(Group mGroup);
69 std::bitset<MAX_GROUPS> getGroupBitSet();
70#endif /* DOXYGEN_SHOULD_SKIP_THIS */
71
73 Manager& getManager() { return manager; };
74
75 template <typename T> bool hasComponent() const
76 {
77 return componentBitSet[getComponentTypeID<T>()];
78 }
79
83 template <typename T, typename...TArgs> T& addComponent(TArgs&&...mArgs)
84 {
85 T* c(new T(std::forward<TArgs>(mArgs)...));
86 c->entity = this;
87 std::shared_ptr<Component> uPtr{ c };
88 this->components.at(getComponentTypeID<T>()) = std::move(uPtr);
89
90 componentArray[getComponentTypeID<T>()] = c;
91 componentBitSet[getComponentTypeID<T>()] = true;
92
93 c->init();
94 return *c;
95 };
96
100 template <typename T> T& getComponent() const
101 {
102 auto ptr(componentArray[getComponentTypeID<T>()]);
103 return *static_cast<T*>(ptr);
104 }
105
108 template <typename T> std::shared_ptr<T> getComponentAsPointer() const
109 {
110 return std::static_pointer_cast<T>(components.at(getComponentTypeID<T>()));
111 }
112
113private:
114 Manager& manager;
115 bool active = true;
116 std::array<std::shared_ptr<Component>, MAX_COMPONENTS> components;
117
118 ComponentArray componentArray = {};
119 ComponentBitSet componentBitSet;
120 GroupBitSet groupBitSet;
121};
void destroy()
Definition Entity.h:59
GroupLabel
Some premade Entity groups used to avoid checking all entities for everything all of the time.
Definition Entity.h:39
@ PROJECTILE
Not used.
Definition Entity.h:43
@ POWERUPS
Not used.
Definition Entity.h:45
@ MAPTILES
Entity using TileComponent, internal use only.
Definition Entity.h:40
@ HEARTS
Not used.
Definition Entity.h:44
@ COLLIDERS
Fixed collider entity, e.g. a wall.
Definition Entity.h:42
@ PLAYERS
Primary entity in player control, used to be able to interact with pickupables.
Definition Entity.h:41
bool hasComponent() const
Definition Entity.h:75
Entity(Manager &mManager)
Definition Entity.h:51
Manager & getManager()
Definition Entity.h:73
T & getComponent() const
Access a specific component of an entity.
Definition Entity.h:100
T & addComponent(TArgs &&...mArgs)
Adds specified type as component and calls its CONSTRUCTOR.
Definition Entity.h:83
bool isActive() const
Definition Entity.h:56
std::shared_ptr< T > getComponentAsPointer() const
Access a specific component of an entity as a pointer.
Definition Entity.h:108
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