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