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