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