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