| [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 |  | 
|---|
| [7819] | 29 | using namespace std; | 
|---|
|  | 30 |  | 
|---|
|  | 31 |  | 
|---|
|  | 32 | /** | 
|---|
|  | 33 | * standard constructor | 
|---|
|  | 34 | */ | 
|---|
|  | 35 | CREngine::CREngine () | 
|---|
| [8190] | 36 | : BaseObject() | 
|---|
| [7819] | 37 | { | 
|---|
|  | 38 | this->setClassID(CL_CR_ENGINE, "CREngine"); | 
|---|
|  | 39 | this->setName("CREngine"); | 
|---|
|  | 40 |  | 
|---|
| [8190] | 41 | this->init(); | 
|---|
| [7819] | 42 | } | 
|---|
|  | 43 |  | 
|---|
|  | 44 | /** | 
|---|
|  | 45 | *  the singleton reference to this class | 
|---|
|  | 46 | */ | 
|---|
|  | 47 | CREngine* CREngine::singletonRef = NULL; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | /** | 
|---|
|  | 50 | @brief standard deconstructor | 
|---|
|  | 51 | */ | 
|---|
|  | 52 | CREngine::~CREngine () | 
|---|
|  | 53 | { | 
|---|
|  | 54 | CREngine::singletonRef = NULL; | 
|---|
| [8190] | 55 |  | 
|---|
|  | 56 | if( this->collisionsUnused.size() != CR_MAX_COLLISIONS) | 
|---|
|  | 57 | PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS); | 
|---|
|  | 58 | if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS) | 
|---|
|  | 59 | PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS); | 
|---|
|  | 60 |  | 
|---|
|  | 61 | this->reset(); | 
|---|
|  | 62 |  | 
|---|
|  | 63 | vector<Collision*>::iterator it1 = this->collisionsUnused.begin(); | 
|---|
|  | 64 | for(; it1 < this->collisionsUnused.end(); it1++) | 
|---|
|  | 65 | delete *it1; | 
|---|
|  | 66 | vector<CollisionEvent*>::iterator it2 = this->collisionEventsUnused.begin(); | 
|---|
|  | 67 | for(; it2 < this->collisionEventsUnused.end(); it2++) | 
|---|
|  | 68 | delete *it2; | 
|---|
|  | 69 |  | 
|---|
|  | 70 | this->collisionsUnused.clear(); | 
|---|
|  | 71 | this->collisionEventsUnused.clear(); | 
|---|
| [7819] | 72 | } | 
|---|
| [7841] | 73 |  | 
|---|
| [8190] | 74 | /** | 
|---|
|  | 75 | * inits the CREngine to a working state | 
|---|
|  | 76 | */ | 
|---|
|  | 77 | void CREngine::init() | 
|---|
|  | 78 | { | 
|---|
|  | 79 | // create a list of Collision events (precaching) | 
|---|
|  | 80 | for( int i = 0; i < CR_MAX_COLLISIONS; i++) | 
|---|
|  | 81 | this->collisionsUnused.push_back(new Collision()); | 
|---|
|  | 82 | for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++) | 
|---|
|  | 83 | this->collisionEventsUnused.push_back(new CollisionEvent()); | 
|---|
|  | 84 | } | 
|---|
| [7841] | 85 |  | 
|---|
|  | 86 |  | 
|---|
| [8190] | 87 | /** | 
|---|
|  | 88 | * flushes the CollisionHandles and restores the CREngine to the initial state | 
|---|
|  | 89 | */ | 
|---|
|  | 90 | void CREngine::reset() | 
|---|
|  | 91 | { | 
|---|
|  | 92 | // first clear all CollisionHandles | 
|---|
| [7865] | 93 |  | 
|---|
| [8190] | 94 | vector<CollisionHandle*>::iterator it = this->collisionHandles.begin(); | 
|---|
|  | 95 | for(; it < this->collisionHandles.end(); it++) | 
|---|
|  | 96 | { | 
|---|
|  | 97 | (*it)->reset(); | 
|---|
|  | 98 | delete *it; | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
|  | 101 | this->collisionHandles.clear(); | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 |  | 
|---|
|  | 105 | /** | 
|---|
|  | 106 | * subscribes a WorldEntity for a CollisionReaction | 
|---|
|  | 107 | *  @param owner: the WE to subscribe | 
|---|
|  | 108 | *  @param type: the type of collision reaction to perform | 
|---|
|  | 109 | *  @return the newly created CollisionHandle | 
|---|
|  | 110 | */ | 
|---|
|  | 111 | CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type) | 
|---|
| [7841] | 112 | { | 
|---|
| [8190] | 113 | CollisionHandle* ch = new CollisionHandle(owner, type); | 
|---|
|  | 114 | this->collisionHandles.push_back(ch); | 
|---|
| [7927] | 115 |  | 
|---|
| [8190] | 116 | return ch; | 
|---|
| [7842] | 117 | } | 
|---|
| [7841] | 118 |  | 
|---|
|  | 119 |  | 
|---|
| [8190] | 120 | /** | 
|---|
|  | 121 | * unsubscribe reaction from the reaction list | 
|---|
|  | 122 | *  @param collisionHandle the CollisionHandle to remove | 
|---|
|  | 123 | *  @param returns true if worked collrectly | 
|---|
|  | 124 | */ | 
|---|
|  | 125 | bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle) | 
|---|
|  | 126 | { | 
|---|
|  | 127 | std::vector<CollisionHandle*>::iterator it; | 
|---|
|  | 128 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) | 
|---|
|  | 129 | { | 
|---|
|  | 130 | if( *it == collisionHandle) | 
|---|
|  | 131 | { | 
|---|
|  | 132 | this->collisionHandles.erase(it); | 
|---|
|  | 133 | delete collisionHandle; | 
|---|
|  | 134 | return true; | 
|---|
|  | 135 | } | 
|---|
|  | 136 | } | 
|---|
|  | 137 | return false; | 
|---|
|  | 138 | } | 
|---|
| [7841] | 139 |  | 
|---|
| [7865] | 140 |  | 
|---|
| [8190] | 141 | /** | 
|---|
|  | 142 | * processes the collisions by calling the EventHandlers | 
|---|
|  | 143 | */ | 
|---|
|  | 144 | void CREngine::handleCollisions() | 
|---|
|  | 145 | { | 
|---|
|  | 146 | std::vector<CollisionHandle*>::iterator it; | 
|---|
|  | 147 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) | 
|---|
|  | 148 | { | 
|---|
|  | 149 | if( (*it)->isCollided() || (*it)->isContinuousPoll())  //does it have any collisions to report at all | 
|---|
|  | 150 | { | 
|---|
|  | 151 | (*it)->handleCollisions(); | 
|---|
|  | 152 | } | 
|---|
|  | 153 | } | 
|---|
|  | 154 | this->flushCollisions(); | 
|---|
|  | 155 | } | 
|---|
|  | 156 |  | 
|---|
|  | 157 |  | 
|---|
|  | 158 | /** | 
|---|
|  | 159 | * flushes all the collision lists and puts them to their initial state | 
|---|
|  | 160 | */ | 
|---|
|  | 161 | void CREngine::flushCollisions() | 
|---|
|  | 162 | { | 
|---|
|  | 163 | vector<Collision*>::iterator it1 = this->collisionsUsed.begin(); | 
|---|
|  | 164 | for(; it1 < this->collisionsUsed.end(); it1++) | 
|---|
|  | 165 | this->collisionsUnused.push_back(*it1); | 
|---|
|  | 166 |  | 
|---|
|  | 167 | vector<CollisionEvent*>::iterator it2 = this->collisionEventsUsed.begin(); | 
|---|
|  | 168 | for(; it2 < this->collisionEventsUsed.end(); it2++) | 
|---|
|  | 169 | this->collisionEventsUnused.push_back(*it2); | 
|---|
|  | 170 |  | 
|---|
|  | 171 | this->collisionsUsed.clear(); | 
|---|
|  | 172 | this->collisionEventsUsed.clear(); | 
|---|
|  | 173 | } | 
|---|
|  | 174 |  | 
|---|
|  | 175 |  | 
|---|
|  | 176 | void CREngine::debug() | 
|---|
|  | 177 | { | 
|---|
|  | 178 |  | 
|---|
|  | 179 | } | 
|---|
|  | 180 |  | 
|---|