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