added screen collisions using an Effect
[physics.git] / src / entityManager.cpp
1 #include "entityManager.h"
2 #include "debug.h"
3
4 #include <set>
5 #include <iostream>
6
7 #include "Entities/Entity.h"
8 #include "Entities/Particle.h"
9 #include "Entities/PhysicsEntity.h"
10
11 /// ***** Private Method Headers *****
12 void updateParticles(float);
13 void updatePhysics(float);
14
15 /// ***** Private Variables *****
16 typedef std::set<Particle*> setPart;
17 setPart particles_To_Add;
18 setPart active_Particles;
19 setPart particles_To_Remove;
20 bool clearParticles;
21
22 typedef std::set<PhysicsEntity*> setPhys;
23 setPhys physics_To_Add;
24 setPhys active_Physics;
25 setPhys physics_To_Remove;
26 bool clearPhysics;
27
28 /// ***** Public Methods *****
29
30 void manager::init()
31 {
32
33 }
34 void manager::clean()
35 {
36
37 }
38
39 void manager::add(Entity* e)
40 {
41     {
42         Particle* p = dynamic_cast<Particle*>(e);
43         if( p != 0 )
44         {
45             particles_To_Add.insert(p);
46             return;
47         }
48     }
49
50     {
51         PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
52         if( p != 0 )
53         {
54             physics_To_Add.insert(p);
55             return;
56         }
57     }
58
59     std::cerr << "ENTITY TYPE NOT SUPPORTED BY addEntity()!!";
60     std::cerr << std::endl;
61 }
62 void manager::remove(Entity* e)
63 {
64     {
65         Particle* p = dynamic_cast<Particle*>(e);
66         if( p != 0 )
67         {
68             particles_To_Remove.insert(p);
69             return;
70         }
71     }
72
73     {
74         PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
75         if( p != 0 )
76         {
77             physics_To_Remove.insert(p);
78             return;
79         }
80     }
81
82     std::cerr << "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!";
83     std::cerr << std::endl;
84 }
85
86 void manager::clear()
87 {
88     clearParticles = true;
89     clearPhysics = true;
90 }
91
92 void manager::handleInput()
93 {
94     // TODO
95 }
96 void manager::update(float time_step)
97 {
98     updateParticles(time_step);
99     updatePhysics(time_step);
100 }
101 void manager::draw()
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     }
118
119 }
120
121 /// ***** Private Methods *****
122 void 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 }
150 void 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 }