SBSPSS/source/platform/pcart.cpp

430 lines
8.4 KiB
C++
Raw Normal View History

2001-04-23 20:58:30 +02:00
/*=========================================================================
pcart.cpp
Author: CRB
Created:
Project: Spongebob
Purpose:
Copyright (c) 2001 Climax Development Ltd
===========================================================================*/
#ifndef __PLATFORM_PCART_H__
#include "platform\pcart.h"
#endif
2001-04-23 22:40:13 +02:00
#ifndef __GAME_GAME_H__
#include "game\game.h"
#endif
2001-05-08 20:57:00 +02:00
#ifndef __UTILS_HEADER__
#include "utils\utils.h"
#endif
#ifndef __VID_HEADER_
#include "system\vid.h"
#endif
2001-04-23 20:58:30 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CNpcCartPlatform::postInit()
{
2001-05-05 15:54:34 +02:00
CNpcPlatform::postInit();
2001-05-04 18:20:10 +02:00
m_npcPath.setPathType( CNpcPath::SINGLE_USE_PATH );
2001-05-09 23:27:23 +02:00
m_carSpeed = m_speed << 8;
2001-06-11 21:32:41 +02:00
m_isActivated = false;
sBBox boundingBox = m_modelGfx->GetBBox();
boundingBox.YMin = boundingBox.YMax - 32;
setCollisionSize( ( boundingBox.XMax - boundingBox.XMin ), ( boundingBox.YMax - boundingBox.YMin ) );
setCollisionCentreOffset( ( boundingBox.XMax + boundingBox.XMin ) >> 1, ( boundingBox.YMax + boundingBox.YMin ) >> 1 );
calculateNonRotatedCollisionData();
2001-06-28 17:34:05 +02:00
m_playerAttached = false;
2001-07-10 18:09:56 +02:00
m_falling = false;
2001-07-06 21:42:07 +02:00
m_rebound = false;
2001-07-10 18:09:56 +02:00
m_inJump = false;
2001-04-23 20:58:30 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CNpcCartPlatform::processMovement( int _frames )
{
2001-05-04 18:20:10 +02:00
s32 fallSpeed = 2;
2001-04-23 20:58:30 +02:00
s8 yMovement = fallSpeed * _frames;
s32 distX, distY, heading;
s32 groundHeight;
s32 moveX = 0;
s32 moveY = 0;
bool pathComplete;
2001-07-10 18:09:56 +02:00
if ( !m_playerAttached && !m_falling )
2001-06-28 17:34:05 +02:00
{
m_playerAttached = true;
CPlayer *player = GameScene.getPlayer();
DVECTOR newPos = Pos;
2001-07-23 21:26:37 +02:00
CRECT const &collisionArea=getCollisionArea();
2001-06-28 17:34:05 +02:00
newPos.vy = collisionArea.y1;
player->setPos( newPos );
player->setPlatform( this );
m_contact = true;
}
2001-06-11 21:32:41 +02:00
if ( m_isActivated )
2001-04-23 20:58:30 +02:00
{
2001-07-10 18:09:56 +02:00
if ( m_falling )
{
m_vertSpeed += 192;
if ( m_vertSpeed > ( 8 << 8 ) )
{
m_vertSpeed = 8 << 8;
}
moveY = ( m_vertSpeed >> 8 ) * _frames;
Pos.vy += moveY;
2001-07-23 21:26:37 +02:00
DVECTOR const &offset = CLevel::getCameraPos();
2001-07-10 18:09:56 +02:00
s32 yPos = Pos.vy - offset.vy;
if ( yPos < 0 || yPos > VidGetScrH() )
{
setToShutdown();
}
}
else if ( m_rebound )
2001-05-04 18:20:10 +02:00
{
2001-07-06 21:42:07 +02:00
moveX = -4 * _frames;
2001-04-23 20:58:30 +02:00
2001-06-28 17:34:05 +02:00
m_vertSpeed += 192;
2001-04-23 20:58:30 +02:00
2001-07-10 18:09:56 +02:00
if ( m_vertSpeed > ( 8 << 8 ) )
2001-06-12 01:12:58 +02:00
{
2001-07-10 18:09:56 +02:00
m_vertSpeed = 8 << 8;
2001-06-12 01:12:58 +02:00
}
else if ( m_vertSpeed < -( 6 << 8 ) )
{
m_vertSpeed = -( 6 << 8 );
}
2001-05-04 18:20:10 +02:00
2001-06-12 01:12:58 +02:00
moveY = ( m_vertSpeed >> 8 ) * _frames;
2001-04-23 20:58:30 +02:00
2001-07-06 21:42:07 +02:00
groundHeight = CGameScene::getCollision()->getHeightFromGroundCart( Pos.vx + moveX, Pos.vy + moveY, 16 );
2001-04-23 20:58:30 +02:00
2001-06-12 01:12:58 +02:00
if ( groundHeight < 0 )
{
// have touched down
2001-07-06 21:42:07 +02:00
m_rebound = false;
2001-06-12 15:45:45 +02:00
moveY += groundHeight;
2001-06-12 01:12:58 +02:00
}
2001-07-06 21:42:07 +02:00
Pos.vx += moveX;
Pos.vy += moveY;
2001-06-11 21:32:41 +02:00
}
else
{
2001-07-06 21:42:07 +02:00
m_npcPath.thinkFlat( Pos, &pathComplete, &distX, &distY, &heading );
2001-04-23 20:58:30 +02:00
2001-07-06 21:42:07 +02:00
if ( !pathComplete )
{
moveX = ( m_carSpeed >> 8 ) * _frames;
2001-05-04 18:20:10 +02:00
2001-07-06 21:42:07 +02:00
if ( heading == 2048 )
{
moveX = -moveX;
}
}
2001-05-04 18:20:10 +02:00
2001-07-06 21:42:07 +02:00
if ( m_inJump )
2001-06-11 21:32:41 +02:00
{
2001-07-06 21:42:07 +02:00
m_vertSpeed += 192;
if ( m_vertSpeed > ( 5 << 8 ) )
{
m_vertSpeed = 5 << 8;
}
else if ( m_vertSpeed < -( 6 << 8 ) )
{
m_vertSpeed = -( 6 << 8 );
}
moveY = ( m_vertSpeed >> 8 ) * _frames;
2001-07-10 18:09:56 +02:00
groundHeight = CGameScene::getCollision()->getHeightFromGroundCart( Pos.vx + moveX, Pos.vy, moveY + 16 );
2001-06-12 01:12:58 +02:00
2001-07-10 18:09:56 +02:00
if ( groundHeight < moveY )
2001-07-06 21:42:07 +02:00
{
// have touched down
m_inJump = false;
moveY += groundHeight;
}
2001-06-12 01:12:58 +02:00
}
else
{
2001-07-06 21:42:07 +02:00
// check for vertical movement
2001-06-12 01:12:58 +02:00
2001-07-06 21:42:07 +02:00
s32 checkDist = yMovement + 50;
2001-06-11 21:32:41 +02:00
2001-07-06 21:42:07 +02:00
groundHeight = CGameScene::getCollision()->getHeightFromGroundCart( Pos.vx + moveX, Pos.vy, checkDist );
2001-06-12 01:12:58 +02:00
2001-07-06 21:42:07 +02:00
if ( groundHeight < checkDist )
2001-06-12 01:12:58 +02:00
{
2001-07-06 21:42:07 +02:00
// groundHeight <= yMovement indicates either just above ground or on or below ground
moveY = groundHeight;
2001-06-12 01:12:58 +02:00
}
2001-07-06 21:42:07 +02:00
else
{
// fall
2001-06-12 01:12:58 +02:00
2001-07-06 21:42:07 +02:00
moveY = yMovement;
}
if ( moveY < 0 )
{
m_carSpeed -= 1;
if ( m_carSpeed < ( 2 << 8 ) )
{
m_carSpeed = ( 2 << 8 );
}
}
else if ( moveY > 0 )
2001-06-12 01:12:58 +02:00
{
2001-07-06 21:42:07 +02:00
m_carSpeed += 20;
2001-07-10 18:09:56 +02:00
if ( m_carSpeed > ( 5 << 8 ) )
2001-07-06 21:42:07 +02:00
{
2001-07-10 18:09:56 +02:00
m_carSpeed = ( 5 << 8 );
2001-07-06 21:42:07 +02:00
}
2001-06-12 01:12:58 +02:00
}
2001-06-11 21:32:41 +02:00
}
2001-05-04 18:20:10 +02:00
2001-07-06 21:42:07 +02:00
Pos.vx += moveX;
Pos.vy += moveY;
}
2001-05-08 20:57:00 +02:00
2001-06-11 21:32:41 +02:00
// sort out draw rotation
2001-05-08 20:57:00 +02:00
2001-06-11 21:32:41 +02:00
DVECTOR testPos1, testPos2;
2001-05-08 20:57:00 +02:00
2001-06-11 21:32:41 +02:00
testPos1 = testPos2 = Pos;
testPos1.vx -= 10;
testPos2.vx += 10;
2001-05-08 20:57:00 +02:00
2001-06-11 21:32:41 +02:00
u8 sensorDist = 16;
2001-05-08 20:57:00 +02:00
2001-08-02 16:51:29 +02:00
s32 yDiff1, yDiff2;
2001-05-08 20:57:00 +02:00
2001-08-02 16:51:29 +02:00
yDiff1 = CGameScene::getCollision()->getHeightFromGroundCart( testPos1.vx, testPos1.vy, sensorDist + 1 );
2001-05-08 20:57:00 +02:00
2001-08-02 16:51:29 +02:00
if ( abs( yDiff1 ) <= sensorDist )
2001-06-11 21:32:41 +02:00
{
// only use if there is ground present
2001-05-08 20:57:00 +02:00
2001-08-02 16:51:29 +02:00
yDiff2 = CGameScene::getCollision()->getHeightFromGroundCart( testPos2.vx, testPos2.vy, sensorDist + 1 );
2001-05-08 20:57:00 +02:00
2001-08-02 16:51:29 +02:00
if ( abs( yDiff2 ) <= sensorDist )
{
// only use if there is ground present
2001-06-11 21:32:41 +02:00
2001-08-02 16:51:29 +02:00
testPos1.vy += yDiff1;
testPos2.vy += yDiff2;
}
2001-06-11 21:32:41 +02:00
}
s32 xDist = testPos2.vx - testPos1.vx;
s32 yDist = testPos2.vy - testPos1.vy;
heading = ratan2( yDist, xDist );
2001-05-08 20:57:00 +02:00
2001-06-11 21:32:41 +02:00
setCollisionAngle( heading );
2001-07-06 21:42:07 +02:00
2001-07-28 17:44:00 +02:00
testPos2 = Pos;
testPos2.vx += 32;
2001-08-02 16:51:29 +02:00
yDiff1 = CGameScene::getCollision()->getHeightFromGroundCart( testPos2.vx, testPos2.vy, sensorDist + 1 );
2001-07-28 17:44:00 +02:00
2001-08-02 16:51:29 +02:00
if ( yDiff1 <= sensorDist )
2001-07-28 17:44:00 +02:00
{
// only use if there is ground present
2001-08-02 16:51:29 +02:00
testPos2.vy += yDiff1;
2001-07-28 17:44:00 +02:00
}
2001-07-10 18:09:56 +02:00
switch ( CGameScene::getCollision()->getCollisionBlock( testPos2.vx, testPos2.vy - 8 ) & COLLISION_TYPE_MASK )
2001-07-06 21:42:07 +02:00
{
2001-07-10 18:09:56 +02:00
case COLLISION_TYPE_FLAG_DAMAGE:
2001-07-06 21:42:07 +02:00
{
2001-07-25 13:39:26 +02:00
if ( m_rebound )
2001-07-06 21:42:07 +02:00
{
m_vertSpeed = -8 << 8;
m_rebound = true;
Pos.vy -= 8;
}
2001-07-10 18:09:56 +02:00
break;
}
2001-07-13 18:25:18 +02:00
default:
break;
}
switch ( CGameScene::getCollision()->getCollisionBlock( Pos.vx, Pos.vy ) & COLLISION_TYPE_MASK )
{
2001-07-10 18:09:56 +02:00
case COLLISION_TYPE_FLAG_DEATH_FALL:
{
m_playerAttached = false;
m_falling = true;
break;
2001-07-06 21:42:07 +02:00
}
2001-07-10 18:09:56 +02:00
default:
break;
2001-07-06 21:42:07 +02:00
}
2001-05-08 20:57:00 +02:00
}
2001-06-11 21:32:41 +02:00
else
{
2001-07-06 21:42:07 +02:00
groundHeight = CGameScene::getCollision()->getHeightFromGroundCart( Pos.vx, Pos.vy, yMovement + 16 );
2001-06-11 21:32:41 +02:00
if ( groundHeight <= yMovement )
{
moveY = groundHeight;
}
else
{
// fall
2001-05-08 20:57:00 +02:00
2001-06-11 21:32:41 +02:00
moveY = yMovement;
}
2001-05-08 20:57:00 +02:00
2001-06-11 21:32:41 +02:00
Pos.vy += moveY;
2001-05-08 20:57:00 +02:00
2001-06-11 21:32:41 +02:00
if ( m_contact )
{
m_isActivated = true;
}
}
2001-05-08 20:57:00 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CNpcCartPlatform::render()
{
if ( m_isActive )
{
CPlatformThing::render();
2001-05-10 18:16:57 +02:00
if (canRender())
2001-05-08 20:57:00 +02:00
{
2001-05-10 18:16:57 +02:00
DVECTOR &renderPos=getRenderPos();
SVECTOR rotation;
rotation.vx = 0;
rotation.vy = 0;
rotation.vz = getCollisionAngle();
2001-05-08 20:57:00 +02:00
2001-05-10 18:16:57 +02:00
VECTOR scale;
scale.vx = ONE;
scale.vy = ONE;
scale.vz = ONE;
2001-05-08 20:57:00 +02:00
2001-05-10 18:16:57 +02:00
m_modelGfx->Render(renderPos,&rotation,&scale);
2001-05-08 20:57:00 +02:00
}
}
}
2001-06-12 01:12:58 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CNpcCartPlatform::jump()
{
2001-08-14 20:46:33 +02:00
if ( !m_inJump && !m_rebound && m_trackContact )
2001-06-12 01:12:58 +02:00
{
m_inJump = true;
2001-06-28 17:34:05 +02:00
m_vertSpeed = -8 << 8;
2001-07-12 18:53:21 +02:00
CSoundMediator::playSfx(CSoundMediator::SFX_SPONGEBOB_JUMP);
2001-06-12 01:12:58 +02:00
}
}
2001-06-12 15:45:45 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CNpcCartPlatform::collidedWith( CThing *_thisThing )
{
2001-07-10 18:09:56 +02:00
if ( !m_falling )
2001-06-12 15:45:45 +02:00
{
2001-07-10 18:09:56 +02:00
switch(_thisThing->getThingType())
2001-06-12 15:45:45 +02:00
{
2001-07-10 18:09:56 +02:00
case TYPE_PLAYER:
{
CPlayer *player;
2001-07-23 21:26:37 +02:00
// DVECTOR playerPos;
// CRECT collisionArea;
// CRECT playerCollisionArea;
2001-06-12 15:45:45 +02:00
2001-07-10 18:09:56 +02:00
// Only interested in SBs feet colliding with the box (pkg)
player=(CPlayer*)_thisThing;
2001-07-23 21:26:37 +02:00
DVECTOR const & playerPos=player->getPos();
CRECT const & playerCollisionArea = player->getCollisionArea();
CRECT const & collisionArea=getCollisionArea();
2001-06-12 15:45:45 +02:00
2001-07-10 18:09:56 +02:00
s32 threshold = abs( collisionArea.y2 - collisionArea.y1 );
2001-06-12 15:45:45 +02:00
2001-07-10 18:09:56 +02:00
if ( threshold > 16 )
2001-06-12 15:45:45 +02:00
{
2001-07-10 18:09:56 +02:00
threshold = 16;
2001-06-12 15:45:45 +02:00
}
2001-07-10 18:09:56 +02:00
2001-07-12 16:10:26 +02:00
//if( playerPos.vx >= collisionArea.x1 && playerPos.vx <= collisionArea.x2 )
if( playerCollisionArea.x2 >= collisionArea.x1 && playerCollisionArea.x1 <= collisionArea.x2 )
2001-06-12 15:45:45 +02:00
{
2001-07-10 18:09:56 +02:00
if ( checkCollisionDelta( _thisThing, threshold, collisionArea ) )
2001-06-12 15:45:45 +02:00
{
2001-07-10 18:09:56 +02:00
player->setPlatform( this );
2001-06-12 15:45:45 +02:00
2001-07-10 18:09:56 +02:00
m_contact = true;
}
else
{
if( playerPos.vy >= collisionArea.y1 && playerPos.vy <= collisionArea.y2 )
{
if ( m_isActivated || player->getPosDelta().vy >= 0 )
2001-06-12 15:45:45 +02:00
{
2001-07-10 18:09:56 +02:00
int height = getHeightFromPlatformAtPosition( playerPos.vx, playerPos.vy );
if ( height >= -threshold && height < 1 )
{
player->setPlatform( this );
2001-06-12 15:45:45 +02:00
2001-07-10 18:09:56 +02:00
m_contact = true;
}
2001-06-12 15:45:45 +02:00
}
}
}
}
2001-07-10 18:09:56 +02:00
break;
}
2001-06-12 15:45:45 +02:00
2001-07-10 18:09:56 +02:00
case TYPE_NPC:
case TYPE_HAZARD:
break;
2001-06-12 15:45:45 +02:00
2001-07-10 18:09:56 +02:00
default:
ASSERT(0);
break;
}
2001-06-12 15:45:45 +02:00
}
}