SHADE_Y3/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/PatrolState.cs

116 lines
3.0 KiB
C#

using SHADE;
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 PatrolState: AIBaseState
{
Random rand;
Vector3 lastFramePos;
float stuckTimer;
bool run = true;
float footStepInterval = 12.0f / 30.0f;
float footStepTimer = 0.0f;
public PatrolState(StateMachine machine) : base(machine)
{
stateName = "Patrol";
rand = new Random();
}
public override void OnEnter()
{
Navigation nav = machine.GetComponent<Navigation>();
Transform transform = machine.GetComponent<Transform>();
if (nav && transform)
{
int index = rand.Next(0, ai.patrolPointPool.Count() - 1);
Transform dest = ai.patrolPointPool.ElementAt(index);
if (dest)
{
nav.MoveTo(dest.GlobalPosition);
}
lastFramePos = transform.GlobalPosition;
stuckTimer = 0.0f;
}
animator.Play(ai.walkingAnim);
AudioHandler.audioClipHandlers["HO_footsteps"].Play();
footStepTimer = footStepInterval;
RotateToVelocity r = machine.GetScript<RotateToVelocity>();
if(r)
{
r.rotateToPlayerLastKnown = false;
}
}
public override void OnExit()
{
animator.Stop();
}
public override void update()
{
Navigation nav = machine.GetComponent<Navigation>();
Transform transform = machine.GetComponent<Transform>();
RigidBody rigid = machine.GetComponent<RigidBody>();
if(nav && transform && rigid)
{
rigid.LinearVelocity = nav.GetForward() * ai.patrolSpeed;
Vector3 d = lastFramePos - transform.GlobalPosition;
if (d.GetSqrMagnitude() < 0.001f)
{
//stuckTimer += Time.DeltaTimeF;
}
if (nav.ReachedTarget())
{
machine.SetState(typeof(IdleState));
}
lastFramePos = transform.GlobalPosition;
}
if(stuckTimer >= 0.5f)
{
machine.SetState(typeof(IdleState));
}
if (ai.ShouldTransitAlert())
{
machine.SetState(typeof(AlertState));
}
footStepTimer -= Time.DeltaTimeF;
if(footStepTimer <= 0.0f)
{
footStepTimer += footStepInterval;
AudioHandler.audioClipHandlers["HO_footsteps"].Play();
}
}
public override void fixedUpdate()
{
}
}
}