aboutsummaryrefslogtreecommitdiffstats
path: root/entities.hpp
blob: 3580d65d627513dc6b59bd1d708ebc787e5da310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
 * @file entities.hpp
 * @brief A simply-written entity component system.
 */
#ifndef ENTITIES_HPP_
#define ENTITIES_HPP_

#include <algorithm> // std::find_if
#include <type_traits> // std::is_convertible
//#include "pvector.hpp"
#include <vector>

#define Container std::vector

/**
 * @class Component
 * A base class for all components to inherit.
 */
class Component {
	/**
	 * Constructs the class using the given XML element(s).
	 */
	virtual void fromXML(/*TODO*/) {}
};

/** An ID number for entities. */
using Id = unsigned int;

/**
 * @struct EntityData
 * Contains all components and an ID, to form an entity.
 */
struct EntityData {
	/** The entity's ID. */
	Id id;

	/** A vector of all components. */
	Container<Component*> components;

	/** Constructs an entity with the given ID. */
	EntityData(Id _id = -1)
		: id (_id) {}

	/** Compares two entities through their IDs. */
	bool operator==(const EntityData& e) {
		return id == e.id;
	}
};

/**
 * @struct Entity
 * Allows access to an entity and it's components.
 * Note that this is not the actual entity's data, that is stored in the
 * EntityManager's EntityData array.
 */
struct Entity {
	Container<EntityData>::iterator pos;

	/** Constructs an entity object to handle the given entity data. */
	Entity(Container<EntityData>::iterator p)
		:  pos(p) {}
	Entity(EntityData& d)
		: pos(&d) {}

	/**
	 * Assigns a component to the entity.
	 * @param args arguments to pass to the component's constructor.
	 * @return a pointer to the new component
	 */
	template<class T, typename... Args>
	T* assign(Args... args) {
		static_assert(std::is_convertible<T*, Component*>::value,
			"components must inherit Component base class");
		auto comp = new T(args...);
		(*pos).components.push_back(comp);
		return comp;
	}

	/**
	 * Removes a component of the given type from the entity.
	 */
	template<class T>
	void remove(void) {
		static_assert(std::is_convertible<T*, Component*>::value,
			"components must inherit Component base class");
		auto c = std::find_if((*pos).components.begin(), (*pos).components.end(),
			[](auto c){ return dynamic_cast<T*>(c); });
		if (c != (*pos).components.end())
			(*pos).components.erase(c);
	}

	/**
	 * Tests if the entity has a component of the given type.
	 * @return true if the entity has the component
	 */
	template<class T>
	bool hasComponent(void) const {
		static_assert(std::is_convertible<T*, Component*>::value,
			"components must inherit Component base class");
		auto c = std::find_if((*pos).components.begin(), (*pos).components.end(),
			[](auto c){ return dynamic_cast<T*>(c); });
		return c != (*pos).components.end();
	}

	/**
	 * Fetches a component from the entity.
	 * @return the component, nullptr if the entity does not have it
	 */
	template<class T>
	T* component(void) {
		static_assert(std::is_convertible<T*, Component*>::value,
			"components must inherit Component base class");
		auto c = std::find_if((*pos).components.begin(), (*pos).components.end(),
			[](auto c){ return dynamic_cast<T*>(c); });
		return (c != (*pos).components.end()) ? dynamic_cast<T*>(*c) : nullptr;
	}
};

/**
 * @class EntityManager
 * Manages a group of entities.
 */
class EntityManager {
private:
	/** The array of all entities. */
	Container<EntityData> entities;

public:
	// max is not enforced
	EntityManager()
		/*: entities()*/ {}

	~EntityManager(void) {
		entities.clear();
	}

	/**
	 * Creates a new entity.
	 * @return an Entity object for the new entity
	 */
	Entity create(void) {
		static Id newId = 0;

		entities.emplace_back(newId);
		return Entity(entities.begin() + entities.size() - 1);
	}

	/**
	 * Kills (removes) an entity.
	 * @param e the entity to remove
	 */
	void kill(const Entity& e) {
		if (e.pos > entities.begin() && e.pos < entities.end())
			entities.erase(e.pos);
	}

	/**
	 * Destroys all entities.
	 */
	void reset(void) {
		entities.clear();
	}

	/**
	 * Runs a function through all entities.
	 * @param f the function to run through
	 */
	void each(std::function<void(Entity e)> f) {
		for (auto i = entities.begin(); i < entities.end(); ++i)
			f(Entity(i));
	}

	template<class T1>
	void trySort(void) {
		static unsigned int oldSize = 0;
		if (entities.size() < 100000 && entities.size() != oldSize) {
			oldSize = entities.size();

			std::sort(entities.begin(), entities.end(),
			[](auto&& e1, auto&& e2){
					return Entity(e1).hasComponent<T1>() && !Entity(e2).hasComponent<T1>();
			});
		}
	}

	/**
	 * Runs a function through all entities with the given components.
	 * @param f the function to run through
	 */
	template<class T1>
	void each(std::function<void(Entity e)> f) {
		trySort<T1>();
		bool good = false;
		for (auto i = entities.begin(); i < entities.end(); ++i) {
			Entity en (i);
			if (en.hasComponent<T1>()) {
				f(en);
				good = true;
			} else if (good) break;
		}
	}

	template<class T1, class T2>
	void each(std::function<void(Entity e)> f) {
		trySort<T1>();
		bool good = false;
		for (auto i = entities.begin(); i < entities.end(); ++i) {
			Entity en (i);
			if (en.hasComponent<T1>() && en.hasComponent<T2>()) {
				f(en);
				good = true;
			} else if (good) break;
		}
	}
};



using DeltaTime = int;

class System {
public:
	virtual void update(EntityManager& em, DeltaTime dt) = 0;
};

class SystemManager {
private:
	Container<System*> systems;
	EntityManager& entities;

public:
	SystemManager(EntityManager& em)
		: entities(em) {}

	template<class T, typename... Args>
	void add(Args... args) {
		static_assert(std::is_convertible<T*, System*>::value,
			"systems must inherit System base class");
		systems.push_back(new T(args...));
	}

	template<class T>
	void update(DeltaTime dt) {
		static_assert(std::is_convertible<T*, System*>::value,
			"systems must inherit System base class");
		for (auto s : systems) {
			if (dynamic_cast<T*>(s)) {
				s->update(entities, dt);
				return;
			}
		}
	}
};

#endif // ENTITIES_HPP_