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

116 lines
3.0 KiB
C#
Raw Normal View History

2023-03-24 13:26:18 +08:00
using SHADE;
using SHADE_Scripting.Audio;
2023-03-24 13:26:18 +08:00
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;
2023-03-25 14:51:28 +08:00
bool run = true;
2023-03-24 13:26:18 +08:00
float footStepInterval = 12.0f / 30.0f;
float footStepTimer = 0.0f;
2023-03-24 13:26:18 +08:00
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);
2023-03-25 14:51:28 +08:00
2023-03-24 13:26:18 +08:00
}
lastFramePos = transform.GlobalPosition;
stuckTimer = 0.0f;
}
2023-03-25 14:51:28 +08:00
animator.Play(ai.walkingAnim);
AudioHandler.audioClipHandlers["HO_footsteps"].Play();
footStepTimer = footStepInterval;
2023-03-25 14:51:28 +08:00
RotateToVelocity r = machine.GetScript<RotateToVelocity>();
if(r)
{
r.rotateToPlayerLastKnown = false;
}
}
public override void OnExit()
{
animator.Stop();
2023-03-24 13:26:18 +08:00
}
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)
{
2023-03-25 14:51:28 +08:00
//stuckTimer += Time.DeltaTimeF;
2023-03-24 13:26:18 +08:00
}
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();
}
2023-03-24 13:26:18 +08:00
}
public override void fixedUpdate()
{
}
}
}