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