[7819] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION |
---|
| 17 | |
---|
[8190] | 18 | |
---|
| 19 | |
---|
| 20 | #include "collision.h" |
---|
| 21 | #include "collision_event.h" |
---|
| 22 | #include "collision_handle.h" |
---|
| 23 | #include "cr_defs.h" |
---|
| 24 | |
---|
[7819] | 25 | #include "cr_engine.h" |
---|
| 26 | |
---|
[8362] | 27 | #include "debug.h" |
---|
| 28 | |
---|
[9889] | 29 | namespace CoRe |
---|
| 30 | { |
---|
[7819] | 31 | |
---|
[9889] | 32 | ObjectListDefinition(CREngine); |
---|
[9890] | 33 | |
---|
| 34 | |
---|
[9889] | 35 | /** |
---|
| 36 | * standard constructor |
---|
| 37 | */ |
---|
| 38 | CREngine::CREngine () |
---|
| 39 | : BaseObject() |
---|
| 40 | { |
---|
| 41 | this->registerObject(this, CREngine::_objectList); |
---|
| 42 | this->setName("CREngine"); |
---|
[7819] | 43 | |
---|
[9889] | 44 | this->init(); |
---|
| 45 | } |
---|
[7819] | 46 | |
---|
[9889] | 47 | /** |
---|
| 48 | * the singleton reference to this class |
---|
| 49 | */ |
---|
| 50 | CREngine* CREngine::singletonRef = NULL; |
---|
[7819] | 51 | |
---|
[9889] | 52 | /** |
---|
| 53 | @brief standard deconstructor |
---|
| 54 | */ |
---|
| 55 | CREngine::~CREngine () |
---|
| 56 | { |
---|
| 57 | CREngine::singletonRef = NULL; |
---|
[7819] | 58 | |
---|
[9889] | 59 | if( this->collisionsUnused.size() != CR_MAX_COLLISIONS) |
---|
| 60 | PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS); |
---|
| 61 | if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS) |
---|
| 62 | PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS); |
---|
[8190] | 63 | |
---|
[9889] | 64 | this->reset(); |
---|
[8190] | 65 | |
---|
[9889] | 66 | std::vector<Collision*>::iterator it1 = this->collisionsUnused.begin(); |
---|
| 67 | for(; it1 < this->collisionsUnused.end(); it1++) |
---|
| 68 | delete *it1; |
---|
| 69 | std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUnused.begin(); |
---|
| 70 | for(; it2 < this->collisionEventsUnused.end(); it2++) |
---|
| 71 | delete *it2; |
---|
[8190] | 72 | |
---|
[9889] | 73 | this->collisionsUnused.clear(); |
---|
| 74 | this->collisionEventsUnused.clear(); |
---|
| 75 | } |
---|
[8190] | 76 | |
---|
[9889] | 77 | /** |
---|
| 78 | * inits the CREngine to a working state |
---|
| 79 | */ |
---|
| 80 | void CREngine::init() |
---|
| 81 | { |
---|
| 82 | // create a list of Collision events (precaching) |
---|
| 83 | for( int i = 0; i < CR_MAX_COLLISIONS; i++) |
---|
| 84 | this->collisionsUnused.push_back(new Collision()); |
---|
| 85 | for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++) |
---|
| 86 | this->collisionEventsUnused.push_back(new CollisionEvent()); |
---|
| 87 | } |
---|
[7841] | 88 | |
---|
| 89 | |
---|
[9889] | 90 | /** |
---|
[9892] | 91 | * @returns an instance to a collision object. instead of creating new object this ones can be resycled |
---|
| 92 | */ |
---|
| 93 | Collision* CREngine::popCollisionObject() |
---|
| 94 | { |
---|
| 95 | if( !this->collisionsUnused.empty()) |
---|
| 96 | { |
---|
| 97 | this->collisionsUsed.push_back(this->collisionsUnused.back()); |
---|
| 98 | this->collisionsUnused.pop_back(); |
---|
| 99 | return this->collisionsUsed.back(); |
---|
| 100 | } |
---|
| 101 | else return NULL; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * @return an instanco of a CollisionEvent object. instead of creating a new object this ones can be used and resycled |
---|
| 106 | */ |
---|
| 107 | CollisionEvent* CREngine::popCollisionEventObject() |
---|
| 108 | { |
---|
| 109 | if( !this->collisionEventsUnused.empty()) |
---|
| 110 | { |
---|
| 111 | this->collisionEventsUsed.push_back(this->collisionEventsUnused.back()); |
---|
| 112 | this->collisionEventsUnused.pop_back(); |
---|
| 113 | return this->collisionEventsUsed.back(); |
---|
| 114 | } |
---|
| 115 | else return NULL; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | /** |
---|
[9889] | 120 | * flushes the CollisionHandles and restores the CREngine to the initial state |
---|
| 121 | */ |
---|
| 122 | void CREngine::reset() |
---|
| 123 | { |
---|
| 124 | // first clear all CollisionHandles |
---|
[7841] | 125 | |
---|
[9889] | 126 | std::vector<CollisionHandle*>::iterator it = this->collisionHandles.begin(); |
---|
| 127 | for(; it < this->collisionHandles.end(); it++) |
---|
| 128 | { |
---|
| 129 | (*it)->reset(); |
---|
| 130 | delete *it; |
---|
| 131 | } |
---|
[7865] | 132 | |
---|
[9889] | 133 | this->collisionHandles.clear(); |
---|
[8190] | 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
[9889] | 137 | /** |
---|
| 138 | * subscribes a WorldEntity for a CollisionReaction |
---|
| 139 | * @param owner: the WE to subscribe |
---|
| 140 | * @param type: the type of collision reaction to perform |
---|
| 141 | * @return the newly created CollisionHandle |
---|
| 142 | */ |
---|
[9892] | 143 | CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, ReactionType type) |
---|
[9889] | 144 | { |
---|
| 145 | CollisionHandle* ch = new CollisionHandle(owner, type); |
---|
| 146 | this->collisionHandles.push_back(ch); |
---|
[8190] | 147 | |
---|
[9889] | 148 | return ch; |
---|
| 149 | } |
---|
[7927] | 150 | |
---|
[7841] | 151 | |
---|
[9889] | 152 | /** |
---|
| 153 | * unsubscribe reaction from the reaction list |
---|
| 154 | * @param collisionHandle the CollisionHandle to remove |
---|
| 155 | * @param returns true if worked collrectly |
---|
| 156 | */ |
---|
| 157 | bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle) |
---|
| 158 | { |
---|
| 159 | std::vector<CollisionHandle*>::iterator it; |
---|
| 160 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) |
---|
| 161 | { |
---|
| 162 | if( *it == collisionHandle) |
---|
| 163 | { |
---|
| 164 | this->collisionHandles.erase(it); |
---|
| 165 | delete collisionHandle; |
---|
| 166 | return true; |
---|
| 167 | } |
---|
[8190] | 168 | } |
---|
[9889] | 169 | return false; |
---|
[8190] | 170 | } |
---|
[7841] | 171 | |
---|
[7865] | 172 | |
---|
[9889] | 173 | /** |
---|
| 174 | * processes the collisions by calling the EventHandlers |
---|
| 175 | */ |
---|
| 176 | void CREngine::handleCollisions() |
---|
[8190] | 177 | { |
---|
[9889] | 178 | std::vector<CollisionHandle*>::iterator it; |
---|
| 179 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) |
---|
[8190] | 180 | { |
---|
[9889] | 181 | if( !(*it)->isDispatched() || (*it)->isContinuousPoll()) //does it have any collisions to report at all |
---|
| 182 | { |
---|
| 183 | (*it)->handleCollisions(); |
---|
| 184 | } |
---|
[8190] | 185 | } |
---|
[9889] | 186 | this->flushCollisions(); |
---|
[8190] | 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
[9889] | 190 | /** |
---|
| 191 | * flushes all the collision lists and puts them to their initial state |
---|
| 192 | */ |
---|
| 193 | void CREngine::flushCollisions() |
---|
| 194 | { |
---|
| 195 | std::vector<Collision*>::iterator it1 = this->collisionsUsed.begin(); |
---|
| 196 | for(; it1 < this->collisionsUsed.end(); it1++) |
---|
| 197 | this->collisionsUnused.push_back(*it1); |
---|
[8190] | 198 | |
---|
[9889] | 199 | std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUsed.begin(); |
---|
| 200 | for(; it2 < this->collisionEventsUsed.end(); it2++) |
---|
| 201 | this->collisionEventsUnused.push_back(*it2); |
---|
[8190] | 202 | |
---|
[9889] | 203 | this->collisionsUsed.clear(); |
---|
| 204 | this->collisionEventsUsed.clear(); |
---|
| 205 | } |
---|
[8190] | 206 | |
---|
| 207 | |
---|
[9889] | 208 | void CREngine::debug() |
---|
| 209 | { |
---|
| 210 | } |
---|
[8190] | 211 | |
---|
| 212 | } |
---|