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

146 lines
3.6 KiB
C#
Raw Normal View History

2023-03-25 14:51:28 +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 AttackState: AIBaseState
{
float timer = 0.0f;
float windupTime = 8.0f / 30.0f;
float holdTime = 0.3f;
2023-03-25 17:29:03 +08:00
float seqTime = 8.0f / 30.0f;
float endTime = 0.5f;
2023-03-25 14:51:28 +08:00
bool windUp = false;
bool hold = false;
bool seq = false;
2023-03-25 17:29:03 +08:00
bool end = false;
2023-03-25 14:51:28 +08:00
public AttackState(StateMachine machine): base(machine)
{
}
public override void OnEnter()
{
timer = 0.0f;
animator.PlayOneShot(ai.atkWindupAnim);
windUp = true;
hold = false;
seq = false;
2023-03-25 17:29:03 +08:00
end = false;
2023-03-25 14:51:28 +08:00
timer = windupTime;
RotateToVelocity rotate = machine.GetScript<RotateToVelocity>();
if (rotate)
rotate.active = false;
RigidBody rigid = machine.GetComponent<RigidBody>();
if(rigid)
{
rigid.LinearVelocity = Vector3.Zero;
}
2023-03-25 14:51:28 +08:00
ai.attackHitbox.SetActive(false);
}
public override void OnExit()
{
animator.Stop();
RotateToVelocity rotate = ai.GetScript<RotateToVelocity>();
if (rotate)
{
rotate.active = true;
rotate.lookAround = false;
}
Transform transform = machine.GetComponent<Transform>();
AILineOfSight los = machine.GetScript<AILineOfSight>();
Transform playerTransform = ai.player.GetComponent<Transform>();
if(los && transform)
{
Vector3 direction = playerTransform.GlobalPosition - transform.GlobalPosition;
Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(direction.x, direction.z), 0.0f);
transform.LocalRotation = targetRotation;
}
ai.attackHitbox.SetActive(false);
}
public override void update()
{
timer -= Time.DeltaTimeF;
if(windUp)
{
if(timer <= 0.0f)
{
windUp = false;
hold = true;
animator.Play(ai.atkHoldAnim);
timer = holdTime;
ai.attackHitbox.SetActive(true);
}
}
else if(hold)
{
if(timer <= 0.0f)
{
hold = false;
seq = true;
animator.PlayOneShot(ai.atkSeqAnim);
timer = seqTime;
}
}
else if(seq)
{
if(timer <= 0.0f)
{
seq = false;
2023-03-25 17:29:03 +08:00
end = true;
animator.PlayOneShot(ai.atkSeqAnim);
timer = endTime;
2023-03-25 14:51:28 +08:00
2023-03-25 17:29:03 +08:00
}
}
else if(end)
{
if(timer <= 0.0f)
{
end = false;
2023-03-25 14:51:28 +08:00
AILineOfSight los = ai.GetScript<AILineOfSight>();
2023-03-25 17:29:03 +08:00
if (los && los.withinSight)
2023-03-25 14:51:28 +08:00
{
machine.SetState(typeof(ChaseState));
}
else
{
machine.SetState(typeof(TimeoutState));
}
}
}
}
public override void fixedUpdate()
{
}
}
}