using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Reflection.PortableExecutable; using System.Text; using System.Threading.Tasks; using SHADE; using SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States; namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework { public class HomeOwnerAI:Script { public float idleDuration = 1.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; [NonSerialized] public IEnumerable patrolPointPool; protected override void awake() { StateMachine machine = GetScript(); if(machine) { Dictionary dictionary = new Dictionary(); 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)); machine.InitStateMachine(dictionary); } patrolPointPool = patrolPointParent.GetComponentsInChildren(); } protected override void update() { if(alertCooldown > 0.0f) { alertCooldown -= Time.DeltaTimeF; } else { alertCooldown = 0.0f; } } public bool ShouldTransitAlert() { AILineOfSight los = GetScript(); if (los) { if (los.withinSight && alertCooldown <= 0.0f) { return true; } } return false; } public void RotateToPlayer() { Transform playerTransform = player.GetComponent(); //Rotate to face player. Transform aiTransform = GetComponent(); 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); } } } }