-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentManager.h
76 lines (68 loc) · 1.72 KB
/
ComponentManager.h
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
#ifndef COMPONENTMANAGER_H
#define COMPONENTMANAGER_H
#include <iostream>
#include <list>
using std::list;
class Component
{
public:
Component() { deleted = false; }
virtual ~Component() {}
virtual void update() = 0;
virtual void draw() = 0;
virtual void kill() { deleted = true; }
protected:
bool deleted;
friend class ComponentManager;
};
class ComponentManager
{
public:
virtual ~ComponentManager() { clear(); }
static ComponentManager &get()
{
static ComponentManager instance;
return instance;
}
void add(Component *);
void update();
void draw();
void clear();
template <class ComponentType> int count();
template <class ComponentType> void find();
template <class ComponentType> ComponentType* next();
protected:
list<Component*> components;
list<Component*>:: iterator foundIter;
private:
ComponentManager() {}
ComponentManager(const ComponentManager& other);
ComponentManager& operator=(const ComponentManager& other);
};
template <class ComponentType> int ComponentManager::count()
{
int cnt = 0;
for (list<Component*>::iterator i = components.begin();
i!=components.end(); ++i)
{
if ( dynamic_cast<ComponentType*>(*i) != NULL )
++cnt;
}
return cnt;
}
template <class ComponentType> void ComponentManager::find()
{
foundIter = components.begin();
}
template <class ComponentType> ComponentType* ComponentManager::next()
{
while ( foundIter != components.end() )
{
ComponentType *et = dynamic_cast<ComponentType*>(*foundIter);
++foundIter;
if ( et != NULL )
return et;
}
return NULL;
}
#endif // COMPONENTMANAGER_H