SBSPSS/source/enemy/ngeneric.cpp

384 lines
6.4 KiB
C++
Raw Normal View History

2001-01-22 15:21:37 +01:00
/*=========================================================================
ngeneric.cpp
Author: CRB
Created:
Project: Spongebob
Purpose:
Copyright (c) 2000 Climax Development Ltd
===========================================================================*/
#ifndef __ENEMY_NPC_H__
#include "enemy\npc.h"
#endif
#ifndef __GAME_GAME_H__
#include "game\game.h"
#endif
#ifndef __PLAYER_PLAYER_H__
#include "player\player.h"
#endif
2001-02-27 22:25:22 +01:00
void CNpcEnemy::processGenericGotoTarget( int _frames, s32 xDist, s32 yDist, s32 speed )
2001-01-25 16:45:15 +01:00
{
s16 decDir, incDir, moveDist;
s32 moveX, moveY;
s16 headingToTarget = ratan2( yDist, xDist );
s16 maxTurnRate = m_data[m_type].turnSpeed;
decDir = m_heading - headingToTarget;
if ( decDir < 0 )
{
decDir += ONE;
}
incDir = headingToTarget - m_heading;
if ( incDir < 0 )
{
incDir += ONE;
}
if ( decDir < incDir )
{
moveDist = -decDir;
}
else
{
moveDist = incDir;
}
if ( moveDist < -maxTurnRate )
{
moveDist = -maxTurnRate;
}
else if ( moveDist > maxTurnRate )
{
moveDist = maxTurnRate;
}
m_heading += moveDist;
2001-04-05 16:17:24 +02:00
m_heading &= 4095;
2001-01-25 16:45:15 +01:00
s32 preShiftX = _frames * speed * rcos( m_heading );
s32 preShiftY = _frames * speed * rsin( m_heading );
moveX = preShiftX >> 12;
if ( !moveX && preShiftX )
{
moveX = preShiftX / abs( preShiftX );
}
2001-04-20 22:22:16 +02:00
if ( xDist > 0 )
{
if ( moveX > xDist )
{
moveX = xDist;
}
}
2001-04-28 19:39:24 +02:00
else if ( xDist < 0 )
2001-04-20 22:22:16 +02:00
{
if ( moveX < xDist )
{
moveX = xDist;
}
}
2001-04-28 19:39:24 +02:00
else
{
moveX = 0;
}
2001-04-20 22:22:16 +02:00
2001-01-25 16:45:15 +01:00
moveY = preShiftY >> 12;
if ( !moveY && preShiftY )
{
moveY = preShiftY / abs( preShiftY );
}
2001-04-20 22:22:16 +02:00
if ( yDist > 0 )
{
if ( moveY > yDist )
{
moveY = yDist;
}
}
2001-04-28 19:39:24 +02:00
else if ( yDist < 0 )
2001-04-20 22:22:16 +02:00
{
if ( moveY < yDist )
{
moveY = yDist;
}
}
2001-04-28 19:39:24 +02:00
else
{
moveY = 0;
}
2001-04-20 22:22:16 +02:00
2001-01-25 16:45:15 +01:00
Pos.vx += moveX;
Pos.vy += moveY;
}
2001-02-27 22:25:22 +01:00
void CNpcEnemy::processGenericGetUserDist( int _frames, s32 *distX, s32 *distY )
2001-01-22 15:21:37 +01:00
{
s32 moveX = 0, moveY = 0;
s16 moveDist = 0;
s32 moveVel = 0;
CPlayer *player = GameScene.getPlayer();
DVECTOR playerPos = player->getPos();
2001-02-06 20:29:35 +01:00
*distX = playerPos.vx - this->Pos.vx;
*distY = playerPos.vy - this->Pos.vy;
2001-01-22 15:21:37 +01:00
}
2001-02-12 18:52:04 +01:00
2001-02-27 22:25:22 +01:00
bool CNpcEnemy::processGroundCollisionReverse( s32 *moveX, s32 *moveY )
2001-02-22 22:00:56 +01:00
{
bool xBlocked = false;
bool yBlocked = false;
// check for collision with ground
2001-05-25 20:43:47 +02:00
if ( CGameScene::getCollision()->Get( ( Pos.vx + *moveX ) >> 4, ( Pos.vy + *moveY ) >> 4 ) )
2001-02-22 22:00:56 +01:00
{
// destination point is below ground, check in individual directions
2001-05-25 20:43:47 +02:00
if ( CGameScene::getCollision()->Get( ( Pos.vx + *moveX ) >> 4, Pos.vy >> 4 ) )
2001-02-22 22:00:56 +01:00
{
// X direction is blocked
xBlocked = true;
}
2001-05-25 20:43:47 +02:00
if ( CGameScene::getCollision()->Get( Pos.vx >> 4, ( Pos.vy + *moveY ) >> 4 ) )
2001-02-22 22:00:56 +01:00
{
yBlocked = true;
}
if ( xBlocked && !yBlocked )
{
// invert X
*moveX = -(*moveX);
m_heading = ratan2( *moveY, *moveX );
}
else if ( !xBlocked && yBlocked )
{
// invert Y
*moveY = -(*moveY);
m_heading = ratan2( *moveY, *moveX );
}
else
{
// invert both
*moveX = -(*moveX);
*moveY = -(*moveY);
m_heading += 2048;
}
2001-04-05 16:17:24 +02:00
m_heading &= 4095;
2001-02-22 22:00:56 +01:00
}
return( xBlocked | yBlocked );
}
2001-06-21 22:10:19 +02:00
u8 CNpcEnemy::processGenericFixedPathMove( int _frames, s32 *moveX, s32 *moveY, s32 *moveVel, s32 *moveDist )
2001-02-22 22:00:56 +01:00
{
bool pathComplete;
bool waypointChange;
2001-04-28 19:39:24 +02:00
s32 xDist, yDist;
2001-02-22 22:00:56 +01:00
2001-04-28 19:39:24 +02:00
s16 headingToTarget = m_npcPath.think( Pos, &pathComplete, &waypointChange, &xDist, &yDist );
2001-02-22 22:00:56 +01:00
2001-04-11 18:39:31 +02:00
/*if ( waypointChange )
2001-02-22 22:00:56 +01:00
{
m_movementTimer = 0;
2001-04-11 18:39:31 +02:00
}*/
2001-02-22 22:00:56 +01:00
if ( !pathComplete )
{
s16 decDir, incDir;
s16 maxTurnRate = m_data[m_type].turnSpeed;
decDir = m_heading - headingToTarget;
if ( decDir < 0 )
{
decDir += ONE;
}
incDir = headingToTarget - m_heading;
if ( incDir < 0 )
{
incDir += ONE;
}
if ( decDir < incDir )
{
*moveDist = -decDir;
}
else
{
*moveDist = incDir;
}
if ( *moveDist < -maxTurnRate )
{
*moveDist = -maxTurnRate;
}
else if ( *moveDist > maxTurnRate )
{
*moveDist = maxTurnRate;
}
m_heading += *moveDist;
2001-04-05 16:17:24 +02:00
m_heading &= 4095;
2001-02-22 22:00:56 +01:00
2001-05-09 23:27:23 +02:00
s32 preShiftX = _frames * m_speed * rcos( m_heading );
s32 preShiftY = _frames * m_speed * rsin( m_heading );
2001-02-22 22:00:56 +01:00
*moveX = preShiftX >> 12;
if ( !(*moveX) && preShiftX )
{
*moveX = preShiftX / abs( preShiftX );
}
2001-04-28 19:39:24 +02:00
if ( xDist > 0 )
{
if ( *moveX > xDist )
{
*moveX = xDist;
}
}
else if ( xDist < 0 )
{
if ( *moveX < xDist )
{
*moveX = xDist;
}
}
else
{
*moveX = 0;
}
2001-02-22 22:00:56 +01:00
*moveY = preShiftY >> 12;
if ( !(*moveY) && preShiftY )
{
*moveY = preShiftY / abs( preShiftY );
}
2001-04-28 19:39:24 +02:00
if ( yDist > 0 )
{
if ( *moveY > yDist )
{
*moveY = yDist;
}
}
else if ( yDist < 0 )
{
if ( *moveY < yDist )
{
*moveY = yDist;
}
}
else
{
*moveY = 0;
}
2001-05-09 23:27:23 +02:00
*moveVel = ( _frames * m_speed ) << 8;
2001-02-22 22:00:56 +01:00
2001-03-06 21:13:16 +01:00
//processGroundCollisionReverse( moveX, moveY );
2001-02-22 22:00:56 +01:00
}
2001-06-21 22:10:19 +02:00
return( waypointChange );
2001-02-22 22:00:56 +01:00
}
2001-02-27 22:25:22 +01:00
void CNpcEnemy::processGenericFixedPathWalk( int _frames, s32 *moveX, s32 *moveY )
2001-02-22 16:39:38 +01:00
{
2001-04-04 21:55:49 +02:00
s32 maxHeight = 20;
2001-02-22 16:39:38 +01:00
s32 distX, distY;
2001-04-04 21:55:49 +02:00
s32 fallSpeed = 3;
2001-02-22 16:39:38 +01:00
s8 yMovement = fallSpeed * _frames;
s32 groundHeight;
*moveX = 0;
*moveY = 0;
// ignore y component of waypoint, since we are stuck to the ground
2001-04-18 18:34:17 +02:00
bool pathComplete;
if ( m_npcPath.thinkFlat( Pos, &pathComplete, &distX, &distY, &m_heading ) )
2001-02-22 16:39:38 +01:00
{
// path has finished, waypoint has changed, or there are no waypoints - do not move horizontally
// check for vertical movement
2001-05-25 20:43:47 +02:00
groundHeight = CGameScene::getCollision()->getHeightFromGround( Pos.vx, Pos.vy, yMovement + 16 );
2001-02-22 16:39:38 +01:00
if ( groundHeight <= yMovement )
{
// groundHeight <= yMovement indicates either just above ground or on or below ground
*moveY = groundHeight;
}
else
{
// fall
*moveY = yMovement;
}
}
else
{
// check for collision
distX = distX / abs( distX );
2001-05-25 20:43:47 +02:00
if ( CGameScene::getCollision()->getHeightFromGround( Pos.vx + ( distX * m_speed * _frames ), Pos.vy ) < -maxHeight )
2001-02-22 16:39:38 +01:00
{
// there is an obstacle in the way, increment the path point (hopefully this will resolve the problem)
m_npcPath.incPath();
}
else
{
// check for vertical movement
2001-05-25 20:43:47 +02:00
groundHeight = CGameScene::getCollision()->getHeightFromGround( Pos.vx, Pos.vy, yMovement + 16 );
2001-02-22 16:39:38 +01:00
if ( groundHeight <= yMovement )
{
// groundHeight <= yMovement indicates either just above ground or on or below ground
2001-05-09 23:27:23 +02:00
*moveX = distX * m_speed * _frames;
2001-02-22 16:39:38 +01:00
*moveY = groundHeight;
}
else
{
// fall
*moveY = yMovement;
}
}
}
}
2001-02-27 22:25:22 +01:00
bool CNpcEnemy::isCollisionWithGround()
2001-02-12 18:52:04 +01:00
{
2001-05-25 20:43:47 +02:00
return CGameScene::getCollision()->Get( Pos.vx >> 4, ( Pos.vy + 1 ) >> 4 ) ? 16:0;
2001-02-12 18:52:04 +01:00
}