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, 18 years ago

removed more bugs, should compile now

File size: 4.6 KB
RevLine 
[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]30namespace 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  {
[9896]144    // if there are no installed criterions just ommit and
[9988]145    if( this->isReactive())
[9896]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++ )
[9889]150    {
[9990]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;
[9889]155    }
156
157    return false;
[8190]158  }
159
160
[9898]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   */
[9939]168  bool CollisionFilter::operator()(const WorldEntity& entity, const CREngine::ReactionType type) const
[9898]169  {
[9980]170    // if there are no installed criterions just omit and return
[9988]171    if( !this->isReactive())
[9898]172      return false;
[8190]173
[9898]174    // goes through all registered filter criterions and looks for matches
[9990]175    TargetIteratorConst it = this->_filters[type].begin();
[9939]176    for(; it != this->_filters[type].end(); it++ )
177      if( unlikely(entity.isA(*it)))
[9898]178        return true;
[8190]179
[9898]180    return false;
181  }
[8190]182
183
[9898]184
185} // namespace end
186
187
188
Note: See TracBrowser for help on using the repository browser.