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

78 lines
1.9 KiB
C#
Raw Normal View History

2023-03-24 13:26:18 +08:00
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 ChaseState :AIBaseState
{
float giveUpDuration = 10.0f;
float giveUpTimer = 0.0f;
public ChaseState(StateMachine machine): base(machine)
{
stateName = "Chase";
}
public override void OnEnter()
{
giveUpTimer = giveUpDuration;
}
public override void update()
{
Navigation nav = machine.GetComponent<Navigation>();
AILineOfSight los = ai.GetScript<AILineOfSight>();
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));
}
RigidBody rigid = machine.GetComponent<RigidBody>();
if(rigid)
{
if (los.withinSight)
rigid.LinearVelocity = nav.GetForward() * ai.chaseSpeed;
else
rigid.LinearVelocity = nav.GetForward() * ai.patrolSpeed;
}
if(nav.ReachedTarget())
{
giveUpTimer -= Time.DeltaTimeF;
ai.RotateToPlayer();
}
}
}
public override void fixedUpdate()
{
}
}
}