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.

115 lines
2.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  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. if (other.tag != "Player")
  42. return;
  43. chased = other;
  44. isTriggering = true;
  45. if (state != SearchState.Chasing)
  46. {
  47. state = SearchState.Chasing;
  48. NotificationServer.notify("statechange Searchlight");
  49. NotificationServer.notify("chasing boat");
  50. }
  51. NotificationServer.notify("spotted boat");
  52. }
  53. void OnTriggerExit(Collider other)
  54. {
  55. if (other.tag != "Player")
  56. return;
  57. isTriggering = false;
  58. if (state == SearchState.Chasing && !isTriggeringAtLeastOne())
  59. {
  60. // chased = null;
  61. // state = SearchState.Returning;
  62. // NotificationServer.notify("statechange Searchlight");
  63. NotificationServer.notify("lost boat");
  64. }
  65. }
  66. public void stateSafe()
  67. {
  68. state = SearchState.Spline;
  69. // NotificationServer.notify("statechange Searchlight");
  70. }
  71. public void stateReturning()
  72. {
  73. state = SearchState.Returning;
  74. NotificationServer.notify("statechange Searchlight");
  75. }
  76. public void stateChanged()
  77. {
  78. LeanTween.cancel(gameObject, false);
  79. if (state == SearchState.Chasing)
  80. LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase);
  81. }
  82. public void chase()
  83. {
  84. if (isTriggering)
  85. {
  86. LeanTween.delayedCall(gameObject, timeBetweenChase, chase);
  87. }
  88. else
  89. {
  90. LeanTween.delayedCall(gameObject, timeBetweenChase, ()=>{
  91. Vector3 start = transform.position;
  92. Vector3 end = chased.transform.position
  93. + Vector3.right * Random.Range(-randomFactor, randomFactor)
  94. + Vector3.forward * Random.Range(-randomFactor, randomFactor);
  95. float distance = (end - start).magnitude;
  96. LeanTween.value(gameObject, 0f, 1f, distance / speed + timePadding).setOnUpdate((float val)=>{
  97. transform.position = start + (end-start)*val;
  98. }).setEase(animationCurve).setOnComplete(chase);
  99. });
  100. }
  101. }
  102. }