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