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 void updateParticles(float);
37 void updatePhysics(float);
39 /// ***** Private Variables *****
41 typedef std::set<Particle*> setPart;
42 setPart particles_To_Add;
43 setPart active_Particles;
44 setPart particles_To_Remove;
46 typedef std::set<PhysicsEntity*> setPhys;
47 setPhys physics_To_Add;
48 setPhys active_Physics;
49 setPhys physics_To_Remove;
51 Mutex particleSetLock;
52 Mutex physicsEntitySetLock;
54 /// ***** Initializers/Cleaners *****
58 particleSetLock.init();
59 physicsEntitySetLock.init();
67 physicsEntitySetLock.clean();
68 particleSetLock.clean();
71 /// ***** Public Methods *****
73 void manager::add(Entity* e)
76 Particle* p = dynamic_cast<Particle*>(e);
79 particles_To_Add.insert(p);
85 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
88 physics_To_Add.insert(p);
93 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
95 void manager::remove(Entity* e)
98 Particle* p = dynamic_cast<Particle*>(e);
101 particles_To_Remove.insert(p);
107 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
110 physics_To_Remove.insert(p);
115 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
118 void manager::handleInput()
120 effect::handleInput();
122 void manager::update(float time_step)
124 effect::update(time_step);
126 updateParticles(time_step);
127 updatePhysics(time_step);
132 Autolock lock( particleSetLock );
133 DPF(0, "Particle Draw Start");
135 // update active Particle*s
136 for( setPart::iterator it = active_Particles.begin();
137 it != active_Particles.end();
143 DPF(0, "Particle Draw End");
147 Autolock lock( physicsEntitySetLock );
148 DPF(0, "Physics Draw Start");
150 // update active PhysicsEntity*s
151 for( setPhys::iterator it = active_Physics.begin();
152 it != active_Physics.end();
158 DPF(0, "Physics Draw End");
162 /// ***** Private Methods *****
164 void updateParticles(float time_step)
167 Autolock lock( particleSetLock );
169 // add new Particle*s to Active
170 for( setPart::iterator it = particles_To_Add.begin();
171 it != particles_To_Add.end();
174 DPF(0, "Particle Insert");
175 active_Particles.insert(*it);
178 // remove dead Particle*s from Active
179 for( setPart::iterator it = particles_To_Remove.begin();
180 it != particles_To_Remove.end();
183 DPF(0, "Particle Delete");
184 active_Particles.erase(*it);
188 particles_To_Add.clear();
189 particles_To_Remove.clear();
191 // update active Particle*s
192 for( setPart::iterator it = active_Particles.begin();
193 it != active_Particles.end();
196 (*it)->update(time_step);
199 void updatePhysics(float time_step)
202 Autolock lock( physicsEntitySetLock );
204 // add new PhysicsEntity*s to Active
205 for( setPhys::iterator it = physics_To_Add.begin();
206 it != physics_To_Add.end();
209 DPF(0, "Physics Insert");
210 active_Physics.insert(*it);
213 // remove dead PhysicsEntity*s from Active
214 for( setPhys::iterator it = physics_To_Remove.begin();
215 it != physics_To_Remove.end();
218 DPF(0, "Physics Delete");
219 active_Physics.erase(*it);
223 physics_To_Remove.clear();
224 physics_To_Add.clear();
226 // apply collision math
227 collision::update(active_Physics);
229 // update active PhysicsEntity*s
230 for( setPhys::iterator it = active_Physics.begin();
231 it != active_Physics.end();
234 (*it)->update(time_step);