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.

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