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

89 lines
2.3 KiB
C#

using SHADE;
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 ;
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);
Debug.Log("Moving to" + dest.GlobalPosition.ToString());
}
lastFramePos = transform.GlobalPosition;
stuckTimer = 0.0f;
}
}
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));
}
}
public override void fixedUpdate()
{
}
}
}