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
45 enum class GroupLabel
46 {
47 MAPTILES,
48 PLAYERS,
49 ENEMIES,
50 COLLIDERS,
52 HEARTS,
54 };
55
59 explicit Entity(Manager& mManager) :
60 manager(mManager) { };
61
62 void update() 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::unique_ptr<Component> uPtr{ c };
96 this->components.emplace_back(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
111private:
112 Manager& manager;
113 bool active = true;
114 std::vector<std::unique_ptr<Component>> components;
115
116 ComponentArray componentArray = {};
117 ComponentBitSet componentBitSet;
118 GroupBitSet groupBitSet;
119};
Definition Component.h:6
Main class for any object in game, stores associations, labeling and components.
Definition Entity.h:35
void destroy()
Definition Entity.h:67
void addGroup(Group mGroup)
Definition Entity.cpp:17
GroupLabel
Used for rendering order (last is highest) or retrieving entities of group.
Definition Entity.h:46
@ MAPTILES
Entity using TileComponent.
@ COLLIDERS
Fixed collider entity, e.g. a wall.
@ PLAYERS
Primary entity in player controll.
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:12
T & getComponent() const
Definition Entity.h:105
std::bitset< MAX_GROUPS > getGroupBitSet()
Definition Entity.cpp:28
void delGroup(Group mGroup)
Definition Entity.cpp:23
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() const
Call each frame to update all components.
Definition Entity.cpp:7
Is responsible for managing all entities.
Definition Manager.h:23