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