2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "entityManager.h"
20 #include <bear/debug.h>
21 #include <bear/Lock.h>
22 #include <bear/Autolock.h>
28 #include "Entities/Entity.h"
29 #include "Entities/Particle.h"
30 #include "Entities/PhysicsEntity.h"
32 #include "collisionManager.h"
33 #include "effectManager.h"
35 /// ***** Private Method Headers *****
37 static void updateParticles(float);
38 static void updatePhysics(float);
39 static void addOrRemoveParticles();
40 static void addOrRemovePhysics();
42 /// ***** Private Variables *****
44 typedef std::set<Particle*> setPart;
45 static setPart particles_To_Add;
46 static setPart active_Particles;
47 static setPart particles_To_Remove;
49 typedef std::set<PhysicsEntity*> setPhys;
50 static setPhys physics_To_Add;
51 static setPhys active_Physics;
52 static setPhys physics_To_Remove;
54 static Lock muSetPart;
55 static Lock muSetPhys;
57 /// ***** Initializers/Cleaners *****
74 /// ***** Public Methods *****
76 void manager::add(Entity* pe)
81 Particle* pp = dynamic_cast<Particle*>(pe);
84 particles_To_Add.insert(pp);
90 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
93 physics_To_Add.insert(ppe);
98 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
100 void manager::remove(Entity* pe)
105 Autolock lock( &muSetPart );
106 Particle* p = dynamic_cast<Particle*>(pe);
109 particles_To_Remove.insert(p);
115 Autolock lock( &muSetPhys );
116 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
119 physics_To_Remove.insert(ppe);
124 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
127 void manager::handleInput()
129 effect::handleInput();
131 void manager::update(float time_step)
133 effect::update(time_step);
135 updateParticles(time_step);
136 updatePhysics(time_step);
141 Autolock lock( &muSetPart );
143 addOrRemoveParticles();
145 // draw active Particle*s
146 for( setPart::iterator it = active_Particles.begin();
147 it != active_Particles.end();
155 Autolock lock( &muSetPhys );
157 addOrRemovePhysics();
159 // draw active PhysicsEntity*s
160 for( setPhys::iterator it = active_Physics.begin();
161 it != active_Physics.end();
169 /// ***** Private Methods *****
171 void updateParticles(float time_step)
173 addOrRemoveParticles();
175 // update active Particle*s
176 for( setPart::iterator it = active_Particles.begin();
177 it != active_Particles.end();
180 (*it)->update(time_step);
183 void updatePhysics(float time_step)
185 addOrRemovePhysics();
187 // apply collision math
188 collision::update(active_Physics);
190 // update active PhysicsEntity*s
191 for( setPhys::iterator it = active_Physics.begin();
192 it != active_Physics.end();
195 (*it)->update(time_step);
198 void addOrRemoveParticles()
200 Autolock lock( &muSetPart );
202 // add new Particle*s to Active
203 for( setPart::iterator it = particles_To_Add.begin();
204 it != particles_To_Add.end();
207 active_Particles.insert(*it);
209 particles_To_Add.clear();
211 // remove dead Particle*s from Active
212 for( setPart::iterator it = particles_To_Remove.begin();
213 it != particles_To_Remove.end();
216 active_Particles.erase(*it);
218 particles_To_Remove.clear();
220 void addOrRemovePhysics()
222 Autolock lock( &muSetPhys );
224 // add new PhysicsEntity*s to Active
225 for( setPhys::iterator it = physics_To_Add.begin();
226 it != physics_To_Add.end();
229 active_Physics.insert(*it);
231 physics_To_Add.clear();
233 // remove dead PhysicsEntity*s from Active
234 for( setPhys::iterator it = physics_To_Remove.begin();
235 it != physics_To_Remove.end();
238 active_Physics.erase(*it);
240 physics_To_Remove.clear();