Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9896 was 9896, checked in by patrick, 18 years ago

more design, thinner interface

File size: 3.7 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#include "collision_reaction.h"
24
25#include "cr_object_damage.h"
26#include "cr_physics_ground_walk.h"
27#include "cr_physics_full_walk.h"
28
29#include "debug.h"
30
31#include <vector>
32
33
34namespace CoRe
35{
36
37  ObjectListDefinition(CollisionFilter);
38
39  /**
40   * standard constructor
41   * @todo this constructor is not jet implemented - do it
42  */
43  CollisionFilter::CollisionFilter (WorldEntity* owner)
44  {
45    this->registerObject(this, CollisionFilter::_objectList);
46
47    this->_owner = owner;
48
49    this->_bContinuousPoll = false;
50    this->_bStopOnFirstCollision = false;
51    this->_bReactive = false;
52  }
53
54
55  /**
56   * standard deconstructor
57  */
58  CollisionFilter::~CollisionFilter ()
59  {
60    this->unsubscribeReactions();
61  }
62
63
64  /**
65   * subscribe reaction
66   */
67  void CollisionFilter::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1)
68  {
69    // check if its a valid type
70    if( likely(this->validCRType( type)))
71    {
72      //check if this or any parent class isn't already registered
73      std::vector<ClassID>::iterator it = this->_filters[type].begin();
74      for(; it != this->_filters[type].end(); it++)
75      {
76        if( unlikely(*it == target1))
77          return;
78      }
79
80      // so subscribe the reaction finaly
81      this->_filters[type].push_back(target1);
82      this->_bReactive = true;
83    }
84  }
85
86
87  /**
88   * subscribe for a reaction
89   */
90  void CollisionFilter::subscribeReaction(CoRe::CREngine::ReactionType type, const ClassID& target1, const ClassID& target2)
91  {
92    this->subscribeReaction(type, target1);
93    this->subscribeReaction(type, target2);
94  }
95
96
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);
105  }
106
107  /**
108   * unsubscribe from a specific collision reaction
109   */
110  void CollisionFilter::unsubscribeReaction(CoRe::CREngine::ReactionType type)
111  {
112    if( likely(this->validCRType( type)))
113      this->_filters[type].clear();
114  }
115
116  /**
117   * unsubscribe from all collision reactions
118   */
119  void CollisionFilter::unsubscribeReactions()
120  {
121    for(int i = 0; i < CREngine::CR_NUMBER; i++)
122      this->_filters[i].clear();
123  }
124
125
126
127  /**
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
133   */
134  bool CollisionFilter::operator()(const WorldEntity* entity) const
135  {
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++ )
142    {
143      std::vector<ClassID>::const_iterator it = this->_filters[i].begin();
144      for(; it != this->_filters[i].end(); i++ )
145        if( unlikely(entity->isA(*it)))
146          return true;
147    }
148
149    return false;
150  }
151
152
153
154}
155
156
157
Note: See TracBrowser for help on using the repository browser.