1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollersf |
---|
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 | */ |
---|
14 | |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION |
---|
16 | |
---|
17 | #include "collision_filter.h" |
---|
18 | |
---|
19 | #include "world_entity.h" |
---|
20 | |
---|
21 | #include "collision.h" |
---|
22 | #include "collision_event.h" |
---|
23 | |
---|
24 | |
---|
25 | #include "debug.h" |
---|
26 | |
---|
27 | #include <vector> |
---|
28 | |
---|
29 | |
---|
30 | namespace CoRe |
---|
31 | { |
---|
32 | |
---|
33 | ObjectListDefinition(CollisionFilter); |
---|
34 | |
---|
35 | /** |
---|
36 | * standard constructor |
---|
37 | * @todo this constructor is not jet implemented - do it |
---|
38 | */ |
---|
39 | CollisionFilter::CollisionFilter (WorldEntity* owner) |
---|
40 | { |
---|
41 | this->registerObject(this, CollisionFilter::_objectList); |
---|
42 | |
---|
43 | this->_owner = owner; |
---|
44 | |
---|
45 | this->_bContinuousPoll = false; |
---|
46 | this->_bStopOnFirstCollision = false; |
---|
47 | this->_bReactive = false; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | /** |
---|
52 | * standard deconstructor |
---|
53 | */ |
---|
54 | CollisionFilter::~CollisionFilter () |
---|
55 | { |
---|
56 | this->unsubscribeReactions(); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * subscribe reaction |
---|
62 | */ |
---|
63 | void CollisionFilter::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1) |
---|
64 | { |
---|
65 | // check if its a valid type |
---|
66 | if( likely(this->validCRType( type))) |
---|
67 | { |
---|
68 | //check if this class isn't already registered |
---|
69 | TargetIterator it = this->_filters[type].begin(); |
---|
70 | for(; it != this->_filters[type].end(); it++) |
---|
71 | { |
---|
72 | if( unlikely(*it == target1)) |
---|
73 | return; |
---|
74 | } |
---|
75 | |
---|
76 | // so subscribe the reaction finally |
---|
77 | this->_filters[type].push_back(target1); |
---|
78 | this->_bReactive = true; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | * subscribe for a reaction |
---|
85 | */ |
---|
86 | void CollisionFilter::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1, const ClassID& target2) |
---|
87 | { |
---|
88 | this->subscribeReaction(type, target1); |
---|
89 | this->subscribeReaction(type, target2); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
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); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * unsubscribe from a specific collision reaction |
---|
105 | */ |
---|
106 | void CollisionFilter::unsubscribeReaction(CoRe::CREngine::ReactionType type) |
---|
107 | { |
---|
108 | if( likely(this->validCRType( type))) |
---|
109 | this->_filters[type].clear(); |
---|
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 | |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * unsubscribe from all collision reactions |
---|
124 | */ |
---|
125 | void CollisionFilter::unsubscribeReactions() |
---|
126 | { |
---|
127 | for(int i = 0; i < CREngine::CR_NUMBER; i++) |
---|
128 | this->_filters[i].clear(); |
---|
129 | |
---|
130 | this->_bReactive = false; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
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 |
---|
141 | */ |
---|
142 | bool CollisionFilter::operator()(const WorldEntity& entity) const |
---|
143 | { |
---|
144 | // PRINTF(0)("operator()(const WorldEntity& entity)\n"); |
---|
145 | |
---|
146 | // if there are no installed criterions just ommit and return |
---|
147 | if( !this->isReactive()) |
---|
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++ ) |
---|
152 | { |
---|
153 | TargetIteratorConst it = this->_filters[i].begin(); |
---|
154 | for(; it != this->_filters[i].end(); it++ ) |
---|
155 | { |
---|
156 | // PRINTF(0)("[%i] check %s is a %s?\n", i, entity.getClassName().c_str(), (*it).name().c_str()); |
---|
157 | if( unlikely(entity.isA(*it) ) ) |
---|
158 | return true; |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | return false; |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
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 | */ |
---|
173 | bool CollisionFilter::operator()(const WorldEntity& entity, const CREngine::ReactionType type) const |
---|
174 | { |
---|
175 | assert(&entity != NULL); |
---|
176 | |
---|
177 | // PRINTF(0)("operator()(const WorldEntity& entity, const CREngine::ReactionType type)\n"); |
---|
178 | // if there are no installed criterions just omit and return |
---|
179 | if( !this->isReactive()) |
---|
180 | return false; |
---|
181 | |
---|
182 | |
---|
183 | // goes through all registered filter criterions and looks for matches |
---|
184 | TargetIteratorConst it = this->_filters[type].begin(); |
---|
185 | for(; it != this->_filters[type].end(); it++ ) |
---|
186 | { |
---|
187 | // PRINTF(0)("size: %i - %s is a %s?\n", this->_filters[type].size(), entity.getClassName().c_str(), (*it).name().c_str()); |
---|
188 | if( entity.isA(*it)) |
---|
189 | return true; |
---|
190 | } |
---|
191 | |
---|
192 | return false; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | } // namespace end |
---|
198 | |
---|
199 | |
---|
200 | |
---|