[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 | /** |
---|
| 91 | * flushes the CollisionHandles and restores the CREngine to the initial state |
---|
| 92 | */ |
---|
| 93 | void CREngine::reset() |
---|
| 94 | { |
---|
| 95 | // first clear all CollisionHandles |
---|
[7841] | 96 | |
---|
[9889] | 97 | std::vector<CollisionHandle*>::iterator it = this->collisionHandles.begin(); |
---|
| 98 | for(; it < this->collisionHandles.end(); it++) |
---|
| 99 | { |
---|
| 100 | (*it)->reset(); |
---|
| 101 | delete *it; |
---|
| 102 | } |
---|
[7865] | 103 | |
---|
[9889] | 104 | this->collisionHandles.clear(); |
---|
[8190] | 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
[9889] | 108 | /** |
---|
| 109 | * subscribes a WorldEntity for a CollisionReaction |
---|
| 110 | * @param owner: the WE to subscribe |
---|
| 111 | * @param type: the type of collision reaction to perform |
---|
| 112 | * @return the newly created CollisionHandle |
---|
| 113 | */ |
---|
| 114 | CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type) |
---|
| 115 | { |
---|
| 116 | CollisionHandle* ch = new CollisionHandle(owner, type); |
---|
| 117 | this->collisionHandles.push_back(ch); |
---|
[8190] | 118 | |
---|
[9889] | 119 | return ch; |
---|
| 120 | } |
---|
[7927] | 121 | |
---|
[7841] | 122 | |
---|
[9889] | 123 | /** |
---|
| 124 | * unsubscribe reaction from the reaction list |
---|
| 125 | * @param collisionHandle the CollisionHandle to remove |
---|
| 126 | * @param returns true if worked collrectly |
---|
| 127 | */ |
---|
| 128 | bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle) |
---|
| 129 | { |
---|
| 130 | std::vector<CollisionHandle*>::iterator it; |
---|
| 131 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) |
---|
| 132 | { |
---|
| 133 | if( *it == collisionHandle) |
---|
| 134 | { |
---|
| 135 | this->collisionHandles.erase(it); |
---|
| 136 | delete collisionHandle; |
---|
| 137 | return true; |
---|
| 138 | } |
---|
[8190] | 139 | } |
---|
[9889] | 140 | return false; |
---|
[8190] | 141 | } |
---|
[7841] | 142 | |
---|
[7865] | 143 | |
---|
[9889] | 144 | /** |
---|
| 145 | * processes the collisions by calling the EventHandlers |
---|
| 146 | */ |
---|
| 147 | void CREngine::handleCollisions() |
---|
[8190] | 148 | { |
---|
[9889] | 149 | std::vector<CollisionHandle*>::iterator it; |
---|
| 150 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) |
---|
[8190] | 151 | { |
---|
[9889] | 152 | if( !(*it)->isDispatched() || (*it)->isContinuousPoll()) //does it have any collisions to report at all |
---|
| 153 | { |
---|
| 154 | (*it)->handleCollisions(); |
---|
| 155 | } |
---|
[8190] | 156 | } |
---|
[9889] | 157 | this->flushCollisions(); |
---|
[8190] | 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
[9889] | 161 | /** |
---|
| 162 | * flushes all the collision lists and puts them to their initial state |
---|
| 163 | */ |
---|
| 164 | void CREngine::flushCollisions() |
---|
| 165 | { |
---|
| 166 | std::vector<Collision*>::iterator it1 = this->collisionsUsed.begin(); |
---|
| 167 | for(; it1 < this->collisionsUsed.end(); it1++) |
---|
| 168 | this->collisionsUnused.push_back(*it1); |
---|
[8190] | 169 | |
---|
[9889] | 170 | std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUsed.begin(); |
---|
| 171 | for(; it2 < this->collisionEventsUsed.end(); it2++) |
---|
| 172 | this->collisionEventsUnused.push_back(*it2); |
---|
[8190] | 173 | |
---|
[9889] | 174 | this->collisionsUsed.clear(); |
---|
| 175 | this->collisionEventsUsed.clear(); |
---|
| 176 | } |
---|
[8190] | 177 | |
---|
| 178 | |
---|
[9889] | 179 | void CREngine::debug() |
---|
| 180 | { |
---|
| 181 | } |
---|
[8190] | 182 | |
---|
| 183 | } |
---|