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