Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

collision reactions now only registered with collision tube. no compile

File size: 5.4 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   * @returns an instance to a collision object. instead of creating new object this ones can be resycled
92   */
93  Collision* CREngine::popCollisionObject()
94  {
95    if( !this->collisionsUnused.empty())
96    {
97      this->collisionsUsed.push_back(this->collisionsUnused.back());
98      this->collisionsUnused.pop_back();
99      return this->collisionsUsed.back();
100    }
101    else return NULL;
102  }
103
104  /**
105   * @return an instanco of a CollisionEvent object. instead of creating a new object this ones can be used and resycled
106   */
107  CollisionEvent* CREngine::popCollisionEventObject()
108  {
109    if( !this->collisionEventsUnused.empty())
110    {
111      this->collisionEventsUsed.push_back(this->collisionEventsUnused.back());
112      this->collisionEventsUnused.pop_back();
113      return this->collisionEventsUsed.back();
114    }
115    else return NULL;
116  }
117
118
119  /**
120   * flushes the CollisionHandles and restores the CREngine to the initial state
121   */
122  void CREngine::reset()
123  {
124    // first clear all CollisionHandles
125    std::vector<CollisionHandle*>::iterator it = this->collisionHandles.begin();
126    for(; it < this->collisionHandles.end(); it++)
127    {
128      (*it)->reset();
129      delete *it;
130    }
131
132    this->collisionHandles.clear();
133  }
134
135
136  /**
137   * subscribes a WorldEntity for a CollisionReaction
138   *  @param owner: the WE to subscribe
139   *  @param type: the type of collision reaction to perform
140   *  @return the newly created CollisionHandle
141   */
142  CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, ReactionType type)
143  {
144    CollisionHandle* ch = new CollisionHandle(owner, type);
145    this->collisionHandles.push_back(ch);
146
147    return ch;
148  }
149
150
151  /**
152   * unsubscribe reaction from the reaction list
153   *  @param collisionHandle the CollisionHandle to remove
154   *  @param returns true if worked collrectly
155   */
156  bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle)
157  {
158    std::vector<CollisionHandle*>::iterator it;
159    for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
160    {
161      if( *it == collisionHandle)
162      {
163        this->collisionHandles.erase(it);
164        delete collisionHandle;
165        return true;
166      }
167    }
168    return false;
169  }
170
171
172  /**
173   * processes the collisions by calling the EventHandlers
174   */
175  void CREngine::handleCollisions()
176  {
177    std::vector<CollisionHandle*>::iterator it;
178    for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
179    {
180      if( !(*it)->isDispatched() || (*it)->isContinuousPoll())  //does it have any collisions to report at all
181      {
182        (*it)->handleCollisions();
183      }
184    }
185    this->flushCollisions();
186  }
187
188
189  /**
190   * flushes all the collision lists and puts them to their initial state
191   */
192  void CREngine::flushCollisions()
193  {
194    std::vector<Collision*>::iterator it1 = this->collisionsUsed.begin();
195    for(; it1 < this->collisionsUsed.end(); it1++)
196      this->collisionsUnused.push_back(*it1);
197
198    std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUsed.begin();
199    for(; it2 < this->collisionEventsUsed.end(); it2++)
200      this->collisionEventsUnused.push_back(*it2);
201
202    this->collisionsUsed.clear();
203    this->collisionEventsUsed.clear();
204  }
205
206
207  void CREngine::debug()
208  {
209  }
210
211}
Note: See TracBrowser for help on using the repository browser.