SDL Minigame  1.0
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
12class Manager;
13class Component;
14
15using ComponentBitSet = std::bitset<MAX_COMPONENTS>;
16using GroupBitSet = std::bitset<MAX_GROUPS>;
17using ComponentArray = std::array<Component*, MAX_COMPONENTS>;
18
28class Entity
29{
30public:
31
39 enum class GroupLabel
40 {
41 MAPTILES,
42 PLAYERS,
43 ENEMIES,
44 COLLIDERS,
46 HEARTS,
48 };
49
53 enum class TeamLabel
54 {
55 NONE,
56 BLUE,
57 RED
58 };
59
63 explicit Entity(Manager& mManager) :
64 manager(mManager) { };
65
66 void update() const;
69 void draw() const;
70
71 bool isActive() const { return this->active; }
74 void destroy() {
75 this->active = false;
77 this->getComponent<ColliderComponent>().removeCollision();
78 }
79 }
80
81 bool hasGroup(Group mGroup);
82 void addGroup(Group mGroup);
83 void delGroup(Group mGroup);
86 std::bitset<MAX_GROUPS> getGroupBitSet();
87
88 void setTeam(TeamLabel teamLabel);
90
92 Manager& getManager() { return manager; };
93
94 template <typename T> bool hasComponent() const
95 {
96 return componentBitSet[getComponentTypeID<T>()];
97 }
98
101 template <typename T, typename...TArgs> T& addComponent(TArgs&&...mArgs)
102 {
103 T* c(new T(std::forward<TArgs>(mArgs)...));
104 c->entity = this;
105 std::unique_ptr<Component> uPtr{ c };
106 this->components.emplace_back(std::move(uPtr));
107
108 componentArray[getComponentTypeID<T>()] = c;
109 componentBitSet[getComponentTypeID<T>()] = true;
110
111 c->init();
112 return *c;
113 };
114
115 template <typename T> T& getComponent() const
116 {
117 auto ptr(componentArray[getComponentTypeID<T>()]);
118 return *static_cast<T*>(ptr);
119 }
120
121private:
122 Manager& manager;
123 bool active = true;
124 std::vector<std::unique_ptr<Component>> components;
125
126 ComponentArray componentArray = {};
127 ComponentBitSet componentBitSet;
128 GroupBitSet groupBitSet;
129 TeamLabel teamLabel;
130};
Definition Component.h:6
Main class for any object in game, stores associations, labeling and components.
Definition Entity.h:29
void destroy()
Definition Entity.h:74
void addGroup(Group mGroup)
Definition Entity.cpp:22
GroupLabel
Used for rendering order (last is highest) or retrieving entities of group.
Definition Entity.h:40
@ MAPTILES
Entity using TileComponent.
@ COLLIDERS
Fixed collider entity, e.g. a wall.
@ PLAYERS
Primary entity in player controll.
bool hasComponent() const
Definition Entity.h:94
Entity(Manager &mManager)
Definition Entity.h:63
TeamLabel
Allows grouping entities by team association for hits, win conditions, etc.
Definition Entity.h:54
@ BLUE
Team blue.
@ NONE
No team, should be skipped in any checks.
Manager & getManager()
Definition Entity.h:92
bool hasGroup(Group mGroup)
Definition Entity.cpp:17
T & getComponent() const
Definition Entity.h:115
std::bitset< MAX_GROUPS > getGroupBitSet()
Definition Entity.cpp:33
void setTeam(TeamLabel teamLabel)
Definition Entity.cpp:38
void delGroup(Group mGroup)
Definition Entity.cpp:28
T & addComponent(TArgs &&...mArgs)
Adds specified type as component and calls Component::init()
Definition Entity.h:101
void draw() const
Definition Entity.cpp:12
bool isActive() const
Definition Entity.h:71
TeamLabel getTeam()
Definition Entity.cpp:44
void update() const
Definition Entity.cpp:7
Is responsible for managing all entities.
Definition Manager.h:20