151 lines
4.0 KiB
C#
151 lines
4.0 KiB
C#
using SHADE;
|
|
using SHADE.Test;
|
|
using SHADE_Scripting.Audio;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States
|
|
{
|
|
public class ChaseState :AIBaseState
|
|
{
|
|
|
|
float giveUpDuration = 10.0f;
|
|
float giveUpTimer = 0.0f;
|
|
float atkDistance = 2.0f;
|
|
|
|
|
|
bool run = true;
|
|
|
|
float footStepInterval = 12.0f / 30.0f;
|
|
float footStepTimer = 0.0f;
|
|
|
|
|
|
public ChaseState(StateMachine machine): base(machine)
|
|
{
|
|
stateName = "Chase";
|
|
}
|
|
|
|
public override void OnEnter()
|
|
{
|
|
giveUpTimer = giveUpDuration;
|
|
|
|
animator.Play(ai.alertRunAnim);
|
|
run = true;
|
|
RotateToVelocity r = machine.GetScript<RotateToVelocity>();
|
|
if (r)
|
|
{
|
|
r.rotateToPlayerLastKnown = true;
|
|
}
|
|
|
|
|
|
footStepTimer = footStepInterval * 0.5f;
|
|
}
|
|
|
|
public override void OnExit()
|
|
{
|
|
animator.Stop();
|
|
RotateToVelocity r = machine.GetScript<RotateToVelocity>();
|
|
if (r)
|
|
{
|
|
r.rotateToPlayerLastKnown = false;
|
|
}
|
|
RotateToVelocity rotate = ai.GetScript<RotateToVelocity>();
|
|
if (rotate)
|
|
{
|
|
rotate.lookAround = false;
|
|
}
|
|
}
|
|
|
|
public override void update()
|
|
{
|
|
Navigation nav = machine.GetComponent<Navigation>();
|
|
AILineOfSight los = ai.GetScript<AILineOfSight>();
|
|
RigidBody rigid = machine.GetComponent<RigidBody>();
|
|
if (los && nav)
|
|
{
|
|
Transform playerTransform = los.player.GetComponent<Transform>();
|
|
if (los.withinSight)
|
|
{
|
|
nav.MoveTo(playerTransform.GlobalPosition);
|
|
}
|
|
else
|
|
{
|
|
nav.MoveTo(los.lastFoundPos);
|
|
giveUpTimer -= Time.DeltaTimeF;
|
|
}
|
|
if(los.lastFoundTimer>= 10.0f || giveUpTimer <= 0.0f)
|
|
{
|
|
machine.SetState(typeof(TimeoutState));
|
|
}
|
|
|
|
if(los.distance < atkDistance)
|
|
{
|
|
machine.SetState(typeof(AttackState));
|
|
}
|
|
|
|
|
|
if(rigid)
|
|
{
|
|
if (los.withinSight)
|
|
rigid.LinearVelocity = nav.GetForward() * ai.chaseSpeed;
|
|
else
|
|
rigid.LinearVelocity = nav.GetForward() * ai.chaseSpeed;
|
|
}
|
|
|
|
if(nav.ReachedTarget())
|
|
{
|
|
giveUpTimer -= Time.DeltaTimeF;
|
|
|
|
ai.RotateToPlayer();
|
|
RotateToVelocity rotate = ai.GetScript<RotateToVelocity>();
|
|
if(rotate)
|
|
{
|
|
rotate.lookAround = true;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (animator && rigid)
|
|
{
|
|
if (rigid.LinearVelocity.GetMagnitude() < 0.001f)
|
|
{
|
|
if(run)
|
|
{
|
|
animator.Play(ai.idleAnim);
|
|
run = false;
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
if(!run)
|
|
{
|
|
animator.Play(ai.alertRunAnim);
|
|
run = true;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
footStepTimer -= Time.DeltaTimeF;
|
|
if (footStepTimer <= 0.0f)
|
|
{
|
|
footStepTimer += footStepInterval;
|
|
AudioHandler.audioClipHandlers["HO_footsteps"].Play();
|
|
}
|
|
}
|
|
|
|
public override void fixedUpdate()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|