gravity well now follows the mouse
[physics.git] / src / entityManager.cpp
... / ...
CommitLineData
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
18#include "entityManager.h"
19#include "debug.h"
20
21#include <set>
22
23#include "Entities/Entity.h"
24#include "Entities/Particle.h"
25#include "Entities/PhysicsEntity.h"
26
27#include "collisionHandler.h"
28#include "effectManager.h"
29
30/// ***** Private Method Headers *****
31
32void updateParticles(float);
33void updatePhysics(float);
34
35/// ***** Private Variables *****
36
37typedef std::set<Particle*> setPart;
38setPart particles_To_Add;
39setPart active_Particles;
40setPart particles_To_Remove;
41
42typedef std::set<PhysicsEntity*> setPhys;
43setPhys physics_To_Add;
44setPhys active_Physics;
45setPhys physics_To_Remove;
46
47/// ***** Initializers/Cleaners *****
48
49void manager::init()
50{
51 collision::init();
52}
53void manager::clean()
54{
55 collision::clean();
56}
57
58/// ***** Public Methods *****
59
60void manager::add(Entity* e)
61{
62 {
63 Particle* p = dynamic_cast<Particle*>(e);
64 if( p != 0 )
65 {
66 particles_To_Add.insert(p);
67 return;
68 }
69 }
70
71 {
72 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
73 if( p != 0 )
74 {
75 physics_To_Add.insert(p);
76 return;
77 }
78 }
79
80#ifdef WARNINGS
81 cerr << "ENTITY TYPE NOT SUPPORTED BY addEntity()!!" << endl;
82#endif
83}
84void manager::remove(Entity* e)
85{
86 {
87 Particle* p = dynamic_cast<Particle*>(e);
88 if( p != 0 )
89 {
90 particles_To_Remove.insert(p);
91 return;
92 }
93 }
94
95 {
96 PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
97 if( p != 0 )
98 {
99 physics_To_Remove.insert(p);
100 return;
101 }
102 }
103
104#ifdef WARNINGS
105 cerr << "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!" << endl;
106#endif
107}
108
109void manager::handleInput()
110{
111 effect::handleInput();
112}
113void manager::update(float time_step)
114{
115 effect::update(time_step);
116
117 updateParticles(time_step);
118 updatePhysics(time_step);
119}
120void manager::draw()
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 *****
140
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
189 // apply collision math
190 collision::update(active_Physics, time_step);
191
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}