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

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. public static bool playerHidden = false;
  22. private static List<Searchlight> instants = new List<Searchlight>();
  23. public static bool isTriggeringAtLeastOne()
  24. {
  25. foreach (Searchlight sl in instants)
  26. {
  27. if (sl.isTriggering)
  28. return true;
  29. }
  30. return false;
  31. }
  32. void Awake()
  33. {
  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. LeanTween.cancel(gameObject, false);
  52. LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase);
  53. NotificationServer.notify("spotted boat");
  54. }
  55. void OnTriggerExit(Collider other)
  56. {
  57. if (other.tag != "Player" || playerHidden)
  58. return;
  59. isTriggering = false;
  60. if (state == SearchState.Chasing && !isTriggeringAtLeastOne())
  61. {
  62. // chased = null;
  63. // state = SearchState.Returning;
  64. // NotificationServer.notify("statechange Searchlight");
  65. NotificationServer.notify("lost boat");
  66. }
  67. }
  68. public void stateSafe()
  69. {
  70. state = SearchState.Spline;
  71. // NotificationServer.notify("statechange Searchlight");
  72. }
  73. public void stateReturning()
  74. {
  75. state = SearchState.Returning;
  76. NotificationServer.notify("statechange Searchlight");
  77. }
  78. public void stateChanged()
  79. {
  80. LeanTween.cancel(gameObject, false);
  81. // if (state == SearchState.Chasing)
  82. // LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase);
  83. }
  84. public void chase()
  85. {
  86. if (isTriggering)
  87. {
  88. LeanTween.delayedCall(gameObject, timeBetweenChase, chase);
  89. }
  90. else
  91. {
  92. LeanTween.delayedCall(gameObject, timeBetweenChase, ()=>{
  93. Vector3 start = transform.position;
  94. Vector3 end = chased.transform.position
  95. + Vector3.right * Random.Range(-randomFactor, randomFactor)
  96. + Vector3.forward * Random.Range(-randomFactor, randomFactor);
  97. float distance = (end - start).magnitude;
  98. LeanTween.value(gameObject, 0f, 1f, distance / speed + timePadding).setOnUpdate((float val)=>{
  99. transform.position = start + (end-start)*val;
  100. }).setEase(animationCurve).setOnComplete(chase);
  101. });
  102. }
  103. }
  104. }