Commit | Line | Data |
---|---|---|
ad9f1fb6 PG |
1 | #include <set> |
2 | using std::set; | |
3 | ||
4 | #include <iostream> | |
5 | ||
054d658f | 6 | #include "entityManager.h" |
a483ed75 PG |
7 | #include "debug.h" |
8 | ||
054d658f PG |
9 | #include "Entities/Entity.h" |
10 | #include "Entities/Particle.h" | |
11 | #include "Entities/PhysicsEntity.h" | |
12 | ||
ad9f1fb6 PG |
13 | /// ***** Private Method Headers ***** |
14 | void updateParticles(float); | |
15 | void updatePhysics(float); | |
16 | ||
17 | /// ***** Private Variables ***** | |
18 | typedef set<Particle*> setPart; | |
19 | setPart particles_To_Add; | |
20 | setPart active_Particles; | |
21 | setPart particles_To_Remove; | |
22 | bool clearParticles; | |
23 | ||
24 | typedef set<PhysicsEntity*> setPhys; | |
25 | setPhys physics_To_Add; | |
26 | setPhys active_Physics; | |
27 | setPhys physics_To_Remove; | |
28 | bool clearPhysics; | |
29 | ||
30 | /// ***** Public Methods ***** | |
31 | ||
d388f0ec | 32 | void manager::init() |
ad9f1fb6 PG |
33 | { |
34 | } | |
d388f0ec | 35 | void manager::clean() |
ad9f1fb6 PG |
36 | { |
37 | } | |
38 | ||
d388f0ec | 39 | void manager::add(Entity* e) |
ad9f1fb6 PG |
40 | { |
41 | { | |
d388f0ec PG |
42 | Particle* p = dynamic_cast<Particle*>(e); |
43 | if( p != 0 ) | |
44 | { | |
45 | particles_To_Add.insert(p); | |
46 | return; | |
47 | } | |
ad9f1fb6 PG |
48 | } |
49 | ||
50 | { | |
d388f0ec PG |
51 | PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e); |
52 | if( p != 0 ) | |
53 | { | |
54 | physics_To_Add.insert(p); | |
55 | return; | |
56 | } | |
ad9f1fb6 PG |
57 | } |
58 | ||
59 | std::cerr << "ENTITY TYPE NOT SUPPORTED BY addEntity()!!"; | |
60 | std::cerr << std::endl; | |
61 | } | |
d388f0ec | 62 | void manager::remove(Entity* e) |
ad9f1fb6 PG |
63 | { |
64 | { | |
d388f0ec PG |
65 | Particle* p = dynamic_cast<Particle*>(e); |
66 | if( p != 0 ) | |
67 | { | |
68 | particles_To_Remove.insert(p); | |
69 | return; | |
70 | } | |
ad9f1fb6 PG |
71 | } |
72 | ||
73 | { | |
d388f0ec PG |
74 | PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e); |
75 | if( p != 0 ) | |
76 | { | |
77 | physics_To_Remove.insert(p); | |
78 | return; | |
79 | } | |
ad9f1fb6 PG |
80 | } |
81 | ||
82 | std::cerr << "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!"; | |
83 | std::cerr << std::endl; | |
84 | } | |
85 | ||
d388f0ec | 86 | void manager::clear() |
ad9f1fb6 PG |
87 | { |
88 | clearParticles = true; | |
89 | clearPhysics = true; | |
90 | } | |
91 | ||
d388f0ec | 92 | void manager::handleInput() |
ad9f1fb6 PG |
93 | { |
94 | // TODO | |
95 | } | |
d388f0ec | 96 | void manager::update(float time_step) |
ad9f1fb6 PG |
97 | { |
98 | updateParticles(time_step); | |
99 | updatePhysics(time_step); | |
100 | } | |
d388f0ec | 101 | void manager::draw() |
ad9f1fb6 PG |
102 | { |
103 | // update active Particle*s | |
104 | for( setPart::iterator it = active_Particles.begin(); | |
105 | it != active_Particles.end(); | |
106 | it++ ) | |
107 | { | |
108 | (*it)->draw(); | |
109 | } | |
110 | ||
111 | // update active PhysicsEntity*s | |
112 | for( setPhys::iterator it = active_Physics.begin(); | |
113 | it != active_Physics.end(); | |
114 | it++ ) | |
115 | { | |
116 | (*it)->draw(); | |
117 | } | |
a483ed75 | 118 | |
ad9f1fb6 PG |
119 | } |
120 | ||
121 | /// ***** Private Methods ***** | |
122 | void updateParticles(float time_step) | |
123 | { | |
124 | // add new Particle*s to Active | |
125 | for( setPart::iterator it = particles_To_Add.begin(); | |
126 | it != particles_To_Add.end(); | |
127 | it++ ) | |
128 | { | |
129 | active_Particles.insert(*it); | |
130 | } | |
131 | particles_To_Add.clear(); | |
132 | ||
133 | // remove dead Particle*s from Active | |
134 | for( setPart::iterator it = particles_To_Remove.begin(); | |
135 | it != particles_To_Remove.end(); | |
136 | it++ ) | |
137 | { | |
138 | active_Particles.erase(*it); | |
139 | } | |
140 | particles_To_Remove.clear(); | |
141 | ||
142 | // update active Particle*s | |
143 | for( setPart::iterator it = active_Particles.begin(); | |
144 | it != active_Particles.end(); | |
145 | it++ ) | |
146 | { | |
147 | (*it)->update(time_step); | |
148 | } | |
149 | } | |
150 | void updatePhysics(float time_step) | |
151 | { | |
152 | // add new PhysicsEntity*s to Active | |
153 | for( setPhys::iterator it = physics_To_Add.begin(); | |
154 | it != physics_To_Add.end(); | |
155 | it++ ) | |
156 | { | |
157 | active_Physics.insert(*it); | |
158 | } | |
159 | physics_To_Add.clear(); | |
160 | ||
161 | // remove dead PhysicsEntity*s from Active | |
162 | for( setPhys::iterator it = physics_To_Remove.begin(); | |
163 | it != physics_To_Remove.end(); | |
164 | it++ ) | |
165 | { | |
166 | active_Physics.erase(*it); | |
167 | } | |
168 | physics_To_Remove.clear(); | |
169 | ||
170 | // update active PhysicsEntity*s | |
171 | for( setPhys::iterator it = active_Physics.begin(); | |
172 | it != active_Physics.end(); | |
173 | it++ ) | |
174 | { | |
175 | (*it)->update(time_step); | |
176 | } | |
177 | } |