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"
23 #include "Entities/Entity.h"
24 #include "Entities/Particle.h"
25 #include "Entities/PhysicsEntity.h"
27 #include "collisionHandler.h"
29 /// ***** Private Method Headers *****
31 void updateParticles(float);
32 void updatePhysics(float);
34 /// ***** Private Variables *****
36 typedef std::set<Particle*> setPart;
37 setPart particles_To_Add;
38 setPart active_Particles;
39 setPart particles_To_Remove;
41 typedef std::set<PhysicsEntity*> setPhys;
42 setPhys physics_To_Add;
43 setPhys active_Physics;
44 setPhys physics_To_Remove;
46 /// ***** Initializers/Cleaners *****
57 /// ***** Public Methods *****
59 void manager::add(Entity* e)
62 Particle* p = dynamic_cast<Particle*>(e);
65 particles_To_Add.insert(p);
71 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
74 physics_To_Add.insert(p);
80 cerr << "ENTITY TYPE NOT SUPPORTED BY addEntity()!!" << endl;
83 void manager::remove(Entity* e)
86 Particle* p = dynamic_cast<Particle*>(e);
89 particles_To_Remove.insert(p);
95 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
98 physics_To_Remove.insert(p);
104 cerr << "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!" << endl;
108 void manager::handleInput()
112 void manager::update(float time_step)
114 updateParticles(time_step);
115 updatePhysics(time_step);
119 // update active Particle*s
120 for( setPart::iterator it = active_Particles.begin();
121 it != active_Particles.end();
127 // update active PhysicsEntity*s
128 for( setPhys::iterator it = active_Physics.begin();
129 it != active_Physics.end();
136 /// ***** Private Methods *****
138 void updateParticles(float time_step)
140 // add new Particle*s to Active
141 for( setPart::iterator it = particles_To_Add.begin();
142 it != particles_To_Add.end();
145 active_Particles.insert(*it);
147 particles_To_Add.clear();
149 // remove dead Particle*s from Active
150 for( setPart::iterator it = particles_To_Remove.begin();
151 it != particles_To_Remove.end();
154 active_Particles.erase(*it);
156 particles_To_Remove.clear();
158 // update active Particle*s
159 for( setPart::iterator it = active_Particles.begin();
160 it != active_Particles.end();
163 (*it)->update(time_step);
166 void updatePhysics(float time_step)
168 // add new PhysicsEntity*s to Active
169 for( setPhys::iterator it = physics_To_Add.begin();
170 it != physics_To_Add.end();
173 active_Physics.insert(*it);
175 physics_To_Add.clear();
177 // remove dead PhysicsEntity*s from Active
178 for( setPhys::iterator it = physics_To_Remove.begin();
179 it != physics_To_Remove.end();
182 active_Physics.erase(*it);
184 physics_To_Remove.clear();
186 // apply collision math
187 collision::update(active_Physics, time_step);
189 // update active PhysicsEntity*s
190 for( setPhys::iterator it = active_Physics.begin();
191 it != active_Physics.end();
194 (*it)->update(time_step);