-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjectPool.h
More file actions
98 lines (86 loc) · 1.86 KB
/
GameObjectPool.h
File metadata and controls
98 lines (86 loc) · 1.86 KB
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
#pragma once
#include "Container.h"
#include <array>
/*
*
*/
template<typename T, int SIZE>
class GameObjectPool: public Container {
public:
GameObjectPool(SDLGame* game);
virtual ~GameObjectPool();
virtual void handleInput(Uint32 time);
virtual void update(Uint32 time);
virtual void render(Uint32 time);
T* getUnusedObject();
const vector<T*>& getAllObjects();
void deactiveAllObjects();
private:
array<T, SIZE> objs_;
vector<T*> objsPointers_;
};
template<typename T, int SIZE>
inline GameObjectPool<T, SIZE>::GameObjectPool(SDLGame* game) :
Container(game), objs_() {
for (auto &o : objs_) {
o.setActive(false);
o.setGame(game);
o.init();
objsPointers_.push_back(&o);
}
}
template<typename T, int SIZE>
inline GameObjectPool<T, SIZE>::~GameObjectPool() {
}
template<typename T, int SIZE>
inline void GameObjectPool<T, SIZE>::handleInput(Uint32 time) {
Container::handleInput(time);
if (!isActive())
return;
for (auto &o : objs_) {
if (o.isActive()) {
o.handleInput(time);
}
}
}
template<typename T, int SIZE>
inline void GameObjectPool<T, SIZE>::update(Uint32 time) {
Container::update(time);
if (!isActive())
return;
for (auto &o : objs_) {
if (o.isActive()) {
o.update(time);
}
}
}
template<typename T, int SIZE>
inline void GameObjectPool<T, SIZE>::render(Uint32 time) {
Container::render(time);
if (!isActive())
return;
for (auto &o : objs_) {
if (o.isActive()) {
o.render(time);
}
}
}
template<typename T, int SIZE>
inline T* GameObjectPool<T, SIZE>::getUnusedObject() {
for (auto &o : objs_) {
if (!o.isActive()) {
return &o;
}
}
return nullptr;
}
template<typename T, int SIZE>
inline const vector<T*>& GameObjectPool<T,SIZE>::getAllObjects() {
return objsPointers_;
}
template<typename T, int SIZE>
inline void GameObjectPool<T,SIZE>::deactiveAllObjects() {
for (auto &o : objs_) {
o.setActive(false);
}
}