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