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