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

174 lines
4.4 KiB
C#
Raw Normal View History

2023-03-24 13:26:18 +08:00
using SHADE;
2023-03-25 14:51:28 +08:00
using SHADE.Test;
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 ChaseState :AIBaseState
{
float giveUpDuration = 10.0f;
float giveUpTimer = 0.0f;
2023-03-30 15:26:15 +08:00
float atkDistance = 1.0f;
2023-03-25 14:51:28 +08:00
bool run = true;
float footStepInterval = 12.0f / 30.0f;
float footStepTimer = 0.0f;
float barkTimer = 0.0f;
Random rand;
2023-03-24 13:26:18 +08:00
public ChaseState(StateMachine machine): base(machine)
{
stateName = "Chase";
rand = new Random();
2023-03-24 13:26:18 +08:00
}
public override void OnEnter()
{
giveUpTimer = giveUpDuration;
2023-03-25 14:51:28 +08:00
animator.Play(ai.alertRunAnim);
run = true;
RotateToVelocity r = machine.GetScript<RotateToVelocity>();
if (r)
{
r.rotateToPlayerLastKnown = true;
}
footStepTimer = footStepInterval * 0.5f;
barkTimer = (float)rand.Next(0, 2) + (float)rand.NextDouble();
2023-03-25 14:51:28 +08:00
}
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;
}
2023-03-24 13:26:18 +08:00
}
public override void update()
{
barkTimer -= Time.DeltaTimeF;
if(barkTimer <= 0.0f)
{
barkTimer = (float)rand.Next(4, 6) + (float)rand.NextDouble();
SHADE.Audio.SetParameterWithLabel("HomeownerBark", "Chase");
AudioHandler.audioClipHandlers["HO_bark"].Play();
}
Navigation nav = machine.GetComponent<Navigation>();
2023-03-24 13:26:18 +08:00
AILineOfSight los = ai.GetScript<AILineOfSight>();
2023-03-25 14:51:28 +08:00
RigidBody rigid = machine.GetComponent<RigidBody>();
if (los && nav)
2023-03-24 13:26:18 +08:00
{
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));
}
2023-03-25 14:51:28 +08:00
if(los.distance < atkDistance)
{
machine.SetState(typeof(AttackState));
}
2023-03-24 13:26:18 +08:00
if(rigid)
{
if (los.withinSight)
rigid.LinearVelocity = nav.GetForward() * ai.chaseSpeed;
else
2023-03-25 14:51:28 +08:00
rigid.LinearVelocity = nav.GetForward() * ai.chaseSpeed;
2023-03-24 13:26:18 +08:00
}
if(nav.ReachedTarget())
{
giveUpTimer -= Time.DeltaTimeF;
ai.RotateToPlayer();
2023-03-25 14:51:28 +08:00
RotateToVelocity rotate = ai.GetScript<RotateToVelocity>();
if(rotate)
{
rotate.lookAround = true;
}
}
2023-03-24 13:26:18 +08:00
2023-03-25 14:51:28 +08:00
}
2023-03-24 13:26:18 +08:00
2023-03-25 14:51:28 +08:00
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;
}
2023-03-24 13:26:18 +08:00
}
}
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()
{
}
}
}