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