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.

119 lines
3.0 KiB

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