-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainer.h
More file actions
112 lines (84 loc) · 2.46 KB
/
Container.h
File metadata and controls
112 lines (84 loc) · 2.46 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once
#include "GameObject.h"
class Component;
class InputComponent;
class PhysicsComponent;
class GraphicsComponent;
/*
*
*/
class Container: public GameObject {
public:
Container();
Container(SDLGame* game);
virtual ~Container();
virtual void handleInput(Uint32 time);
virtual void update(Uint32 time);
virtual void render(Uint32 time);
void addC(InputComponent* ic);
void addC(PhysicsComponent* pc);
void addC(GraphicsComponent* gc);
void addOC(Component* oc); // add observer component, just to receive messages
bool hasC(InputComponent* ic);
bool hasC(PhysicsComponent* pc);
bool hasC(GraphicsComponent* gc);
bool hasOC(Component* oc);
void removeC(InputComponent* ic);
void removeC(PhysicsComponent* pc);
void removeC(GraphicsComponent* gc);
void removeOC(Component* oc);
void reset(); // removes all components
virtual void receive(const void* senderObj, const msg::Message& msg);
// used by components to send message at the level of container
void localSend(const void* senderObj, const msg::Message& msg);
// this will be used by components to send message globally
void globalSend(const void* senderObj, const msg::Message& msg);
private:
void broadcastToLocalComponents(const void* senderObj, const msg::Message& msg);
template<typename T>
struct wrapper {
bool active;
T* data;
bool operator==(const wrapper<T>& other) const {
return other.data == data;
}
};
template<typename T>
void addC(vector<wrapper<T>> &v, T* c);
template<typename T>
bool hasC(vector<wrapper<T>> &v, T* c);
template<typename T>
void removeC(vector<wrapper<T>> &v, T* c);
vector<wrapper<InputComponent>> ic_;
vector<wrapper<PhysicsComponent>> pc_;
vector<wrapper<GraphicsComponent>> gc_;
vector<wrapper<Component>> oc_;
};
template<typename T>
inline void Container::addC(vector<wrapper<T>> &v, T* c) {
if (c == nullptr)
return;
wrapper<T> r = { true, c };
auto position = std::find(v.begin(), v.end(), r);
if (position != v.end()) {
(*position).active = true;
} else {
v.push_back(r);
}
}
template<typename T>
inline bool Container::hasC(vector<wrapper<T>> &v, T* c) {
wrapper<T> r = { true, c };
auto position = std::find(v.begin(), v.end(), r);
return position != v.end();
}
template<typename T>
inline void Container::removeC(vector<wrapper<T>> &v, T* c) {
if (c == nullptr)
return;
wrapper<T> r = { true, c };
auto position = std::find(v.begin(), v.end(), r);
if (position != v.end()) {
(*position).active = false;
}
}