SBSPSS/source/enemy/npcpath.cpp

141 lines
2.3 KiB
C++
Raw Normal View History

2001-01-16 22:40:14 +01:00
/*=========================================================================
npcpath.cpp
Author: CRB
Created:
Project: Spongebob
Purpose:
Copyright (c) 2000 Climax Development Ltd
===========================================================================*/
2001-01-18 20:18:39 +01:00
#ifndef __ENEMY_NPCPATH_H__
#include "enemy\npcpath.h"
2001-01-16 22:56:29 +01:00
#endif
2001-01-18 20:18:39 +01:00
bool CNpcWaypoint::isPointNear( DVECTOR testPos, s32 *xDist, s32 *yDist )
2001-01-16 22:40:14 +01:00
{
s32 xDistSqr, yDistSqr;
2001-01-19 22:46:30 +01:00
*xDist = this->pos.vx - testPos.vx;
2001-01-18 20:18:39 +01:00
xDistSqr = (*xDist) * (*xDist);
2001-01-16 22:40:14 +01:00
2001-01-19 22:46:30 +01:00
*yDist = this->pos.vy - testPos.vy;
2001-01-18 20:18:39 +01:00
yDistSqr = (*yDist) * (*yDist);
2001-01-16 22:40:14 +01:00
if ( xDistSqr + yDistSqr < 100 )
{
return( true );
}
else
{
return( false );
}
}
2001-01-16 22:56:29 +01:00
void CNpcPath::initPath()
{
int loop;
for ( loop = 0 ; loop < NPC_MAX_WAYPOINTS ; loop++ )
{
waypoint[loop].pos.vx = 0;
2001-01-18 20:18:39 +01:00
waypoint[loop].pos.vy = 0;
2001-01-16 22:56:29 +01:00
}
pathType = SINGLE_USE_PATH;
2001-01-23 18:03:27 +01:00
currentWaypoint = lastWaypoint = 0;
2001-01-16 22:56:29 +01:00
waypointCount = 0;
reversePath = false;
}
2001-01-16 22:40:14 +01:00
void CNpcPath::addWaypoint( DVECTOR newPos )
{
if ( waypointCount < NPC_MAX_WAYPOINTS )
{
2001-01-18 20:18:39 +01:00
waypoint[waypointCount].pos = newPos;
2001-01-16 22:40:14 +01:00
waypointCount++;
}
}
2001-01-16 22:56:29 +01:00
void CNpcPath::setPathType( NPC_PATH_TYPE newPathType )
{
pathType = newPathType;
}
bool CNpcPath::incPath()
{
if ( !reversePath )
{
2001-01-18 20:18:39 +01:00
if ( currentWaypoint < waypointCount - 1 )
2001-01-16 22:56:29 +01:00
{
currentWaypoint++;
}
else
{
switch( pathType )
{
case SINGLE_USE_PATH:
// path is completed
return( true );
case REPEATING_PATH:
// go back to start
currentWaypoint = 0;
break;
case PONG_PATH:
// reverse path
reversePath = !reversePath;
currentWaypoint--;
break;
}
}
}
else
{
// must be pong path if reversed
if ( currentWaypoint > 0 )
{
currentWaypoint--;
}
else
{
reversePath = !reversePath;
currentWaypoint++;
}
}
return( false );
}
2001-01-19 22:46:30 +01:00
bool CNpcPath::getDistToNextWaypoint( DVECTOR currentPos, s32 *distX, s32 *distY )
{
return( waypoint[currentWaypoint].isPointNear( currentPos, distX, distY ) );
}
2001-01-23 18:03:27 +01:00
s32 CNpcPath::think( DVECTOR currentPos, bool *pathComplete, bool *waypointChange )
2001-01-16 22:56:29 +01:00
{
2001-01-18 20:18:39 +01:00
s32 xDist, yDist;
2001-01-16 22:56:29 +01:00
2001-01-18 20:18:39 +01:00
*pathComplete = false;
2001-01-23 18:03:27 +01:00
*waypointChange = false;
2001-01-16 22:56:29 +01:00
2001-01-18 20:18:39 +01:00
if ( waypoint[currentWaypoint].isPointNear( currentPos, &xDist, &yDist ) )
2001-01-16 22:56:29 +01:00
{
2001-01-18 20:18:39 +01:00
*pathComplete = incPath();
2001-01-23 18:03:27 +01:00
*waypointChange = true;
2001-01-16 22:56:29 +01:00
}
2001-01-18 20:18:39 +01:00
2001-01-19 22:46:30 +01:00
s32 headingToTarget = ratan2( yDist, xDist );
2001-01-18 20:18:39 +01:00
return( headingToTarget );
2001-01-16 22:56:29 +01:00
}