SBSPSS/source/level/layercollision.cpp

432 lines
11 KiB
C++
Raw Normal View History

2001-02-02 19:39:16 +01:00
/******************************/
2001-02-02 20:16:49 +01:00
/*** Collision Layer Class ***/
2001-02-02 19:39:16 +01:00
/******************************/
#include "system\global.h"
#include <DStructs.h>
#include "utils\utils.h"
2001-02-02 20:16:49 +01:00
#include "LayerCollision.h"
2001-02-02 19:39:16 +01:00
2001-02-15 20:49:34 +01:00
#ifndef _FILEIO_HEADER_
#include "fileio\fileio.h"
#endif
2001-02-02 19:39:16 +01:00
2001-03-01 17:35:05 +01:00
// Manual collision height table (pkg)
u8 CLayerCollision::s_collisionTable[]=
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1,
9, 9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
16,16,15,15,14,14,13,13,12,12,11,11,10,10, 9, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,
16,15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
2001-05-30 17:30:29 +02:00
};
2001-02-02 19:39:16 +01:00
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
2001-02-02 20:16:49 +01:00
CLayerCollision::CLayerCollision(sLayerHdr *Hdr)
2001-02-02 19:39:16 +01:00
{
2001-02-02 20:16:49 +01:00
LayerHdr=Hdr;
Map=(u8*)MakePtr(Hdr,sizeof(sLayerHdr));
MapWidth=LayerHdr->Width;
MapHeight=LayerHdr->Height;
2001-07-13 21:03:55 +02:00
int TabH=MapHeight+COL_Y_OFS;
ColYTable=(u16*)MemAlloc(TabH*sizeof(u16),"ColYTable");
ASSERT(ColYTable);
for (int i=0; i<TabH; i++)
{
int Y=i-COL_Y_OFS;
if (Y<0)
{
ColYTable[i]=0;
}
else
{
ColYTable[i]=Y*MapWidth;
}
}
2001-02-02 19:39:16 +01:00
}
/*****************************************************************************/
2001-02-02 20:16:49 +01:00
CLayerCollision::~CLayerCollision()
2001-02-02 19:39:16 +01:00
{
}
/*****************************************************************************/
2001-02-02 20:16:49 +01:00
void CLayerCollision::shutdown()
2001-02-02 19:39:16 +01:00
{
2001-07-13 21:03:55 +02:00
MemFree(ColYTable);
2001-02-13 17:44:54 +01:00
}
2001-02-15 20:49:34 +01:00
/*****************************************************************************/
int CLayerCollision::getHeightFromGround(int _x,int _y,int _maxHeight)
{
int mapX,mapY,xFraction,yFraction;
int distanceFromGround;
int colHeight;
2001-02-16 23:53:16 +01:00
int maxHeightToCheck;
2001-02-15 20:49:34 +01:00
mapX=_x>>4;
mapY=(_y>>4)*MapWidth;
xFraction=_x&0x0f;
yFraction=16-(_y&0x0f);
distanceFromGround=0;
2001-05-09 14:52:54 +02:00
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-02-15 20:49:34 +01:00
if(colHeight)
{
// Inside a collision block.. find the nearest ground above this point
2001-02-16 23:53:16 +01:00
maxHeightToCheck=-_maxHeight-16; // Need to check one block more incase we cross onto a new block
2001-02-15 20:49:34 +01:00
while(colHeight==16)
{
mapY-=MapWidth;
distanceFromGround-=16;
2001-02-16 23:53:16 +01:00
if(distanceFromGround<=maxHeightToCheck)
2001-02-15 20:49:34 +01:00
{
return -_maxHeight;
}
2001-05-09 14:52:54 +02:00
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-02-15 20:49:34 +01:00
}
distanceFromGround+=yFraction-colHeight;
2001-02-16 23:53:16 +01:00
if(distanceFromGround<-_maxHeight)distanceFromGround=-_maxHeight;
2001-02-15 20:49:34 +01:00
}
else
{
// Not inside a collision block.. find the nearest ground below this point
2001-02-16 23:53:16 +01:00
maxHeightToCheck=_maxHeight+16; // Need to check one block more incase we cross onto a new block
2001-02-15 20:49:34 +01:00
while(colHeight==0)
{
mapY+=MapWidth;
distanceFromGround+=16;
2001-02-16 23:53:16 +01:00
if(distanceFromGround>=maxHeightToCheck)
2001-02-15 20:49:34 +01:00
{
return _maxHeight;
}
2001-05-09 14:52:54 +02:00
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-02-15 20:49:34 +01:00
}
distanceFromGround+=yFraction-colHeight;
2001-02-16 23:53:16 +01:00
if(distanceFromGround>_maxHeight)distanceFromGround=_maxHeight;
2001-02-15 20:49:34 +01:00
}
return distanceFromGround;
}
2001-07-06 00:27:34 +02:00
/*****************************************************************************/
2001-07-12 17:45:39 +02:00
int CLayerCollision::getHeightFromGroundExcluding(int _x,int _y,int _exclusion,int _maxHeight=32)
2001-07-06 00:27:34 +02:00
{
int mapX,mapY,xFraction,yFraction;
int distanceFromGround;
int colHeight;
int maxHeightToCheck;
mapX=_x>>4;
mapY=(_y>>4)*MapWidth;
xFraction=_x&0x0f;
yFraction=16-(_y&0x0f);
distanceFromGround=0;
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-07-12 17:45:39 +02:00
if ( (Map[mapX+mapY] & COLLISION_TYPE_MASK) == _exclusion )
2001-07-06 00:27:34 +02:00
{
colHeight = 0;
}
if(colHeight)
{
// Inside a collision block.. find the nearest ground above this point
maxHeightToCheck=-_maxHeight-16; // Need to check one block more incase we cross onto a new block
while(colHeight==16)
{
mapY-=MapWidth;
distanceFromGround-=16;
if(distanceFromGround<=maxHeightToCheck)
{
return -_maxHeight;
}
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-07-12 17:45:39 +02:00
if ( (Map[mapX+mapY] & COLLISION_TYPE_MASK) == _exclusion )
2001-07-06 00:27:34 +02:00
{
colHeight = 0;
}
}
distanceFromGround+=yFraction-colHeight;
if(distanceFromGround<-_maxHeight)distanceFromGround=-_maxHeight;
}
else
{
// Not inside a collision block.. find the nearest ground below this point
maxHeightToCheck=_maxHeight+16; // Need to check one block more incase we cross onto a new block
while(colHeight==0)
{
mapY+=MapWidth;
distanceFromGround+=16;
if(distanceFromGround>=maxHeightToCheck)
{
return _maxHeight;
}
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-07-12 17:45:39 +02:00
if ( (Map[mapX+mapY] & COLLISION_TYPE_MASK) == _exclusion )
2001-07-06 00:27:34 +02:00
{
colHeight = 0;
}
}
distanceFromGround+=yFraction-colHeight;
if(distanceFromGround>_maxHeight)distanceFromGround=_maxHeight;
}
return distanceFromGround;
}
2001-07-06 21:42:07 +02:00
/*****************************************************************************/
int CLayerCollision::getHeightFromGroundCart(int _x,int _y,int _maxHeight)
{
int mapX,mapY,xFraction,yFraction;
int distanceFromGround;
int colHeight;
int maxHeightToCheck;
mapX=_x>>4;
mapY=(_y>>4)*MapWidth;
xFraction=_x&0x0f;
yFraction=16-(_y&0x0f);
distanceFromGround=0;
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-07-10 18:09:56 +02:00
if ( (Map[mapX+mapY] & COLLISION_TYPE_MASK) == COLLISION_TYPE_FLAG_DAMAGE ||
(Map[mapX+mapY] & COLLISION_TYPE_MASK) == COLLISION_TYPE_FLAG_DEATH_FALL )
2001-07-06 21:42:07 +02:00
{
colHeight = 0;
}
if(colHeight)
{
// Inside a collision block.. find the nearest ground above this point
maxHeightToCheck=-_maxHeight-16; // Need to check one block more incase we cross onto a new block
while(colHeight==16)
{
mapY-=MapWidth;
distanceFromGround-=16;
if(distanceFromGround<=maxHeightToCheck)
{
return -_maxHeight;
}
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-07-10 18:09:56 +02:00
if ( (Map[mapX+mapY] & COLLISION_TYPE_MASK) == COLLISION_TYPE_FLAG_DAMAGE ||
(Map[mapX+mapY] & COLLISION_TYPE_MASK) == COLLISION_TYPE_FLAG_DEATH_FALL )
2001-07-06 21:42:07 +02:00
{
colHeight = 0;
}
}
distanceFromGround+=yFraction-colHeight;
if(distanceFromGround<-_maxHeight)distanceFromGround=-_maxHeight;
}
else
{
// Not inside a collision block.. find the nearest ground below this point
maxHeightToCheck=_maxHeight+16; // Need to check one block more incase we cross onto a new block
while(colHeight==0)
{
mapY+=MapWidth;
distanceFromGround+=16;
if(distanceFromGround>=maxHeightToCheck)
{
return _maxHeight;
}
colHeight=s_collisionTable[((Map[mapX+mapY]&COLLISION_TILE_MASK)*16)+xFraction];
2001-07-10 18:09:56 +02:00
if ( (Map[mapX+mapY] & COLLISION_TYPE_MASK) == COLLISION_TYPE_FLAG_DAMAGE ||
(Map[mapX+mapY] & COLLISION_TYPE_MASK) == COLLISION_TYPE_FLAG_DEATH_FALL )
2001-07-06 21:42:07 +02:00
{
colHeight = 0;
}
}
distanceFromGround+=yFraction-colHeight;
if(distanceFromGround>_maxHeight)distanceFromGround=_maxHeight;
}
return distanceFromGround;
}
2001-07-12 17:45:39 +02:00
/*****************************************************************************/
int CLayerCollision::getHeightFromGroundAmmo(int _x,int _y,int _maxHeight)
{
return( CLayerCollision::getHeightFromGroundExcluding( _x, _y, COLLISION_TYPE_FLAG_NORMAL, _maxHeight ) );
}
2001-02-13 17:44:54 +01:00
/*****************************************************************************/
#ifdef __SHOW_COLLISION__
#include "gfx\prim.h"
2001-02-13 23:36:06 +01:00
#include "pad\pads.h"
2001-03-08 22:58:14 +01:00
typedef struct{int r,g,b;} colrgb;
const colrgb s_typeColours[]={
{90,90,90},
2001-04-06 18:36:15 +02:00
{90,0,0}, // damage
{0,90,0}, // slippery
{90,0,90}, // electric
{0,90,90}, // sticky
{0,0,90}, // water
{45,45,90}, // solid
{90,45,45}, // death
2001-03-08 22:58:14 +01:00
};
typedef enum {SHOW_NONE,SHOW_OPAQUE,SHOW_SEMITRANS}ColShowType;
#if defined (__USER_paul__)
ColShowType showCollision=SHOW_OPAQUE;
2001-03-21 21:00:22 +01:00
#elif defined (__USER_art__) || defined (__USER_sbart__)
2001-03-08 22:58:14 +01:00
ColShowType showCollision=SHOW_SEMITRANS;
2001-02-13 17:46:16 +01:00
#else
2001-03-08 22:58:14 +01:00
ColShowType showCollision=SHOW_NONE;
2001-02-13 17:46:16 +01:00
#endif
2001-02-13 17:44:54 +01:00
void CLayerCollision::render(DVECTOR &MapPos)
{
2001-05-18 23:03:51 +02:00
if(PadGetDown(0)&PAD_L2)
2001-02-13 23:36:06 +01:00
{
2001-03-08 22:58:14 +01:00
showCollision=(ColShowType)(showCollision+1);
if(showCollision>SHOW_SEMITRANS)showCollision=SHOW_NONE;
2001-02-13 23:36:06 +01:00
}
2001-03-08 22:58:14 +01:00
if(showCollision!=SHOW_NONE)
2001-02-13 17:44:54 +01:00
{
2001-03-08 22:58:14 +01:00
int semiTrans;
int x,y;
int mapx,mapy;
int xoff,yoff;
u8 *coll;
POLY_F4 *f4;
POLY_F3 *f3;
const colrgb *colour;
semiTrans=showCollision==SHOW_SEMITRANS;
2001-02-13 17:44:54 +01:00
xoff=MapPos.vx&15;
yoff=MapPos.vy&15;
mapx=MapPos.vx/16;
2001-02-22 18:01:34 +01:00
mapy=MapPos.vy/16;
2001-02-13 17:44:54 +01:00
for(y=-yoff;y<(20*16)-yoff;y+=16)
{
coll=&Map[mapx+(mapy*MapWidth)];
for(x=-xoff;x<(33*16)-xoff;x+=16)
{
2001-03-08 22:58:14 +01:00
colour=&s_typeColours[((*coll)&COLLISION_TYPE_MASK)>>COLLISION_TYPE_FLAG_SHIFT];
2001-05-09 14:52:54 +02:00
switch((*coll)&COLLISION_TILE_MASK)
2001-02-13 17:44:54 +01:00
{
2001-02-13 21:24:05 +01:00
case 0:
break;
case 1:
f4=GetPrimF4();
setXYWH(f4,x,y,16,16);
2001-03-08 22:58:14 +01:00
setRGB0(f4,colour->r,colour->g,colour->b);
setSemiTrans(f4,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f4,0);
2001-02-13 21:24:05 +01:00
break;
2001-05-30 17:30:29 +02:00
case 2:
2001-02-13 21:24:05 +01:00
f3=GetPrimF3();
setXY3(f3,x+16,y+16,
x,y+16,
x+16,y+8);
2001-03-08 22:58:14 +01:00
setRGB0(f3,colour->r,colour->g,colour->b);
setSemiTrans(f3,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f3,0);
2001-02-13 21:24:05 +01:00
break;
2001-05-30 17:30:29 +02:00
case 3:
2001-02-13 21:24:05 +01:00
f3=GetPrimF3();
setXY3(f3,x,y+16,
x+16,y+16,
x,y+8);
2001-03-08 22:58:14 +01:00
setRGB0(f3,colour->r,colour->g,colour->b);
setSemiTrans(f3,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f3,0);
2001-02-13 21:24:05 +01:00
break;
2001-05-30 17:30:29 +02:00
case 4:
2001-02-13 21:24:05 +01:00
f4=GetPrimF4();
setXY4(f4,x+16,y,
x,y+8,
x+16,y+16,
x,y+16);
2001-03-08 22:58:14 +01:00
setRGB0(f4,colour->r,colour->g,colour->b);
setSemiTrans(f4,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f4,0);
2001-02-13 21:24:05 +01:00
break;
2001-05-30 17:30:29 +02:00
case 5:
2001-02-13 21:24:05 +01:00
f4=GetPrimF4();
setXY4(f4,x,y,
x+16,y+8,
x,y+16,
x+16,y+16);
2001-03-08 22:58:14 +01:00
setRGB0(f4,colour->r,colour->g,colour->b);
setSemiTrans(f4,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f4,0);
2001-02-13 21:24:05 +01:00
break;
2001-05-30 17:30:29 +02:00
case 6:
2001-02-13 21:24:05 +01:00
f3=GetPrimF3();
setXY3(f3,x+16,y+16,
x,y+16,
x+16,y);
2001-03-08 22:58:14 +01:00
setRGB0(f3,colour->r,colour->g,colour->b);
setSemiTrans(f3,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f3,0);
2001-02-13 21:24:05 +01:00
break;
2001-05-30 17:30:29 +02:00
case 7:
2001-02-13 21:24:05 +01:00
f3=GetPrimF3();
setXY3(f3,x,y+16,
x+16,y+16,
x,y);
2001-03-08 22:58:14 +01:00
setRGB0(f3,colour->r,colour->g,colour->b);
setSemiTrans(f3,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f3,0);
2001-02-13 21:24:05 +01:00
break;
2001-05-30 17:30:29 +02:00
/*
2001-02-14 16:47:13 +01:00
case 15:
f3=GetPrimF3();
setXY3(f3,x+16,y,
x,y,
x+16,y+16);
2001-03-08 22:58:14 +01:00
setRGB0(f3,colour->r,colour->g,colour->b);
setSemiTrans(f3,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f3,0);
2001-02-14 16:47:13 +01:00
break;
case 16:
f3=GetPrimF3();
setXY3(f3,x,y,
x+16,y,
x,y+16);
2001-03-08 22:58:14 +01:00
setRGB0(f3,colour->r,colour->g,colour->b);
setSemiTrans(f3,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f3,0);
2001-02-14 16:47:13 +01:00
break;
2001-05-30 17:30:29 +02:00
*/
2001-02-13 21:24:05 +01:00
default:
f4=GetPrimF4();
setXYWH(f4,x,y,16,16);
2001-03-08 22:58:14 +01:00
setRGB0(f4,255,0,0);
setSemiTrans(f4,semiTrans);
2001-05-02 01:27:30 +02:00
AddPrimToList(f4,0);
2001-02-13 21:24:05 +01:00
break;
2001-02-13 17:44:54 +01:00
}
coll++;
}
mapy++;
}
}
2001-02-02 19:39:16 +01:00
}
2001-02-13 17:44:54 +01:00
#endif