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

166 lines
3.3 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
{
2023-03-26 13:27:42 +08:00
public class AttackState : AIBaseState
{
float timer = 0.0f;
float windupTime = 8.0f / 30.0f;
float holdTime = 0.3f;
float seqTime = 8.0f / 30.0f;
float endTime = 0.5f;
bool windUp = false;
bool hold = false;
bool seq = false;
bool end = false;
public AttackState(StateMachine machine) : base(machine)
2023-03-25 14:51:28 +08:00
{
2023-03-30 15:26:15 +08:00
stateName = "Attack";
2023-03-26 13:27:42 +08:00
}
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
public override void OnEnter()
{
timer = 0.0f;
animator.PlayOneShot(ai.atkWindupAnim);
windUp = true;
hold = false;
seq = false;
end = false;
timer = windupTime;
RotateToVelocity rotate = machine.GetScript<RotateToVelocity>();
if (rotate)
rotate.active = false;
RigidBody rigid = machine.GetComponent<RigidBody>();
if (rigid)
{
rigid.LinearVelocity = Vector3.Zero;
}
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);
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
}
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
public override void OnExit()
{
2023-03-30 15:26:15 +08:00
2023-03-26 13:27:42 +08:00
RotateToVelocity rotate = ai.GetScript<RotateToVelocity>();
if (rotate)
{
rotate.active = true;
rotate.lookAround = false;
}
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
//ai.attackHitbox.SetActive(false);
machine.GetScript<HomeOwnerAI>().atk = false;
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
}
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
public override void update()
{
2023-03-30 15:26:15 +08:00
if (ai.hitboxScript.raccoonFound)
{
machine.SetState(typeof(CaughtRaccoonState));
}
2023-03-26 13:27:42 +08:00
timer -= Time.DeltaTimeF;
if (windUp)
{
if (timer <= 0.0f)
{
windUp = false;
hold = true;
animator.Play(ai.atkHoldAnim);
timer = holdTime;
//ai.attackHitbox.SetActive(true);
//machine.GetScript<HomeOwnerAI>().atk = true;
2023-03-25 14:51:28 +08:00
}
2023-03-26 13:27:42 +08:00
}
else if (hold)
{
if (timer <= 0.0f)
2023-03-25 14:51:28 +08:00
{
2023-03-26 13:27:42 +08:00
hold = false;
seq = true;
animator.PlayOneShot(ai.atkSeqAnim);
timer = seqTime;
}
}
else if (seq)
{
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
if (timer <= 1.0f / 30.0f)
{
//ai.attackHitbox.SetActive(true);
machine.GetScript<HomeOwnerAI>().atk = true;
2023-03-25 14:51:28 +08:00
}
2023-03-26 13:27:42 +08:00
if (timer <= 0.0f)
2023-03-25 14:51:28 +08:00
{
2023-03-26 13:27:42 +08:00
seq = false;
end = true;
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
timer = endTime;
2023-03-25 14:51:28 +08:00
2023-03-26 13:27:42 +08:00
}
}
else if (end)
{
if (timer <= 0.0f)
2023-03-25 14:51:28 +08:00
{
2023-03-26 13:27:42 +08:00
end = false;
AILineOfSight los = ai.GetScript<AILineOfSight>();
if (los && los.withinSight)
{
machine.SetState(typeof(ChaseState));
}
else
{
machine.SetState(typeof(TimeoutState));
}
2023-03-25 14:51:28 +08:00
}
2023-03-30 15:26:15 +08:00
2023-03-26 13:27:42 +08:00
}
2023-03-25 14:51:28 +08:00
2023-03-30 15:26:15 +08:00
2023-03-25 14:51:28 +08:00
}
2023-03-26 13:27:42 +08:00
public override void fixedUpdate()
{
}
}
2023-03-25 14:51:28 +08:00
}