Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/coll_rect/src/lib/collision_reaction/cr_engine.cc @ 9890

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

intruducing new container collision_tube. which serves as a central collision registration tube.

File size: 4.6 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
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   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION
17
18
19
20#include "collision.h"
21#include "collision_event.h"
22#include "collision_handle.h"
23#include "cr_defs.h"
24
25#include "cr_engine.h"
26
27#include "debug.h"
28
29namespace CoRe
30{
31
32  ObjectListDefinition(CREngine);
33
34
35  /**
36   * standard constructor
37   */
38  CREngine::CREngine ()
39      : BaseObject()
40  {
41    this->registerObject(this, CREngine::_objectList);
42    this->setName("CREngine");
43
44    this->init();
45  }
46
47  /**
48   *  the singleton reference to this class
49   */
50  CREngine* CREngine::singletonRef = NULL;
51
52  /**
53     @brief standard deconstructor
54   */
55  CREngine::~CREngine ()
56  {
57    CREngine::singletonRef = NULL;
58
59    if( this->collisionsUnused.size() != CR_MAX_COLLISIONS)
60      PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS);
61    if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS)
62      PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS);
63
64    this->reset();
65
66    std::vector<Collision*>::iterator it1 = this->collisionsUnused.begin();
67    for(; it1 < this->collisionsUnused.end(); it1++)
68      delete *it1;
69    std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUnused.begin();
70    for(; it2 < this->collisionEventsUnused.end(); it2++)
71      delete *it2;
72
73    this->collisionsUnused.clear();
74    this->collisionEventsUnused.clear();
75  }
76
77  /**
78   * inits the CREngine to a working state
79   */
80  void CREngine::init()
81  {
82    // create a list of Collision events (precaching)
83    for( int i = 0; i < CR_MAX_COLLISIONS; i++)
84      this->collisionsUnused.push_back(new Collision());
85    for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++)
86      this->collisionEventsUnused.push_back(new CollisionEvent());
87  }
88
89
90  /**
91   * flushes the CollisionHandles and restores the CREngine to the initial state
92   */
93  void CREngine::reset()
94  {
95    // first clear all CollisionHandles
96
97    std::vector<CollisionHandle*>::iterator it = this->collisionHandles.begin();
98    for(; it < this->collisionHandles.end(); it++)
99    {
100      (*it)->reset();
101      delete *it;
102    }
103
104    this->collisionHandles.clear();
105  }
106
107
108  /**
109   * subscribes a WorldEntity for a CollisionReaction
110   *  @param owner: the WE to subscribe
111   *  @param type: the type of collision reaction to perform
112   *  @return the newly created CollisionHandle
113   */
114  CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type)
115  {
116    CollisionHandle* ch = new CollisionHandle(owner, type);
117    this->collisionHandles.push_back(ch);
118
119    return ch;
120  }
121
122
123  /**
124   * unsubscribe reaction from the reaction list
125   *  @param collisionHandle the CollisionHandle to remove
126   *  @param returns true if worked collrectly
127   */
128  bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle)
129  {
130    std::vector<CollisionHandle*>::iterator it;
131    for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
132    {
133      if( *it == collisionHandle)
134      {
135        this->collisionHandles.erase(it);
136        delete collisionHandle;
137        return true;
138      }
139    }
140    return false;
141  }
142
143
144  /**
145   * processes the collisions by calling the EventHandlers
146   */
147  void CREngine::handleCollisions()
148  {
149    std::vector<CollisionHandle*>::iterator it;
150    for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
151    {
152      if( !(*it)->isDispatched() || (*it)->isContinuousPoll())  //does it have any collisions to report at all
153      {
154        (*it)->handleCollisions();
155      }
156    }
157    this->flushCollisions();
158  }
159
160
161  /**
162   * flushes all the collision lists and puts them to their initial state
163   */
164  void CREngine::flushCollisions()
165  {
166    std::vector<Collision*>::iterator it1 = this->collisionsUsed.begin();
167    for(; it1 < this->collisionsUsed.end(); it1++)
168      this->collisionsUnused.push_back(*it1);
169
170    std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUsed.begin();
171    for(; it2 < this->collisionEventsUsed.end(); it2++)
172      this->collisionEventsUnused.push_back(*it2);
173
174    this->collisionsUsed.clear();
175    this->collisionEventsUsed.clear();
176  }
177
178
179  void CREngine::debug()
180  {
181  }
182
183}
Note: See TracBrowser for help on using the repository browser.