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

154 lines
4.0 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
{
2023-03-26 13:27:42 +08:00
public class HomeOwnerAI : Script
{
public float idleDuration = 1.0f;
public float timeoutDuration = 2.0f;
public GameObject patrolPointParent;
2023-03-26 13:27:42 +08:00
public float patrolSpeed = 2.0f;
public float chaseSpeed = 3.0f;
2023-03-26 13:27:42 +08:00
public float alertCooldown = 0.0f;
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +08:00
public GameObject player;
public GameObject attackHitbox;
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +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-26 13:27:42 +08:00
private Vector3 startPos;
public bool atk { get; set; }
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +08:00
[NonSerialized]
public IEnumerable<Transform> patrolPointPool;
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +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-25 14:51:28 +08:00
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +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));
dictionary.Add(typeof(AttackState), new AttackState(machine));
machine.InitStateMachine(dictionary);
}
AudioHandler.audioClipHandlers["HO_footsteps"] = SHADE.Audio.CreateAudioClip("event:/Homeowner/homeowner_footsteps");
patrolPointPool = patrolPointParent.GetComponentsInChildren<Transform>();
Transform transform = GetComponent<Transform>();
if (transform)
{
startPos = transform.GlobalPosition;
}
atk = false;
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +08:00
}
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +08:00
protected override void start()
{
//attackHitbox.SetActive(false);
}
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +08:00
protected override void update()
{
if (alertCooldown > 0.0f)
{
alertCooldown -= Time.DeltaTimeF;
}
else
{
alertCooldown = 0.0f;
}
AICheat();
}
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +08:00
public bool ShouldTransitAlert()
{
AILineOfSight los = GetScript<AILineOfSight>();
if (los)
{
if (los.withinSight && alertCooldown <= 0.0f)
2023-03-24 13:26:18 +08:00
{
2023-03-26 13:27:42 +08:00
return true;
2023-03-24 13:26:18 +08:00
}
2023-03-26 13:27:42 +08:00
}
return false;
}
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +08:00
public void RotateToPlayer()
{
2023-03-24 13:26:18 +08:00
2023-03-26 13:27:42 +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-26 13:27:42 +08:00
}
private void AICheat()
{
StateMachine machine = GetScript<StateMachine>();
AILineOfSight los = GetScript<AILineOfSight>();
if (machine && los)
{
if (Input.GetKeyDown(Input.KeyCode.K))
2023-03-25 14:51:28 +08:00
{
2023-03-26 13:27:42 +08:00
los.range = 5.0f;
machine.SetState(typeof(AlertState));
}
if (Input.GetKeyDown(Input.KeyCode.L))
{
los.range = 0.0f;
machine.SetState(typeof(IdleState));
2023-03-25 14:51:28 +08:00
}
2023-03-26 13:27:42 +08:00
}
}
2023-03-26 13:27:42 +08:00
}
}