You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

118 lines
2.9 KiB

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 static List<Searchlight> instants = new List<Searchlight>();
public static bool isTriggeringAtLeastOne()
{
foreach (Searchlight sl in instants)
{
if (sl.isTriggering)
return true;
}
return false;
}
void Awake()
{
NotificationServer.register("statechange Searchlight safe", stateSafe);
NotificationServer.register("statechange Searchlight returning", stateReturning);
if (!instants.Contains(this))
instants.Add(this);
}
void OnTriggerEnter(Collider other)
{
if (other.tag != "Player")
return;
chased = other;
isTriggering = true;
if (state != SearchState.Chasing)
{
state = SearchState.Chasing;
NotificationServer.notify("statechange Searchlight");
NotificationServer.notify("chasing boat");
}
LeanTween.cancel(gameObject, false);
LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase);
NotificationServer.notify("spotted boat");
}
void OnTriggerExit(Collider other)
{
if (other.tag != "Player" || playerHidden)
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;
// NotificationServer.notify("statechange Searchlight");
}
public void stateReturning()
{
state = SearchState.Returning;
NotificationServer.notify("statechange Searchlight");
}
public void stateChanged()
{
LeanTween.cancel(gameObject, false);
// if (state == SearchState.Chasing)
// LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase);
}
public void chase()
{
if (isTriggering)
{
LeanTween.delayedCall(gameObject, timeBetweenChase, chase);
}
else
{
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;
LeanTween.value(gameObject, 0f, 1f, distance / speed + timePadding).setOnUpdate((float val)=>{
transform.position = start + (end-start)*val;
}).setEase(animationCurve).setOnComplete(chase);
});
}
}
}