Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 7, 2006, 3:00:01 PM (19 years ago)
Author:
patrick
Message:

trunk: merged the cr branche to trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/collision_reaction/cr_engine.cc

    r7927 r8190  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION
    1717
     18
     19
     20#include "collision.h"
     21#include "collision_event.h"
     22#include "collision_handle.h"
     23#include "cr_defs.h"
     24
    1825#include "cr_engine.h"
    1926
     
    2532 */
    2633CREngine::CREngine ()
     34  : BaseObject()
    2735{
    2836   this->setClassID(CL_CR_ENGINE, "CREngine");
    2937   this->setName("CREngine");
    3038
     39   this->init();
    3140}
    3241
     
    4251{
    4352  CREngine::singletonRef = NULL;
     53
     54  if( this->collisionsUnused.size() != CR_MAX_COLLISIONS)
     55    PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS);
     56  if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS)
     57    PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS);
     58
     59  this->reset();
     60
     61  vector<Collision*>::iterator it1 = this->collisionsUnused.begin();
     62  for(; it1 < this->collisionsUnused.end(); it1++)
     63    delete *it1;
     64  vector<CollisionEvent*>::iterator it2 = this->collisionEventsUnused.begin();
     65  for(; it2 < this->collisionEventsUnused.end(); it2++)
     66    delete *it2;
     67
     68  this->collisionsUnused.clear();
     69  this->collisionEventsUnused.clear();
     70}
     71
     72/**
     73 * inits the CREngine to a working state
     74 */
     75void CREngine::init()
     76{
     77  // create a list of Collision events (precaching)
     78  for( int i = 0; i < CR_MAX_COLLISIONS; i++)
     79    this->collisionsUnused.push_back(new Collision());
     80  for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++)
     81    this->collisionEventsUnused.push_back(new CollisionEvent());
    4482}
    4583
    4684
     85/**
     86 * flushes the CollisionHandles and restores the CREngine to the initial state
     87 */
     88void CREngine::reset()
     89{
     90  // first clear all CollisionHandles
    4791
     92  vector<CollisionHandle*>::iterator it = this->collisionHandles.begin();
     93  for(; it < this->collisionHandles.end(); it++)
     94  {
     95    (*it)->reset();
     96    delete *it;
     97  }
    4898
    49 CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type, int nrOfTargets, ...)
    50 {
    51 
    52   va_list itemlist;
    53   va_start (itemlist, type);
    54 //  for (int i = 0; i < nrOfTargets; i++)
    55   //  this->targetList.push_back(va_arg(itemlist, int));
    56   va_end(itemlist);
     99  this->collisionHandles.clear();
    57100}
    58101
    59102
     103/**
     104 * subscribes a WorldEntity for a CollisionReaction
     105 *  @param owner: the WE to subscribe
     106 *  @param type: the type of collision reaction to perform
     107 *  @return the newly created CollisionHandle
     108 */
     109CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type)
     110{
     111  CollisionHandle* ch = new CollisionHandle(owner, type);
     112  this->collisionHandles.push_back(ch);
     113
     114  return ch;
     115}
    60116
    61117
     118/**
     119 * unsubscribe reaction from the reaction list
     120 *  @param collisionHandle the CollisionHandle to remove
     121 *  @param returns true if worked collrectly
     122 */
     123bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle)
     124{
     125  std::vector<CollisionHandle*>::iterator it;
     126  for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
     127  {
     128    if( *it == collisionHandle)
     129    {
     130      this->collisionHandles.erase(it);
     131      delete collisionHandle;
     132      return true;
     133    }
     134  }
     135  return false;
     136}
     137
     138
     139/**
     140 * processes the collisions by calling the EventHandlers
     141 */
     142void CREngine::handleCollisions()
     143{
     144  std::vector<CollisionHandle*>::iterator it;
     145  for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
     146  {
     147    if( (*it)->isCollided() || (*it)->isContinuousPoll())  //does it have any collisions to report at all
     148    {
     149      (*it)->handleCollisions();
     150    }
     151  }
     152  this->flushCollisions();
     153}
     154
     155
     156/**
     157 * flushes all the collision lists and puts them to their initial state
     158 */
     159void CREngine::flushCollisions()
     160{
     161  vector<Collision*>::iterator it1 = this->collisionsUsed.begin();
     162  for(; it1 < this->collisionsUsed.end(); it1++)
     163    this->collisionsUnused.push_back(*it1);
     164
     165  vector<CollisionEvent*>::iterator it2 = this->collisionEventsUsed.begin();
     166  for(; it2 < this->collisionEventsUsed.end(); it2++)
     167    this->collisionEventsUnused.push_back(*it2);
     168
     169  this->collisionsUsed.clear();
     170  this->collisionEventsUsed.clear();
     171}
     172
     173
     174void CREngine::debug()
     175{
     176
     177}
     178
Note: See TracChangeset for help on using the changeset viewer.