using System.Collections; using System.Collections.Generic; using UnityEngine; public class Searchlight : MonoBehaviour { public enum SearchState { Spline, Chasing, Returning } public float speed = 500f; public float timePadding = 0.5f; public float timeBetweenChase = 0.5f; public float randomFactor = 50f; public AnimationCurve animationCurve; [HideInInspector] public bool isTriggering = false; public static SearchState state = SearchState.Spline; public static Collider chased; public static bool playerHidden = false; private int chaseID; private static List instants = new List(); public static bool isTriggeringAtLeastOne() { foreach (Searchlight sl in instants) { if (sl.isActiveAndEnabled && sl.isTriggering) return true; } return false; } void Awake() { NotificationServer.register("statechange Searchlight safe", stateSafe); NotificationServer.register("statechange Searchlight returning", stateReturning); NotificationServer.register("spotted boat", spottedBoat); NotificationServer.register("restart scene", stateSafe); if (!instants.Contains(this)) instants.Add(this); } void OnTriggerEnter(Collider other) { if (other.tag != "Player" || playerHidden ) return; chased = other; if (state != SearchState.Chasing) { state = SearchState.Chasing; NotificationServer.notify("statechange Searchlight"); NotificationServer.notify("chasing boat"); } LeanTween.cancel(gameObject, false); NotificationServer.notify("spotted boat"); } void OnTriggerExit(Collider other) { if (other.tag != "Player" ) return; isTriggering = false; if (state == SearchState.Chasing && !isTriggeringAtLeastOne()) { // chased = null; // state = SearchState.Returning; // NotificationServer.notify("statechange Searchlight"); NotificationServer.notify("lost boat"); } } public void stateSafe() { state = SearchState.Spline; isTriggering = false; LeanTween.cancel(chaseID, false); } public void stateReturning() { state = SearchState.Returning; LeanTween.cancel(chaseID, false); } public void spottedBoat() { isTriggering = true; chaseID = LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase).uniqueId; } public void chase() { if (isTriggering) { chaseID = LeanTween.delayedCall(gameObject, timeBetweenChase, chase).uniqueId; } else { chaseID = LeanTween.delayedCall(gameObject, timeBetweenChase, ()=>{ Vector3 start = transform.position; Vector3 end = chased.transform.position + Vector3.right * Random.Range(-randomFactor, randomFactor) + Vector3.forward * Random.Range(-randomFactor, randomFactor); float distance = (end - start).magnitude; chaseID = LeanTween.value(gameObject, 0f, 1f, distance / speed + timePadding).setOnUpdate((float val)=>{ transform.position = start + (end-start)*val; }).setEase(animationCurve).setOnComplete(chase).uniqueId; }).uniqueId; } } }