SHADE_Y3/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs

148 lines
4.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using SHADE;
using SHADE_Scripting.Audio;
2023-03-24 13:26:18 +08:00
using SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States;
namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework
{
public class HomeOwnerAI:Script
{
2023-03-24 13:26:18 +08:00
public float idleDuration = 1.0f;
public float timeoutDuration = 2.0f;
public GameObject patrolPointParent;
2023-03-24 13:26:18 +08:00
public float patrolSpeed = 2.0f;
public float chaseSpeed = 3.0f;
2023-03-24 13:26:18 +08:00
public float alertCooldown = 0.0f;
public GameObject player;
2023-03-25 14:51:28 +08:00
public GameObject attackHitbox;
2023-03-24 13:26:18 +08:00
2023-03-25 14:51:28 +08:00
public AnimationClipAsset walkingAnim;
public AnimationClipAsset idleAnim;
public AnimationClipAsset alertAnim;
public AnimationClipAsset alertRunAnim;
public AnimationClipAsset alertIdleAnim;
public AnimationClipAsset timeoutAnim;
public AnimationClipAsset atkWindupAnim;
public AnimationClipAsset atkHoldAnim;
public AnimationClipAsset atkSeqAnim;
2023-03-24 13:26:18 +08:00
2023-03-25 14:51:28 +08:00
private Vector3 startPos;
2023-03-24 13:26:18 +08:00
[NonSerialized]
public IEnumerable<Transform> patrolPointPool;
2023-03-25 14:51:28 +08:00
public void Reset()
{
Transform transform = GetComponent<Transform>();
StateMachine machine = GetScript<StateMachine>();
if (transform && machine)
{
transform.GlobalPosition = startPos;
machine.SetState(typeof(IdleState));
}
}
2023-03-24 13:26:18 +08:00
protected override void awake()
{
StateMachine machine = GetScript<StateMachine>();
if(machine)
{
Dictionary<Type, BaseState> dictionary = new Dictionary<Type, BaseState>();
dictionary.Add(typeof(IdleState), new IdleState(machine));
dictionary.Add(typeof(PatrolState), new PatrolState(machine));
dictionary.Add(typeof(TimeoutState), new TimeoutState(machine));
dictionary.Add(typeof(ChaseState), new ChaseState(machine));
dictionary.Add(typeof(AlertState), new AlertState(machine));
2023-03-25 14:51:28 +08:00
dictionary.Add(typeof(AttackState), new AttackState(machine));
2023-03-24 13:26:18 +08:00
machine.InitStateMachine(dictionary);
}
AudioHandler.audioClipHandlers["HO_footsteps"] = SHADE.Audio.CreateAudioClip("event:/Homeowner/homeowner_footsteps");
2023-03-24 13:26:18 +08:00
patrolPointPool = patrolPointParent.GetComponentsInChildren<Transform>();
2023-03-25 14:51:28 +08:00
Transform transform = GetComponent<Transform>();
if (transform)
{
startPos = transform.GlobalPosition;
}
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
protected override void start()
{
attackHitbox.SetActive(false);
2023-03-24 13:26:18 +08:00
}
protected override void update()
{
if(alertCooldown > 0.0f)
{
alertCooldown -= Time.DeltaTimeF;
}
else
{
alertCooldown = 0.0f;
}
2023-03-25 14:51:28 +08:00
AICheat();
2023-03-24 13:26:18 +08:00
}
public bool ShouldTransitAlert()
{
AILineOfSight los = GetScript<AILineOfSight>();
if (los)
{
if (los.withinSight && alertCooldown <= 0.0f)
{
return true;
}
}
return false;
}
public void RotateToPlayer()
{
2023-03-24 16:06:35 +08:00
//Transform playerTransform = player.GetComponent<Transform>();
////Rotate to face player.
//Transform aiTransform = GetComponent<Transform>();
//if(playerTransform && aiTransform)
//{
// Vector3 direction = playerTransform.GlobalPosition - aiTransform.GlobalPosition;
// Quaternion currentRotation = aiTransform.LocalRotation;
// Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(direction.x, direction.z), 0.0f);
// aiTransform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, 5.0f * (float)Time.FixedDeltaTime);
//}
2023-03-24 13:26:18 +08:00
}
2023-03-25 14:51:28 +08:00
private void AICheat()
{
StateMachine machine = GetScript<StateMachine>();
if(machine)
{
if (Input.GetKeyDown(Input.KeyCode.F))
{
machine.SetState(typeof(AlertState));
}
if(Input.GetKeyDown(Input.KeyCode.L))
{
machine.SetState(typeof(IdleState));
}
}
}
}
}