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);
38 void clearUpParticles();
39 void clearUpPhysics();
41 /// ***** Private Variables *****
43 typedef std::set<Particle*> setPart;
44 setPart particles_To_Add;
45 setPart active_Particles;
46 setPart particles_To_Remove;
48 typedef std::set<PhysicsEntity*> setPhys;
49 setPhys physics_To_Add;
50 setPhys active_Physics;
51 setPhys physics_To_Remove;
53 Mutex particleSetLock;
54 Mutex physicsEntitySetLock;
56 /// ***** Initializers/Cleaners *****
60 particleSetLock.init();
61 physicsEntitySetLock.init();
69 physicsEntitySetLock.clean();
70 particleSetLock.clean();
73 /// ***** Public Methods *****
75 void manager::add(Entity* e)
78 Particle* p = dynamic_cast<Particle*>(e);
81 particles_To_Add.insert(p);
87 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
90 physics_To_Add.insert(p);
95 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
97 void manager::remove(Entity* e)
100 Autolock lock( particleSetLock );
101 Particle* p = dynamic_cast<Particle*>(e);
104 particles_To_Remove.insert(p);
110 Autolock lock( physicsEntitySetLock );
111 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
114 physics_To_Remove.insert(p);
119 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
122 void manager::handleInput()
124 effect::handleInput();
126 void manager::update(float time_step)
128 effect::update(time_step);
130 updateParticles(time_step);
131 updatePhysics(time_step);
137 Autolock lock( particleSetLock );
139 // update active Particle*s
140 for( setPart::iterator it = active_Particles.begin();
141 it != active_Particles.end();
150 Autolock lock( physicsEntitySetLock );
152 // update active PhysicsEntity*s
153 for( setPhys::iterator it = active_Physics.begin();
154 it != active_Physics.end();
162 /// ***** Private Methods *****
164 void updateParticles(float time_step)
168 // update active Particle*s
169 for( setPart::iterator it = active_Particles.begin();
170 it != active_Particles.end();
173 (*it)->update(time_step);
176 void updatePhysics(float time_step)
180 // apply collision math
181 collision::update(active_Physics);
183 // update active PhysicsEntity*s
184 for( setPhys::iterator it = active_Physics.begin();
185 it != active_Physics.end();
188 (*it)->update(time_step);
191 void clearUpParticles()
193 Autolock lock( particleSetLock );
195 // add new Particle*s to Active
196 for( setPart::iterator it = particles_To_Add.begin();
197 it != particles_To_Add.end();
200 active_Particles.insert(*it);
202 particles_To_Add.clear();
204 // remove dead Particle*s from Active
205 for( setPart::iterator it = particles_To_Remove.begin();
206 it != particles_To_Remove.end();
209 active_Particles.erase(*it);
211 particles_To_Remove.clear();
213 void clearUpPhysics()
215 Autolock lock( physicsEntitySetLock );
217 // add new PhysicsEntity*s to Active
218 for( setPhys::iterator it = physics_To_Add.begin();
219 it != physics_To_Add.end();
222 active_Physics.insert(*it);
224 physics_To_Add.clear();
226 // remove dead PhysicsEntity*s from Active
227 for( setPhys::iterator it = physics_To_Remove.begin();
228 it != physics_To_Remove.end();
231 active_Physics.erase(*it);
233 physics_To_Remove.clear();