Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 4, 2006, 4:39:45 PM (17 years ago)
Author:
patrick
Message:

merged the temp branch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/coll_rect.merge/src/lib/collision_reaction/cr_engine.cc

    r9869 r10010  
    1717
    1818
     19#include "cr_object_damage.h"
     20#include "cr_physics_full_walk.h"
     21#include "cr_physics_ground_walk.h"
    1922
    2023#include "collision.h"
    2124#include "collision_event.h"
    22 #include "collision_handle.h"
     25#include "collision_filter.h"
     26#include "collision_tube.h"
    2327#include "cr_defs.h"
    2428
     
    2731#include "debug.h"
    2832
    29 
    30 
    31 ObjectListDefinition(CREngine);
    32 /**
    33  * standard constructor
    34  */
    35 CREngine::CREngine ()
    36   : BaseObject()
     33namespace CoRe
    3734{
    38   this->registerObject(this, CREngine::_objectList);
    39    this->setName("CREngine");
    40 
    41    this->init();
     35
     36  ObjectListDefinition(CREngine);
     37
     38
     39  /**
     40   * standard constructor
     41   */
     42  CREngine::CREngine ()
     43      : BaseObject()
     44  {
     45    this->registerObject(this, CREngine::_objectList);
     46    this->setName("CREngine");
     47
     48    this->init();
     49  }
     50
     51  /**
     52   *  the singleton reference to this class
     53   */
     54  CREngine* CREngine::singletonRef = NULL;
     55
     56  /**
     57     @brief standard deconstructor
     58   */
     59  CREngine::~CREngine ()
     60  {
     61    CREngine::singletonRef = NULL;
     62
     63    if( this->collisionsUnused.size() != CR_MAX_COLLISIONS)
     64      PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS);
     65    if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS)
     66      PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS);
     67
     68    this->reset();
     69
     70    CollisionIterator it1 = this->collisionsUnused.begin();
     71    for(; it1 < this->collisionsUnused.end(); it1++)
     72      delete *it1;
     73    CollisionEventIterator it2 = this->collisionEventsUnused.begin();
     74    for(; it2 < this->collisionEventsUnused.end(); it2++)
     75      delete *it2;
     76
     77    this->collisionsUnused.clear();
     78    this->collisionEventsUnused.clear();
     79  }
     80
     81
     82  /**
     83   * inits the CREngine to a working state
     84   */
     85  void CREngine::init()
     86  {
     87    // precaching:
     88    // create a list of Collisions and CollisionEvents for fast object recycling purposes
     89    for( int i = 0; i < CR_MAX_COLLISIONS; i++)
     90      this->collisionsUnused.push_back(new Collision());
     91    for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++)
     92      this->collisionEventsUnused.push_back(new CollisionEvent());
     93
     94
     95    // push the collision reaction object on the list in the right order
     96
     97    // physical reactions
     98    this->_reactionList[CREngine::CR_PHYSICS_MOMENTUM]      = NULL;
     99    this->_reactionList[CREngine::CR_PHYSICS_STEP_BACK]     = NULL;
     100    this->_reactionList[CREngine::CR_PHYSICS_GROUND_WALK]   = new CRPhysicsGroundWalk();
     101    this->_reactionList[CREngine::CR_PHYSICS_FULL_WALK]     = new CRPhysicsFullWalk();
     102    this->_reactionList[CREngine::CR_PHYSICS_DAMAGE]        = NULL;
     103    // object based reactions
     104    this->_reactionList[CREngine::CR_OBJECT_DAMAGE]         = new CRObjectDamage();
     105    this->_reactionList[CREngine::CR_OBJECT_PICKUP]         = NULL;
     106    // misc reactions
     107    this->_reactionList[CREngine::CR_VERTEX_TRAFO]          = NULL;
     108    this->_reactionList[CREngine::CR_SPECIAL_CALLBACK]      = NULL;
     109  }
     110
     111
     112  /**
     113   * @returns an instance to a collision object. instead of creating new object this ones can be resycled
     114   */
     115  Collision* CREngine::popCollisionObject()
     116  {
     117    if( !this->collisionsUnused.empty())
     118    {
     119      this->collisionsUsed.push_back(this->collisionsUnused.back());
     120      this->collisionsUnused.pop_back();
     121      return this->collisionsUsed.back();
     122    }
     123    else
     124    {
     125      PRINTF(0)("There is no Collision Object left in the precache table, fatal error will cause segfault, change CR_MAX_COLLISIONS\n");
     126      assert(false);
     127      return NULL;
     128    }
     129  }
     130
     131
     132  /**
     133   * @return an instanco of a CollisionEvent object. instead of creating a new object this ones can be used and resycled
     134   */
     135  CollisionEvent* CREngine::popCollisionEventObject()
     136  {
     137    if( !this->collisionEventsUnused.empty())
     138    {
     139      this->collisionEventsUsed.push_back(this->collisionEventsUnused.back());
     140      this->collisionEventsUnused.pop_back();
     141      return this->collisionEventsUsed.back();
     142    }
     143    else
     144    {
     145      PRINTF(0)("There is no Collision Object left in the precache table, fatal error will cause segfault, change CR_MAX_COLLISION_EVENTS\n");
     146      assert(false);
     147      return NULL;
     148    }
     149  }
     150
     151
     152  /**
     153   * handles all collisions in registered in this tube
     154   */
     155  void CREngine::handleCollisions()
     156  {
     157    // for all collisions:
     158    CollisionIterator ci = CollisionTube::getInstance()->begin();
     159    for(; ci < CollisionTube::getInstance()->end(); ci++)
     160    {
     161      for( int i = CREngine::CR_PHYSICS_MOMENTUM; i < CREngine::CR_NUMBER; i++)
     162      {
     163        if( _reactionList[i] == NULL)
     164          continue;
     165
     166        assert((*ci)->getEntityA() != NULL && (*ci)->getEntityB() != NULL);
     167
     168//         PRINTF(0)("CR CHECK: collision between: %s, %s\n", (*ci)->getEntityA()->getClassName().c_str(), (*ci)->getEntityB()->getClassName().c_str());
     169
     170        // check if entity A or B is subscibed for this event
     171        if( (*ci)->getEntityA()->isReactive(*(*ci)->getEntityB(), (CREngine::ReactionType)i) ||
     172            (*ci)->getEntityB()->isReactive(*(*ci)->getEntityA(), (CREngine::ReactionType)i))
     173        {
     174          this->_reactionList[i]->reactToCollision(*ci);
     175          PRINTF(0)("executing reaction: %s, between %s - %s\n", this->_reactionList[i]->getClassName().c_str(), (*ci)->getEntityA()->getClassName().c_str(), (*ci)->getEntityB()->getClassName().c_str());
     176        }
     177      }
     178      (*ci)->reset();
     179    }
     180
     181    this->reset();
     182  }
     183
     184
     185  /**
     186   * flushes all the collision lists and puts them to their initial state
     187   */
     188  void CREngine::reset()
     189  {
     190    CollisionIterator it1 = this->collisionsUsed.begin();
     191    for(; it1 < this->collisionsUsed.end(); it1++)
     192      this->collisionsUnused.push_back(*it1);
     193
     194    CollisionEventIterator it2 = this->collisionEventsUsed.begin();
     195    for(; it2 < this->collisionEventsUsed.end(); it2++)
     196      this->collisionEventsUnused.push_back(*it2);
     197
     198    this->collisionsUsed.clear();
     199    this->collisionEventsUsed.clear();
     200
     201    CollisionTube::getInstance()->reset();
     202  }
     203
     204
     205  void CREngine::debug()
     206  {
     207  }
     208
    42209}
    43 
    44 /**
    45  *  the singleton reference to this class
    46  */
    47 CREngine* CREngine::singletonRef = NULL;
    48 
    49 /**
    50    @brief standard deconstructor
    51  */
    52 CREngine::~CREngine ()
    53 {
    54   CREngine::singletonRef = NULL;
    55 
    56   if( this->collisionsUnused.size() != CR_MAX_COLLISIONS)
    57     PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS);
    58   if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS)
    59     PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS);
    60 
    61   this->reset();
    62 
    63   std::vector<Collision*>::iterator it1 = this->collisionsUnused.begin();
    64   for(; it1 < this->collisionsUnused.end(); it1++)
    65     delete *it1;
    66   std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUnused.begin();
    67   for(; it2 < this->collisionEventsUnused.end(); it2++)
    68     delete *it2;
    69 
    70   this->collisionsUnused.clear();
    71   this->collisionEventsUnused.clear();
    72 }
    73 
    74 /**
    75  * inits the CREngine to a working state
    76  */
    77 void CREngine::init()
    78 {
    79   // create a list of Collision events (precaching)
    80   for( int i = 0; i < CR_MAX_COLLISIONS; i++)
    81     this->collisionsUnused.push_back(new Collision());
    82   for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++)
    83     this->collisionEventsUnused.push_back(new CollisionEvent());
    84 }
    85 
    86 
    87 /**
    88  * flushes the CollisionHandles and restores the CREngine to the initial state
    89  */
    90 void CREngine::reset()
    91 {
    92   // first clear all CollisionHandles
    93 
    94   std::vector<CollisionHandle*>::iterator it = this->collisionHandles.begin();
    95   for(; it < this->collisionHandles.end(); it++)
    96   {
    97     (*it)->reset();
    98     delete *it;
    99   }
    100 
    101   this->collisionHandles.clear();
    102 }
    103 
    104 
    105 /**
    106  * subscribes a WorldEntity for a CollisionReaction
    107  *  @param owner: the WE to subscribe
    108  *  @param type: the type of collision reaction to perform
    109  *  @return the newly created CollisionHandle
    110  */
    111 CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type)
    112 {
    113   CollisionHandle* ch = new CollisionHandle(owner, type);
    114   this->collisionHandles.push_back(ch);
    115 
    116   return ch;
    117 }
    118 
    119 
    120 /**
    121  * unsubscribe reaction from the reaction list
    122  *  @param collisionHandle the CollisionHandle to remove
    123  *  @param returns true if worked collrectly
    124  */
    125 bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle)
    126 {
    127   std::vector<CollisionHandle*>::iterator it;
    128   for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)  {
    129     if( *it == collisionHandle) {
    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  */
    142 void CREngine::handleCollisions()
    143 {
    144   std::vector<CollisionHandle*>::iterator it;
    145   for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
    146   {
    147     if( !(*it)->isDispatched() || (*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  */
    159 void CREngine::flushCollisions()
    160 {
    161   std::vector<Collision*>::iterator it1 = this->collisionsUsed.begin();
    162   for(; it1 < this->collisionsUsed.end(); it1++)
    163     this->collisionsUnused.push_back(*it1);
    164 
    165   std::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 
    174 void CREngine::debug()
    175 {
    176 
    177 }
    178 
Note: See TracChangeset for help on using the changeset viewer.