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