gravity well now follows the mouse
[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
54fe85c5 27#include "collisionHandler.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
54fe85c5
PG
80#ifdef WARNINGS
81 cerr << "ENTITY TYPE NOT SUPPORTED BY addEntity()!!" << endl;
82#endif
ad9f1fb6 83}
d388f0ec 84void manager::remove(Entity* e)
ad9f1fb6
PG
85{
86 {
d388f0ec
PG
87 Particle* p = dynamic_cast<Particle*>(e);
88 if( p != 0 )
89 {
90 particles_To_Remove.insert(p);
91 return;
92 }
ad9f1fb6
PG
93 }
94
95 {
d388f0ec
PG
96 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
97 if( p != 0 )
98 {
99 physics_To_Remove.insert(p);
100 return;
101 }
ad9f1fb6
PG
102 }
103
54fe85c5
PG
104#ifdef WARNINGS
105 cerr << "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!" << endl;
106#endif
ad9f1fb6
PG
107}
108
d388f0ec 109void manager::handleInput()
ad9f1fb6 110{
a823a800 111 effect::handleInput();
ad9f1fb6 112}
d388f0ec 113void manager::update(float time_step)
ad9f1fb6 114{
a823a800
PG
115 effect::update(time_step);
116
ad9f1fb6
PG
117 updateParticles(time_step);
118 updatePhysics(time_step);
119}
d388f0ec 120void manager::draw()
ad9f1fb6
PG
121{
122 // update active Particle*s
123 for( setPart::iterator it = active_Particles.begin();
124 it != active_Particles.end();
125 it++ )
126 {
127 (*it)->draw();
128 }
129
130 // update active PhysicsEntity*s
131 for( setPhys::iterator it = active_Physics.begin();
132 it != active_Physics.end();
133 it++ )
134 {
135 (*it)->draw();
136 }
137}
138
139/// ***** Private Methods *****
617dcc71 140
ad9f1fb6
PG
141void updateParticles(float time_step)
142{
143 // add new Particle*s to Active
144 for( setPart::iterator it = particles_To_Add.begin();
145 it != particles_To_Add.end();
146 it++ )
147 {
148 active_Particles.insert(*it);
149 }
150 particles_To_Add.clear();
151
152 // remove dead Particle*s from Active
153 for( setPart::iterator it = particles_To_Remove.begin();
154 it != particles_To_Remove.end();
155 it++ )
156 {
157 active_Particles.erase(*it);
158 }
159 particles_To_Remove.clear();
160
161 // update active Particle*s
162 for( setPart::iterator it = active_Particles.begin();
163 it != active_Particles.end();
164 it++ )
165 {
166 (*it)->update(time_step);
167 }
168}
169void updatePhysics(float time_step)
170{
171 // add new PhysicsEntity*s to Active
172 for( setPhys::iterator it = physics_To_Add.begin();
173 it != physics_To_Add.end();
174 it++ )
175 {
176 active_Physics.insert(*it);
177 }
178 physics_To_Add.clear();
179
180 // remove dead PhysicsEntity*s from Active
181 for( setPhys::iterator it = physics_To_Remove.begin();
182 it != physics_To_Remove.end();
183 it++ )
184 {
185 active_Physics.erase(*it);
186 }
187 physics_To_Remove.clear();
188
54fe85c5
PG
189 // apply collision math
190 collision::update(active_Physics, time_step);
191
ad9f1fb6
PG
192 // update active PhysicsEntity*s
193 for( setPhys::iterator it = active_Physics.begin();
194 it != active_Physics.end();
195 it++ )
196 {
197 (*it)->update(time_step);
198 }
199}