Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/coll_rect/src/lib/collision_reaction/collision_filter.cc @ 9990

Last change on this file since 9990 was 9990, checked in by patrick, 17 years ago

removed more bugs, should compile now

File size: 4.6 KB
Line 
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
30namespace 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    // if there are no installed criterions just ommit and
145    if( this->isReactive())
146      return false;
147
148    // goes through all registered filter criterions and looks for matches
149    for( int i = 0; i < CREngine::CR_NUMBER; i++ )
150    {
151      std::vector<ClassID>::const_iterator it = this->_filters[i].begin();
152      for(; it != this->_filters[i].end(); i++ )
153        if( unlikely(entity.isA(*it) ) )
154          return true;
155    }
156
157    return false;
158  }
159
160
161  /**
162  * tests if the owner WorldEntity is listening to collisions from another specif WorldEntity entity
163  *  @param entity WorldEntity to test against
164  *
165  * This is the most important interface function of this class: it performs a check and returns true
166  * if the WorldEntity entity is actualy responsive for a certain other WorldEntity
167   */
168  bool CollisionFilter::operator()(const WorldEntity& entity, const CREngine::ReactionType type) const
169  {
170    // if there are no installed criterions just omit and return
171    if( !this->isReactive())
172      return false;
173
174    // goes through all registered filter criterions and looks for matches
175    TargetIteratorConst it = this->_filters[type].begin();
176    for(; it != this->_filters[type].end(); it++ )
177      if( unlikely(entity.isA(*it)))
178        return true;
179
180    return false;
181  }
182
183
184
185} // namespace end
186
187
188
Note: See TracBrowser for help on using the repository browser.