more debug changes
[physics.git] / src / entityManager.cpp
CommitLineData
e68f847b
PG
1/*
2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
054d658f 18#include "entityManager.h"
a483ed75
PG
19#include "debug.h"
20
33b8c69b 21#include <set>
33b8c69b 22
054d658f
PG
23#include "Entities/Entity.h"
24#include "Entities/Particle.h"
25#include "Entities/PhysicsEntity.h"
26
0ac1dc80 27#include "collisionManager.h"
a823a800 28#include "effectManager.h"
54fe85c5 29
ad9f1fb6 30/// ***** Private Method Headers *****
617dcc71 31
ad9f1fb6
PG
32void updateParticles(float);
33void updatePhysics(float);
34
35/// ***** Private Variables *****
617dcc71 36
f72f4a59 37typedef std::set<Particle*> setPart;
ad9f1fb6
PG
38setPart particles_To_Add;
39setPart active_Particles;
40setPart particles_To_Remove;
ad9f1fb6 41
f72f4a59 42typedef std::set<PhysicsEntity*> setPhys;
ad9f1fb6
PG
43setPhys physics_To_Add;
44setPhys active_Physics;
45setPhys physics_To_Remove;
ad9f1fb6 46
617dcc71 47/// ***** Initializers/Cleaners *****
ad9f1fb6 48
d388f0ec 49void manager::init()
ad9f1fb6 50{
54fe85c5 51 collision::init();
ad9f1fb6 52}
d388f0ec 53void manager::clean()
ad9f1fb6 54{
54fe85c5 55 collision::clean();
ad9f1fb6
PG
56}
57
617dcc71
PG
58/// ***** Public Methods *****
59
d388f0ec 60void manager::add(Entity* e)
ad9f1fb6
PG
61{
62 {
d388f0ec
PG
63 Particle* p = dynamic_cast<Particle*>(e);
64 if( p != 0 )
65 {
66 particles_To_Add.insert(p);
67 return;
68 }
ad9f1fb6
PG
69 }
70
71 {
d388f0ec
PG
72 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
73 if( p != 0 )
74 {
75 physics_To_Add.insert(p);
76 return;
77 }
ad9f1fb6
PG
78 }
79
ca2d526e 80 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
ad9f1fb6 81}
d388f0ec 82void manager::remove(Entity* e)
ad9f1fb6
PG
83{
84 {
d388f0ec
PG
85 Particle* p = dynamic_cast<Particle*>(e);
86 if( p != 0 )
87 {
88 particles_To_Remove.insert(p);
89 return;
90 }
ad9f1fb6
PG
91 }
92
93 {
d388f0ec
PG
94 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
95 if( p != 0 )
96 {
97 physics_To_Remove.insert(p);
98 return;
99 }
ad9f1fb6
PG
100 }
101
ca2d526e 102 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
ad9f1fb6
PG
103}
104
d388f0ec 105void manager::handleInput()
ad9f1fb6 106{
a823a800 107 effect::handleInput();
ad9f1fb6 108}
d388f0ec 109void manager::update(float time_step)
ad9f1fb6 110{
a823a800
PG
111 effect::update(time_step);
112
ad9f1fb6
PG
113 updateParticles(time_step);
114 updatePhysics(time_step);
115}
d388f0ec 116void manager::draw()
ad9f1fb6
PG
117{
118 // update active Particle*s
119 for( setPart::iterator it = active_Particles.begin();
120 it != active_Particles.end();
121 it++ )
122 {
123 (*it)->draw();
124 }
125
126 // update active PhysicsEntity*s
127 for( setPhys::iterator it = active_Physics.begin();
128 it != active_Physics.end();
129 it++ )
130 {
131 (*it)->draw();
132 }
133}
134
135/// ***** Private Methods *****
617dcc71 136
ad9f1fb6
PG
137void updateParticles(float time_step)
138{
139 // add new Particle*s to Active
140 for( setPart::iterator it = particles_To_Add.begin();
141 it != particles_To_Add.end();
142 it++ )
143 {
144 active_Particles.insert(*it);
145 }
146 particles_To_Add.clear();
147
148 // remove dead Particle*s from Active
149 for( setPart::iterator it = particles_To_Remove.begin();
150 it != particles_To_Remove.end();
151 it++ )
152 {
153 active_Particles.erase(*it);
154 }
155 particles_To_Remove.clear();
156
157 // update active Particle*s
158 for( setPart::iterator it = active_Particles.begin();
159 it != active_Particles.end();
160 it++ )
161 {
162 (*it)->update(time_step);
163 }
164}
165void updatePhysics(float time_step)
166{
167 // add new PhysicsEntity*s to Active
168 for( setPhys::iterator it = physics_To_Add.begin();
169 it != physics_To_Add.end();
170 it++ )
171 {
172 active_Physics.insert(*it);
173 }
174 physics_To_Add.clear();
175
176 // remove dead PhysicsEntity*s from Active
177 for( setPhys::iterator it = physics_To_Remove.begin();
178 it != physics_To_Remove.end();
179 it++ )
180 {
181 active_Physics.erase(*it);
182 }
183 physics_To_Remove.clear();
184
54fe85c5 185 // apply collision math
2869e2e8 186 collision::update(active_Physics);
54fe85c5 187
ad9f1fb6
PG
188 // update active PhysicsEntity*s
189 for( setPhys::iterator it = active_Physics.begin();
190 it != active_Physics.end();
191 it++ )
192 {
193 (*it)->update(time_step);
194 }
195}