Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cr/src/lib/collision_reaction/cr_engine.cc @ 7949

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

cr: got segfault in the cleanup function: list manipulation. flush continue tomorrow

File size: 3.0 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_handle.h"
22#include "cr_defs.h"
23
24#include "cr_engine.h"
25
26using namespace std;
27
28
29/**
30 * standard constructor
31 */
32CREngine::CREngine ()
33  : BaseObject()
34{
35   this->setClassID(CL_CR_ENGINE, "CREngine");
36   this->setName("CREngine");
37
38   this->init();
39}
40
41/**
42 *  the singleton reference to this class
43 */
44CREngine* CREngine::singletonRef = NULL;
45
46/**
47   @brief standard deconstructor
48 */
49CREngine::~CREngine ()
50{
51  CREngine::singletonRef = NULL;
52
53  if( this->cachedCollisions.size() != CR_MAX_COLLISIONS)
54    PRINTF(0)("CollisionReaction Error: cache size missmatch: %i of %i\n", this->cachedCollisions.size(), CR_NUMBER);
55
56  this->reset();
57
58   while( !this->cachedCollisions.empty())
59     delete this->cachedCollisions.back();
60  this->cachedCollisions.clear();
61}
62
63/**
64 * inits the CREngine to a working state
65 */
66void CREngine::init()
67{
68  // create a list of Collision events (precaching)
69  Collision* collisions = new Collision[CR_MAX_COLLISIONS];
70  for( int i = 0; i < CR_MAX_COLLISIONS; i++)
71    this->cachedCollisions.push_back(&collisions[i]);
72}
73
74
75/**
76 * flushes the CollisionHandles and restores the CREngine to the initial state
77 */
78void CREngine::reset()
79{
80  // first clear all CollisionHandles
81
82  while( !this->collisionHandles.empty())
83  {
84    this->collisionHandles.front()->reset();
85    delete this->collisionHandles.front();
86  }
87  this->collisionHandles.clear();
88}
89
90
91/**
92 * subscribes a WorldEntity for a CollisionReaction
93 *  @param owner: the WE to subscribe
94 *  @param type: the type of collision reaction to perform
95 *  @return the newly created CollisionHandle
96 */
97CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type)
98{
99  CollisionHandle* ch = new CollisionHandle(owner, type);
100  this->collisionHandles.push_back(ch);
101}
102
103
104/**
105 * unsubscribe reaction from the reaction list
106 *  @param collisionHandle the CollisionHandle to remove
107 *  @param returns true if worked collrectly
108 */
109bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle)
110{
111  std::vector<CollisionHandle*>::iterator it;
112  for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
113  {
114    if( *it == collisionHandle)
115    {
116      this->collisionHandles.erase(it);
117      delete collisionHandle;
118      return true;
119    }
120  }
121  return false;
122}
123
124
125void CREngine::handleCollisions()
126{
127  std::vector<CollisionHandle*>::iterator it;
128  for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
129  {
130    if( (*it)->isCollided())
131    {
132      (*it)->handleCollisions();
133    }
134  }
135}
136
137
138
139void CREngine::debug()
140{
141
142}
143
Note: See TracBrowser for help on using the repository browser.