bae30ae4c0360f938b83e0561f44c495c333ff8b
[physics.git] / src / entityManager.cpp
1 /*
2  *  Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "entityManager.h"
19 #include "debug.h"
20
21 #include <set>
22
23 #include "Entities/Entity.h"
24 #include "Entities/Particle.h"
25 #include "Entities/PhysicsEntity.h"
26
27 #include "collisionManager.h"
28 #include "effectManager.h"
29
30 /// ***** Private Method Headers *****
31
32 void updateParticles(float);
33 void updatePhysics(float);
34
35 /// ***** Private Variables *****
36
37 typedef std::set<Particle*> setPart;
38 setPart particles_To_Add;
39 setPart active_Particles;
40 setPart particles_To_Remove;
41
42 typedef std::set<PhysicsEntity*> setPhys;
43 setPhys physics_To_Add;
44 setPhys active_Physics;
45 setPhys physics_To_Remove;
46
47 /// ***** Initializers/Cleaners *****
48
49 void manager::init()
50 {
51     collision::init();
52 }
53 void manager::clean()
54 {
55     collision::clean();
56 }
57
58 /// ***** Public Methods *****
59
60 void manager::add(Entity* e)
61 {
62     {
63         Particle* p = dynamic_cast<Particle*>(e);
64         if( p != 0 )
65         {
66             particles_To_Add.insert(p);
67             return;
68         }
69     }
70
71     {
72         PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
73         if( p != 0 )
74         {
75             physics_To_Add.insert(p);
76             return;
77         }
78     }
79
80     DPF(0, "ENTITY TYPE NOT SUPPORTED BY addEntity()!!");
81 }
82 void manager::remove(Entity* e)
83 {
84     {
85         Particle* p = dynamic_cast<Particle*>(e);
86         if( p != 0 )
87         {
88             particles_To_Remove.insert(p);
89             return;
90         }
91     }
92
93     {
94         PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
95         if( p != 0 )
96         {
97             physics_To_Remove.insert(p);
98             return;
99         }
100     }
101
102     DPF(0, "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!");
103 }
104
105 void manager::handleInput()
106 {
107     effect::handleInput();
108 }
109 void manager::update(float time_step)
110 {
111     effect::update(time_step);
112
113     updateParticles(time_step);
114     updatePhysics(time_step);
115 }
116 void manager::draw()
117 {
118     // update active Particle*s
119     for( setPart::iterator it = active_Particles.begin();
120          it != active_Particles.end();
121          it++ )
122     {
123         (*it)->draw();
124     }
125
126     // update active PhysicsEntity*s
127     for( setPhys::iterator it = active_Physics.begin();
128          it != active_Physics.end();
129          it++ )
130     {
131         (*it)->draw();
132     }
133 }
134
135 /// ***** Private Methods *****
136
137 void updateParticles(float time_step)
138 {
139     // add new Particle*s to Active
140     for( setPart::iterator it = particles_To_Add.begin();
141          it != particles_To_Add.end();
142          it++ )
143     {
144         active_Particles.insert(*it);
145     }
146     particles_To_Add.clear();
147
148     // remove dead Particle*s from Active
149     for( setPart::iterator it = particles_To_Remove.begin();
150          it != particles_To_Remove.end();
151          it++ )
152     {
153         active_Particles.erase(*it);
154     }
155     particles_To_Remove.clear();
156
157     // update active Particle*s
158     for( setPart::iterator it = active_Particles.begin();
159          it != active_Particles.end();
160          it++ )
161     {
162         (*it)->update(time_step);
163     }
164 }
165 void updatePhysics(float time_step)
166 {
167     // add new PhysicsEntity*s to Active
168     for( setPhys::iterator it = physics_To_Add.begin();
169          it != physics_To_Add.end();
170          it++ )
171     {
172         active_Physics.insert(*it);
173     }
174     physics_To_Add.clear();
175
176     // remove dead PhysicsEntity*s from Active
177     for( setPhys::iterator it = physics_To_Remove.begin();
178          it != physics_To_Remove.end();
179          it++ )
180     {
181         active_Physics.erase(*it);
182     }
183     physics_To_Remove.clear();
184
185     // apply collision math
186     collision::update(active_Physics);
187
188     // update active PhysicsEntity*s
189     for( setPhys::iterator it = active_Physics.begin();
190          it != active_Physics.end();
191          it++ )
192     {
193         (*it)->update(time_step);
194     }
195 }