Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added namspacing to collision reaction. now comes the harder part :D

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