[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 | |
---|
[7940] | 18 | |
---|
| 19 | |
---|
| 20 | #include "collision.h" |
---|
[7945] | 21 | #include "collision_handle.h" |
---|
[7940] | 22 | #include "cr_defs.h" |
---|
| 23 | |
---|
[7819] | 24 | #include "cr_engine.h" |
---|
| 25 | |
---|
| 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * standard constructor |
---|
| 31 | */ |
---|
| 32 | CREngine::CREngine () |
---|
[7937] | 33 | : BaseObject() |
---|
[7819] | 34 | { |
---|
| 35 | this->setClassID(CL_CR_ENGINE, "CREngine"); |
---|
| 36 | this->setName("CREngine"); |
---|
| 37 | |
---|
[7940] | 38 | this->init(); |
---|
[7819] | 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * the singleton reference to this class |
---|
| 43 | */ |
---|
| 44 | CREngine* CREngine::singletonRef = NULL; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | @brief standard deconstructor |
---|
| 48 | */ |
---|
| 49 | CREngine::~CREngine () |
---|
| 50 | { |
---|
| 51 | CREngine::singletonRef = NULL; |
---|
[7948] | 52 | |
---|
| 53 | if( this->cachedCollisions.size() != CR_MAX_COLLISIONS) |
---|
| 54 | PRINTF(0)("CollisionReaction Error: cache size missmatch: %i of %i\n", this->cachedCollisions.size(), CR_NUMBER); |
---|
| 55 | |
---|
| 56 | this->reset(); |
---|
| 57 | |
---|
[7949] | 58 | while( !this->cachedCollisions.empty()) |
---|
| 59 | delete this->cachedCollisions.back(); |
---|
[7948] | 60 | this->cachedCollisions.clear(); |
---|
[7819] | 61 | } |
---|
[7841] | 62 | |
---|
[7945] | 63 | /** |
---|
| 64 | * inits the CREngine to a working state |
---|
| 65 | */ |
---|
[7940] | 66 | void CREngine::init() |
---|
| 67 | { |
---|
| 68 | // create a list of Collision events (precaching) |
---|
| 69 | Collision* collisions = new Collision[CR_MAX_COLLISIONS]; |
---|
| 70 | for( int i = 0; i < CR_MAX_COLLISIONS; i++) |
---|
[7944] | 71 | this->cachedCollisions.push_back(&collisions[i]); |
---|
[7940] | 72 | } |
---|
[7841] | 73 | |
---|
[7940] | 74 | |
---|
[7946] | 75 | /** |
---|
| 76 | * flushes the CollisionHandles and restores the CREngine to the initial state |
---|
| 77 | */ |
---|
[7940] | 78 | void CREngine::reset() |
---|
[7945] | 79 | { |
---|
| 80 | // first clear all CollisionHandles |
---|
[7948] | 81 | |
---|
| 82 | while( !this->collisionHandles.empty()) |
---|
[7946] | 83 | { |
---|
[7948] | 84 | this->collisionHandles.front()->reset(); |
---|
| 85 | delete this->collisionHandles.front(); |
---|
[7946] | 86 | } |
---|
[7945] | 87 | this->collisionHandles.clear(); |
---|
| 88 | } |
---|
[7940] | 89 | |
---|
| 90 | |
---|
[7937] | 91 | /** |
---|
| 92 | * subscribes a WorldEntity for a CollisionReaction |
---|
| 93 | * @param owner: the WE to subscribe |
---|
| 94 | * @param type: the type of collision reaction to perform |
---|
[7946] | 95 | * @return the newly created CollisionHandle |
---|
[7937] | 96 | */ |
---|
[7932] | 97 | CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type) |
---|
[7841] | 98 | { |
---|
[7945] | 99 | CollisionHandle* ch = new CollisionHandle(owner, type); |
---|
| 100 | this->collisionHandles.push_back(ch); |
---|
[7842] | 101 | } |
---|
[7841] | 102 | |
---|
[7945] | 103 | |
---|
[7946] | 104 | /** |
---|
| 105 | * unsubscribe reaction from the reaction list |
---|
| 106 | * @param collisionHandle the CollisionHandle to remove |
---|
| 107 | * @param returns true if worked collrectly |
---|
| 108 | */ |
---|
[7937] | 109 | bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle) |
---|
[7946] | 110 | { |
---|
| 111 | std::vector<CollisionHandle*>::iterator it; |
---|
| 112 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) |
---|
| 113 | { |
---|
| 114 | if( *it == collisionHandle) |
---|
| 115 | { |
---|
| 116 | this->collisionHandles.erase(it); |
---|
| 117 | delete collisionHandle; |
---|
| 118 | return true; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | return false; |
---|
| 122 | } |
---|
[7865] | 123 | |
---|
[7937] | 124 | |
---|
| 125 | void CREngine::handleCollisions() |
---|
[7947] | 126 | { |
---|
| 127 | std::vector<CollisionHandle*>::iterator it; |
---|
| 128 | for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++) |
---|
| 129 | { |
---|
| 130 | if( (*it)->isCollided()) |
---|
| 131 | { |
---|
| 132 | (*it)->handleCollisions(); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | } |
---|
[7937] | 136 | |
---|
| 137 | |
---|
| 138 | |
---|
[7933] | 139 | void CREngine::debug() |
---|
| 140 | { |
---|
| 141 | |
---|
[7940] | 142 | } |
---|
| 143 | |
---|