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

177 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using SHADE;
using SHADE_Scripting.Audio;
using SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States;
namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework
{
public class HomeOwnerAI : Script
{
public float idleDuration = 1.0f;
public float caughtDuration = 2.0f;
public float timeoutDuration = 2.0f;
public GameObject patrolPointParent;
public float patrolSpeed = 2.0f;
public float chaseSpeed = 3.0f;
public float alertCooldown = 0.0f;
public GameObject player;
public GameObject attackHitbox;
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;
private Vector3 startPos;
public bool atk { get; set; }
[NonSerialized]
public IEnumerable<Transform> patrolPointPool;
private List<Transform> pppList;
private Transform transform;
[NonSerialized]
public HomeOwnerAttackHitbox hitboxScript;
public void Reset()
{
StateMachine machine = GetScript<StateMachine>();
if (transform && machine)
{
transform.GlobalPosition = startPos;
machine.SetState(typeof(IdleState));
}
}
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));
dictionary.Add(typeof(CaughtRaccoonState), new CaughtRaccoonState(machine));
machine.InitStateMachine(dictionary);
}
AudioHandler.audioClipHandlers["HO_footsteps"] = SHADE.Audio.CreateAudioClip("event:/Homeowner/homeowner_footsteps");
SHADE.Audio.AttachAudioClipToObject(AudioHandler.audioClipHandlers["HO_footsteps"], GameObject.EntityId);
AudioHandler.audioClipHandlers["HO_bark"] = SHADE.Audio.CreateAudioClip("event:/Homeowner/homeowner_bark");
SHADE.Audio.AttachAudioClipToObject(AudioHandler.audioClipHandlers["HO_bark"], GameObject.EntityId);
AudioHandler.audioClipHandlers["HO_humming"] = SHADE.Audio.CreateAudioClip("event:/Homeowner/homeowner_humming");
SHADE.Audio.AttachAudioClipToObject(AudioHandler.audioClipHandlers["HO_humming"], GameObject.EntityId);
patrolPointPool = patrolPointParent.GetComponentsInChildren<Transform>();
pppList = patrolPointPool.ToList<Transform>();
transform = GetComponent<Transform>();
atk = false;
}
protected override void start()
{
//attackHitbox.SetActive(false);
if (pppList != null)
startPos = pppList[0].LocalPosition;
if (attackHitbox)
hitboxScript = attackHitbox.GetScript<HomeOwnerAttackHitbox>();
}
protected override void update()
{
if (alertCooldown > 0.0f)
{
alertCooldown -= Time.DeltaTimeF;
}
else
{
alertCooldown = 0.0f;
}
AICheat();
}
public bool ShouldTransitAlert()
{
AILineOfSight los = GetScript<AILineOfSight>();
if (los)
{
if (los.withinSight && alertCooldown <= 0.0f)
{
return true;
}
}
return false;
}
public void RotateToPlayer()
{
//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);
//}
}
private void AICheat()
{
StateMachine machine = GetScript<StateMachine>();
AILineOfSight los = GetScript<AILineOfSight>();
if (machine && los)
{
if (Input.GetKeyDown(Input.KeyCode.K))
{
los.range = 5.0f;
machine.SetState(typeof(AlertState));
}
if (Input.GetKeyDown(Input.KeyCode.L))
{
los.range = 0.0f;
machine.SetState(typeof(IdleState));
}
}
}
}
}