renamed libpg to libbear
[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"
f32a9b7c 19
5e0713e5
PG
20#include <bear/debug.h>
21#include <bear/Lock.h>
22#include <bear/Autolock.h>
23using namespace bear;
a483ed75 24
33b8c69b 25#include <set>
5829cb66 26#include <SDL/SDL.h>
33b8c69b 27
054d658f
PG
28#include "Entities/Entity.h"
29#include "Entities/Particle.h"
30#include "Entities/PhysicsEntity.h"
31
0ac1dc80 32#include "collisionManager.h"
a823a800 33#include "effectManager.h"
54fe85c5 34
ad9f1fb6 35/// ***** Private Method Headers *****
617dcc71 36
b6ef7e06
PG
37static void updateParticles(float);
38static void updatePhysics(float);
39static void addOrRemoveParticles();
40static void addOrRemovePhysics();
ad9f1fb6
PG
41
42/// ***** Private Variables *****
617dcc71 43
f72f4a59 44typedef std::set<Particle*> setPart;
b6ef7e06
PG
45static setPart particles_To_Add;
46static setPart active_Particles;
47static setPart particles_To_Remove;
ad9f1fb6 48
f72f4a59 49typedef std::set<PhysicsEntity*> setPhys;
b6ef7e06
PG
50static setPhys physics_To_Add;
51static setPhys active_Physics;
52static setPhys physics_To_Remove;
ad9f1fb6 53
563db69a
PG
54static Lock muSetPart;
55static Lock muSetPhys;
5829cb66 56
617dcc71 57/// ***** Initializers/Cleaners *****
ad9f1fb6 58
d388f0ec 59void manager::init()
ad9f1fb6 60{
b6ef7e06
PG
61 muSetPart.init();
62 muSetPhys.init();
5829cb66 63
54fe85c5 64 collision::init();
ad9f1fb6 65}
d388f0ec 66void manager::clean()
ad9f1fb6 67{
54fe85c5 68 collision::clean();
5829cb66 69
563db69a
PG
70 muSetPhys.fini();
71 muSetPart.fini();
ad9f1fb6
PG
72}
73
617dcc71
PG
74/// ***** Public Methods *****
75
b6ef7e06 76void manager::add(Entity* pe)
ad9f1fb6 77{
b6ef7e06
PG
78 DASSERT(pe != NULL);
79
ad9f1fb6 80 {
b6ef7e06
PG
81 Particle* pp = dynamic_cast<Particle*>(pe);
82 if( pp != NULL )
d388f0ec 83 {
b6ef7e06 84 particles_To_Add.insert(pp);
d388f0ec
PG
85 return;
86 }
ad9f1fb6
PG
87 }
88
89 {
b6ef7e06
PG
90 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
91 if( ppe != NULL )
d388f0ec 92 {
b6ef7e06 93 physics_To_Add.insert(ppe);
d388f0ec
PG
94 return;
95 }
ad9f1fb6
PG
96 }
97
ca2d526e 98 DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
ad9f1fb6 99}
b6ef7e06 100void manager::remove(Entity* pe)
ad9f1fb6 101{
b6ef7e06
PG
102 DASSERT(pe != NULL);
103
ad9f1fb6 104 {
563db69a 105 Autolock lock( &muSetPart );
b6ef7e06
PG
106 Particle* p = dynamic_cast<Particle*>(pe);
107 if( p != NULL )
d388f0ec
PG
108 {
109 particles_To_Remove.insert(p);
110 return;
111 }
ad9f1fb6
PG
112 }
113
114 {
563db69a 115 Autolock lock( &muSetPhys );
b6ef7e06
PG
116 PhysicsEntity* ppe = dynamic_cast<PhysicsEntity*>(pe);
117 if( ppe != NULL )
d388f0ec 118 {
b6ef7e06 119 physics_To_Remove.insert(ppe);
d388f0ec
PG
120 return;
121 }
ad9f1fb6
PG
122 }
123
ca2d526e 124 DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
ad9f1fb6
PG
125}
126
d388f0ec 127void manager::handleInput()
ad9f1fb6 128{
a823a800 129 effect::handleInput();
ad9f1fb6 130}
d388f0ec 131void manager::update(float time_step)
ad9f1fb6 132{
a823a800
PG
133 effect::update(time_step);
134
ad9f1fb6
PG
135 updateParticles(time_step);
136 updatePhysics(time_step);
137}
d388f0ec 138void manager::draw()
ad9f1fb6 139{
3bccd1d7 140 {
563db69a 141 Autolock lock( &muSetPart );
3bccd1d7 142
b6ef7e06
PG
143 addOrRemoveParticles();
144
145 // draw active Particle*s
5829cb66
PG
146 for( setPart::iterator it = active_Particles.begin();
147 it != active_Particles.end();
148 it++ )
149 {
150 (*it)->draw();
151 }
3bccd1d7
PG
152 }
153
154 {
563db69a 155 Autolock lock( &muSetPhys );
b6ef7e06
PG
156
157 addOrRemovePhysics();
3bccd1d7 158
b6ef7e06 159 // draw active PhysicsEntity*s
5829cb66
PG
160 for( setPhys::iterator it = active_Physics.begin();
161 it != active_Physics.end();
162 it++ )
163 {
164 (*it)->draw();
165 }
3bccd1d7 166 }
ad9f1fb6
PG
167}
168
169/// ***** Private Methods *****
617dcc71 170
ad9f1fb6
PG
171void updateParticles(float time_step)
172{
b6ef7e06 173 addOrRemoveParticles();
ad9f1fb6
PG
174
175 // update active Particle*s
176 for( setPart::iterator it = active_Particles.begin();
177 it != active_Particles.end();
178 it++ )
179 {
180 (*it)->update(time_step);
181 }
182}
183void updatePhysics(float time_step)
184{
b6ef7e06 185 addOrRemovePhysics();
ad9f1fb6 186
54fe85c5 187 // apply collision math
2869e2e8 188 collision::update(active_Physics);
54fe85c5 189
ad9f1fb6
PG
190 // update active PhysicsEntity*s
191 for( setPhys::iterator it = active_Physics.begin();
192 it != active_Physics.end();
193 it++ )
194 {
195 (*it)->update(time_step);
196 }
197}
b6ef7e06 198void addOrRemoveParticles()
f342ff31 199{
563db69a 200 Autolock lock( &muSetPart );
f342ff31
PG
201
202 // add new Particle*s to Active
203 for( setPart::iterator it = particles_To_Add.begin();
204 it != particles_To_Add.end();
205 it++ )
206 {
f342ff31
PG
207 active_Particles.insert(*it);
208 }
209 particles_To_Add.clear();
210
211 // remove dead Particle*s from Active
212 for( setPart::iterator it = particles_To_Remove.begin();
213 it != particles_To_Remove.end();
214 it++ )
215 {
f342ff31
PG
216 active_Particles.erase(*it);
217 }
218 particles_To_Remove.clear();
219}
b6ef7e06 220void addOrRemovePhysics()
f342ff31 221{
563db69a 222 Autolock lock( &muSetPhys );
f342ff31
PG
223
224 // add new PhysicsEntity*s to Active
225 for( setPhys::iterator it = physics_To_Add.begin();
226 it != physics_To_Add.end();
227 it++ )
228 {
f342ff31
PG
229 active_Physics.insert(*it);
230 }
231 physics_To_Add.clear();
232
233 // remove dead PhysicsEntity*s from Active
234 for( setPhys::iterator it = physics_To_Remove.begin();
235 it != physics_To_Remove.end();
236 it++ )
237 {
f342ff31
PG
238 active_Physics.erase(*it);
239 }
240 physics_To_Remove.clear();
241}