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.

111 lines
2.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Searchlight : MonoBehaviour
  5. {
  6. public enum SearchState
  7. {
  8. Spline,
  9. Chasing,
  10. Returning
  11. }
  12. public float speed = 500f;
  13. public float timePadding = 0.5f;
  14. public float timeBetweenChase = 0.5f;
  15. public float randomFactor = 50f;
  16. public AnimationCurve animationCurve;
  17. [HideInInspector]
  18. public bool isTriggering = false;
  19. public static SearchState state = SearchState.Spline;
  20. public static Collider chased;
  21. private static List<Searchlight> instants = new List<Searchlight>();
  22. public static bool isTriggeringAtLeastOne()
  23. {
  24. foreach (Searchlight sl in instants)
  25. {
  26. if (sl.isTriggering)
  27. return true;
  28. }
  29. return false;
  30. }
  31. void Awake()
  32. {
  33. NotificationServer.register("statechange Searchlight", stateChanged);
  34. NotificationServer.register("statechange Searchlight safe", stateSafe);
  35. NotificationServer.register("statechange Searchlight returning", stateReturning);
  36. if (!instants.Contains(this))
  37. instants.Add(this);
  38. }
  39. void OnTriggerEnter(Collider other)
  40. {
  41. chased = other;
  42. isTriggering = true;
  43. if (state != SearchState.Chasing)
  44. {
  45. state = SearchState.Chasing;
  46. NotificationServer.notify("statechange Searchlight");
  47. NotificationServer.notify("chasing boat");
  48. }
  49. NotificationServer.notify("spotted boat");
  50. }
  51. void OnTriggerExit(Collider other)
  52. {
  53. isTriggering = false;
  54. if (state == SearchState.Chasing && !isTriggeringAtLeastOne())
  55. {
  56. // chased = null;
  57. // state = SearchState.Returning;
  58. // NotificationServer.notify("statechange Searchlight");
  59. NotificationServer.notify("lost boat");
  60. }
  61. }
  62. public void stateSafe()
  63. {
  64. state = SearchState.Spline;
  65. // NotificationServer.notify("statechange Searchlight");
  66. }
  67. public void stateReturning()
  68. {
  69. state = SearchState.Returning;
  70. NotificationServer.notify("statechange Searchlight");
  71. }
  72. public void stateChanged()
  73. {
  74. LeanTween.cancel(gameObject, false);
  75. if (state == SearchState.Chasing)
  76. LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase);
  77. }
  78. public void chase()
  79. {
  80. if (isTriggering)
  81. {
  82. LeanTween.delayedCall(gameObject, timeBetweenChase, chase);
  83. }
  84. else
  85. {
  86. LeanTween.delayedCall(gameObject, timeBetweenChase, ()=>{
  87. Vector3 start = transform.localPosition;
  88. Vector3 end = chased.transform.localPosition
  89. + Vector3.right * Random.Range(-randomFactor, randomFactor)
  90. + Vector3.forward * Random.Range(-randomFactor, randomFactor);
  91. float distance = (end - start).magnitude;
  92. LeanTween.value(gameObject, 0f, 1f, distance / speed + timePadding).setOnUpdate((float val)=>{
  93. transform.localPosition = start + (end-start)*val;
  94. }).setEase(animationCurve).setOnComplete(chase);
  95. });
  96. }
  97. }
  98. }