This commit is contained in:
Paul 2001-04-06 21:25:18 +00:00
parent 461123580e
commit 0cb7d8d491
5 changed files with 159 additions and 28 deletions

View file

@ -256,6 +256,48 @@ void CThingManager::processEventAllThings(GAME_EVENT _event,CThing *_sourceThin
}
}
/*----------------------------------------------------------------------
Function:
Purpose: Searches through a list of things to check for collision against an area.
The first time this is called, _continue should be false. If no colliding things are found then
NULL will be returned. If a colliding thing is found then it's address gets returned. To continue
searching through the list for the next colliding thing, call the function again with _continue set
to true.
NB: This function could probly cause weird bugs if not used properly! BE AWARE!
Params: *_area Area to check against
_type Type of thing to search for
_continue If false then the list is searched from the start, if true then the search continues
from the last thing that was found ( um.. see above )
Returns:
---------------------------------------------------------------------- */
CThing *CThingManager::checkCollisionAreaAgainstThings(CRECT *_area,int _type,int _continue)
{
static CThing *thing=NULL;
ASSERT(_type<CThing::MAX_TYPE);
if(_continue)
{
ASSERT(thing);
thing=thing->m_nextThing;
}
else
{
thing=s_thingLists[_type];
}
while(thing)
{
if(thing->canCollide()&&
thing->checkCollisionAgainstArea(_area))
{
return thing;
}
thing=thing->m_nextThing;
}
return NULL;
}
/*----------------------------------------------------------------------
Function:
Purpose:
@ -647,6 +689,29 @@ int CThing::checkCollisionAgainst(CThing *_thisThing, int _frames)
return collided;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int CThing::checkCollisionAgainstArea(CRECT *_rect)
{
CRECT thisRect;
int ret;
thisRect=getCollisionArea();
ret=false;
if(((thisRect.x1>=_rect->x1&&thisRect.x1<=_rect->x2)||(thisRect.x2>=_rect->x1&&thisRect.x2<=_rect->x2)||(thisRect.x1<=_rect->x1&&thisRect.x2>=_rect->x2))&&
((thisRect.y1>=_rect->y1&&thisRect.y1<=_rect->y2)||(thisRect.y2>=_rect->y1&&thisRect.y2<=_rect->y2)||(thisRect.y1<=_rect->y1&&thisRect.y2>=_rect->y2)))
{
ret=true;
}
return ret;
}
/*----------------------------------------------------------------------
Function:
Purpose:

View file

@ -38,6 +38,14 @@
Structure defintions
-------------------- */
// Collision rectangle definition
typedef struct
{
int x1,y1,x2,y2;
}
CRECT;
// Thing manager class
class CThingManager
{
@ -49,6 +57,8 @@ public:
static void renderAllThings();
static void processEventAllThings(GAME_EVENT _event,class CThing *_sourceThing);
static CThing* checkCollisionAreaAgainstThings(CRECT *_area,int _type,int _continue);
protected:
static void addToThingList(class CThing *_this);
static void removeFromThingList(CThing *_this);
@ -126,13 +136,6 @@ public:
// -- Collision --
public:
typedef struct
{
int x1,y1,x2,y2;
}
CRECT;
DVECTOR getCollisionCentre() {return m_collisionCentre;}
int getCollisionRadius() {return m_collisionRadius;}
CRECT getCollisionArea() {return m_collisionArea;}
@ -143,6 +146,7 @@ public:
virtual int canCollide() {return true;}
virtual int checkCollisionAgainst(CThing *_thisThing, int _frames);
int checkCollisionAgainstArea(CRECT *_rect);
void updateCollisionArea();
virtual void collidedWith(CThing *_thisThing) {;}
virtual void setHasPlatformCollided( bool newVal ) {;}
@ -150,6 +154,8 @@ public:
virtual s32 getNewYPos( CThing *_thisThing );
void setNewCollidedPos(DVECTOR newPos) {m_newCollidedPos = newPos;}
void setCentreCollision(bool newCentreCollision) {m_centreCollision = newCentreCollision;}
protected:
void setCollisionSize(int _w,int _h);
void setCollisionCentreOffset(int _x,int _y) {m_collisionCentreOffset.vx=_x;m_collisionCentreOffset.vy=_y;}