[7841] | 1 | /* |
---|
[8495] | 2 | orxonox - the future of 3D-vertical-scrollersf |
---|
[7841] | 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: |
---|
[7927] | 12 | main-programmer: Patrick Boenzli |
---|
[7841] | 13 | */ |
---|
| 14 | |
---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION |
---|
| 16 | |
---|
[9895] | 17 | #include "collision_filter.h" |
---|
[7841] | 18 | |
---|
[8190] | 19 | #include "world_entity.h" |
---|
| 20 | |
---|
| 21 | #include "collision.h" |
---|
| 22 | #include "collision_event.h" |
---|
| 23 | |
---|
| 24 | |
---|
[8362] | 25 | #include "debug.h" |
---|
| 26 | |
---|
[9896] | 27 | #include <vector> |
---|
| 28 | |
---|
| 29 | |
---|
[9889] | 30 | namespace CoRe |
---|
[7841] | 31 | { |
---|
| 32 | |
---|
[9895] | 33 | ObjectListDefinition(CollisionFilter); |
---|
[7927] | 34 | |
---|
[9889] | 35 | /** |
---|
| 36 | * standard constructor |
---|
| 37 | * @todo this constructor is not jet implemented - do it |
---|
| 38 | */ |
---|
[9896] | 39 | CollisionFilter::CollisionFilter (WorldEntity* owner) |
---|
[9889] | 40 | { |
---|
[9895] | 41 | this->registerObject(this, CollisionFilter::_objectList); |
---|
[8190] | 42 | |
---|
[9896] | 43 | this->_owner = owner; |
---|
[8190] | 44 | |
---|
[9896] | 45 | this->_bContinuousPoll = false; |
---|
| 46 | this->_bStopOnFirstCollision = false; |
---|
| 47 | this->_bReactive = false; |
---|
[9889] | 48 | } |
---|
[8190] | 49 | |
---|
| 50 | |
---|
[9889] | 51 | /** |
---|
| 52 | * standard deconstructor |
---|
| 53 | */ |
---|
[9895] | 54 | CollisionFilter::~CollisionFilter () |
---|
[9889] | 55 | { |
---|
[9896] | 56 | this->unsubscribeReactions(); |
---|
[9889] | 57 | } |
---|
[8190] | 58 | |
---|
[9896] | 59 | |
---|
[9889] | 60 | /** |
---|
[9896] | 61 | * subscribe reaction |
---|
[9889] | 62 | */ |
---|
[9896] | 63 | void CollisionFilter::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1) |
---|
[9889] | 64 | { |
---|
[9896] | 65 | // check if its a valid type |
---|
| 66 | if( likely(this->validCRType( type))) |
---|
| 67 | { |
---|
[9939] | 68 | //check if this class isn't already registered |
---|
[9980] | 69 | TargetIterator it = this->_filters[type].begin(); |
---|
[9896] | 70 | for(; it != this->_filters[type].end(); it++) |
---|
| 71 | { |
---|
| 72 | if( unlikely(*it == target1)) |
---|
| 73 | return; |
---|
| 74 | } |
---|
| 75 | |
---|
[9939] | 76 | // so subscribe the reaction finally |
---|
[9896] | 77 | this->_filters[type].push_back(target1); |
---|
| 78 | this->_bReactive = true; |
---|
| 79 | } |
---|
[9889] | 80 | } |
---|
[8190] | 81 | |
---|
[9869] | 82 | |
---|
[9889] | 83 | /** |
---|
[9896] | 84 | * subscribe for a reaction |
---|
[9889] | 85 | */ |
---|
[9896] | 86 | void CollisionFilter::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1, const ClassID& target2) |
---|
[9889] | 87 | { |
---|
[9896] | 88 | this->subscribeReaction(type, target1); |
---|
| 89 | this->subscribeReaction(type, target2); |
---|
| 90 | } |
---|
[8190] | 91 | |
---|
| 92 | |
---|
[9896] | 93 | /** |
---|
| 94 | * subscribe for a reaction |
---|
| 95 | */ |
---|
| 96 | void CollisionFilter::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1, const ClassID& target2, const ClassID& target3) |
---|
| 97 | { |
---|
| 98 | this->subscribeReaction(type, target1); |
---|
| 99 | this->subscribeReaction(type, target2); |
---|
| 100 | this->subscribeReaction(type, target3); |
---|
[8190] | 101 | } |
---|
| 102 | |
---|
[9889] | 103 | /** |
---|
[9896] | 104 | * unsubscribe from a specific collision reaction |
---|
[9889] | 105 | */ |
---|
[9896] | 106 | void CollisionFilter::unsubscribeReaction(CoRe::CREngine::ReactionType type) |
---|
[9889] | 107 | { |
---|
[9896] | 108 | if( likely(this->validCRType( type))) |
---|
| 109 | this->_filters[type].clear(); |
---|
[9988] | 110 | |
---|
| 111 | for( int i = 0; i < CREngine::CR_NUMBER; i++) |
---|
| 112 | { |
---|
| 113 | if( this->_filters[i].size() > 0) |
---|
| 114 | { |
---|
| 115 | this->_bReactive = true; |
---|
| 116 | break; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
[9889] | 120 | } |
---|
[8190] | 121 | |
---|
[9889] | 122 | /** |
---|
[9896] | 123 | * unsubscribe from all collision reactions |
---|
[9889] | 124 | */ |
---|
[9896] | 125 | void CollisionFilter::unsubscribeReactions() |
---|
[8190] | 126 | { |
---|
[9896] | 127 | for(int i = 0; i < CREngine::CR_NUMBER; i++) |
---|
| 128 | this->_filters[i].clear(); |
---|
[9988] | 129 | |
---|
| 130 | this->_bReactive = false; |
---|
[9896] | 131 | } |
---|
[9235] | 132 | |
---|
[9889] | 133 | |
---|
[8190] | 134 | |
---|
[9889] | 135 | /** |
---|
[9896] | 136 | * tests if the owner WorldEntity is listening to collisions from another specif WorldEntity entity |
---|
| 137 | * @param entity WorldEntity to test against |
---|
| 138 | * |
---|
| 139 | * This is the most important interface function of this class: it performs a check and returns true |
---|
| 140 | * if the WorldEntity entity is actualy responsive for a certain other WorldEntity |
---|
[9889] | 141 | */ |
---|
[9939] | 142 | bool CollisionFilter::operator()(const WorldEntity& entity) const |
---|
[8190] | 143 | { |
---|
[10007] | 144 | // PRINTF(0)("operator()(const WorldEntity& entity)\n"); |
---|
[10006] | 145 | |
---|
| 146 | // if there are no installed criterions just ommit and return |
---|
| 147 | if( !this->isReactive()) |
---|
[9896] | 148 | return false; |
---|
| 149 | |
---|
| 150 | // goes through all registered filter criterions and looks for matches |
---|
| 151 | for( int i = 0; i < CREngine::CR_NUMBER; i++ ) |
---|
[9889] | 152 | { |
---|
[10006] | 153 | TargetIteratorConst it = this->_filters[i].begin(); |
---|
| 154 | for(; it != this->_filters[i].end(); it++ ) |
---|
| 155 | { |
---|
[10007] | 156 | // PRINTF(0)("[%i] check %s is a %s?\n", i, entity.getClassName().c_str(), (*it).name().c_str()); |
---|
[9990] | 157 | if( unlikely(entity.isA(*it) ) ) |
---|
| 158 | return true; |
---|
[10006] | 159 | } |
---|
[9889] | 160 | } |
---|
| 161 | |
---|
| 162 | return false; |
---|
[8190] | 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
[9898] | 166 | /** |
---|
| 167 | * tests if the owner WorldEntity is listening to collisions from another specif WorldEntity entity |
---|
| 168 | * @param entity WorldEntity to test against |
---|
| 169 | * |
---|
| 170 | * This is the most important interface function of this class: it performs a check and returns true |
---|
| 171 | * if the WorldEntity entity is actualy responsive for a certain other WorldEntity |
---|
| 172 | */ |
---|
[9939] | 173 | bool CollisionFilter::operator()(const WorldEntity& entity, const CREngine::ReactionType type) const |
---|
[9898] | 174 | { |
---|
[10006] | 175 | assert(&entity != NULL); |
---|
| 176 | |
---|
[10007] | 177 | // PRINTF(0)("operator()(const WorldEntity& entity, const CREngine::ReactionType type)\n"); |
---|
[9980] | 178 | // if there are no installed criterions just omit and return |
---|
[9988] | 179 | if( !this->isReactive()) |
---|
[9898] | 180 | return false; |
---|
[8190] | 181 | |
---|
[10006] | 182 | |
---|
[9898] | 183 | // goes through all registered filter criterions and looks for matches |
---|
[9990] | 184 | TargetIteratorConst it = this->_filters[type].begin(); |
---|
[9939] | 185 | for(; it != this->_filters[type].end(); it++ ) |
---|
[10006] | 186 | { |
---|
[10007] | 187 | // PRINTF(0)("size: %i - %s is a %s?\n", this->_filters[type].size(), entity.getClassName().c_str(), (*it).name().c_str()); |
---|
[10006] | 188 | if( entity.isA(*it)) |
---|
[9898] | 189 | return true; |
---|
[10006] | 190 | } |
---|
[8190] | 191 | |
---|
[9898] | 192 | return false; |
---|
| 193 | } |
---|
[8190] | 194 | |
---|
| 195 | |
---|
[9898] | 196 | |
---|
| 197 | } // namespace end |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | |
---|