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

187 lines
3.8 KiB
C#
Raw Permalink 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;
bool raccoonCaught = true;
2023-03-26 13:27:42 +08:00
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;
raccoonCaught = false;
2023-03-26 13:27:42 +08:00
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-04-02 13:32:21 +08:00
if (ai.hitboxScript.raccoonFound && machine.GetScript<HomeOwnerAI>().atk)
2023-03-30 15:26:15 +08:00
{
raccoonCaught = true;
Transform pcTransform = ai.player.GetComponent<Transform>();
2023-04-01 17:22:51 +08:00
ai.player.GetScript<PlayerController>().Caught();
Transform netTransform = ai.attackHitbox.GetComponentInChildren<Transform>();
if (pcTransform && netTransform)
{
pcTransform.GlobalPosition = netTransform.GlobalPosition;
}
2023-03-30 15:26:15 +08:00
}
2023-03-30 15:26:15 +08:00
timer -= Time.DeltaTimeF;
2023-03-26 13:27:42 +08:00
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-04-02 12:34:47 +08:00
if (timer <= seqTime * 0.5f)
2023-03-26 13:27:42 +08:00
{
//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;
if(raccoonCaught)
2023-03-26 13:27:42 +08:00
{
machine.SetState(typeof(CaughtRaccoonState));
2023-03-26 13:27:42 +08:00
}
else
{
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
}