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"
24 #include "locks/Mutex.h"
25 #include "locks/Autolock.h"
27 #include "Entities/Entity.h"
28 #include "Entities/Particle.h"
29 #include "Entities/PhysicsEntity.h"
31 #include "collisionManager.h"
32 #include "effectManager.h"
34 /// ***** Private Method Headers *****
36 static void updateParticles(float);
37 static void updatePhysics(float);
38 static void addOrRemoveParticles();
39 static void addOrRemovePhysics();
41 /// ***** Private Variables *****
43 typedef std::set<Particle*> setPart;
44 static setPart particles_To_Add;
45 static setPart active_Particles;
46 static setPart particles_To_Remove;
48 typedef std::set<PhysicsEntity*> setPhys;
49 static setPhys physics_To_Add;
50 static setPhys active_Physics;
51 static setPhys physics_To_Remove;
53 static Mutex muSetPart;
54 static Mutex muSetPhys;
56 /// ***** Initializers/Cleaners *****
73 /// ***** Public Methods *****
75 void manager::add(Entity* pe)
80 Particle* pp = dynamic_cast<Particle*>(pe);
83 particles_To_Add.insert(pp);
89 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
92 physics_To_Add.insert(ppe);
97 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
99 void manager::remove(Entity* pe)
104 Autolock lock( muSetPart );
105 Particle* p = dynamic_cast<Particle*>(pe);
108 particles_To_Remove.insert(p);
114 Autolock lock( muSetPhys );
115 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
118 physics_To_Remove.insert(ppe);
123 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
126 void manager::handleInput()
128 effect::handleInput();
130 void manager::update(float time_step)
132 effect::update(time_step);
134 updateParticles(time_step);
135 updatePhysics(time_step);
140 Autolock lock( muSetPart );
142 addOrRemoveParticles();
144 // draw active Particle*s
145 for( setPart::iterator it = active_Particles.begin();
146 it != active_Particles.end();
154 Autolock lock( muSetPhys );
156 addOrRemovePhysics();
158 // draw active PhysicsEntity*s
159 for( setPhys::iterator it = active_Physics.begin();
160 it != active_Physics.end();
168 /// ***** Private Methods *****
170 void updateParticles(float time_step)
172 addOrRemoveParticles();
174 // update active Particle*s
175 for( setPart::iterator it = active_Particles.begin();
176 it != active_Particles.end();
179 (*it)->update(time_step);
182 void updatePhysics(float time_step)
184 addOrRemovePhysics();
186 // apply collision math
187 collision::update(active_Physics);
189 // update active PhysicsEntity*s
190 for( setPhys::iterator it = active_Physics.begin();
191 it != active_Physics.end();
194 (*it)->update(time_step);
197 void addOrRemoveParticles()
199 Autolock lock( muSetPart );
201 // add new Particle*s to Active
202 for( setPart::iterator it = particles_To_Add.begin();
203 it != particles_To_Add.end();
206 active_Particles.insert(*it);
208 particles_To_Add.clear();
210 // remove dead Particle*s from Active
211 for( setPart::iterator it = particles_To_Remove.begin();
212 it != particles_To_Remove.end();
215 active_Particles.erase(*it);
217 particles_To_Remove.clear();
219 void addOrRemovePhysics()
221 Autolock lock( muSetPhys );
223 // add new PhysicsEntity*s to Active
224 for( setPhys::iterator it = physics_To_Add.begin();
225 it != physics_To_Add.end();
228 active_Physics.insert(*it);
230 physics_To_Add.clear();
232 // remove dead PhysicsEntity*s from Active
233 for( setPhys::iterator it = physics_To_Remove.begin();
234 it != physics_To_Remove.end();
237 active_Physics.erase(*it);
239 physics_To_Remove.clear();