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.

116 lines
2.9 KiB

8 years ago
8 years ago
8 years ago
8 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 safe", stateSafe);
  34. NotificationServer.register("statechange Searchlight returning", stateReturning);
  35. if (!instants.Contains(this))
  36. instants.Add(this);
  37. }
  38. void OnTriggerEnter(Collider other)
  39. {
  40. if (other.tag != "Player")
  41. return;
  42. chased = other;
  43. isTriggering = true;
  44. if (state != SearchState.Chasing)
  45. {
  46. state = SearchState.Chasing;
  47. NotificationServer.notify("statechange Searchlight");
  48. NotificationServer.notify("chasing boat");
  49. }
  50. LeanTween.cancel(gameObject, false);
  51. LeanTween.move(gameObject, chased.transform.position, 0.5f).setEase(animationCurve).setOnComplete(chase);
  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 chase()
  84. {
  85. if (isTriggering)
  86. {
  87. LeanTween.delayedCall(gameObject, timeBetweenChase, chase);
  88. }
  89. else
  90. {
  91. LeanTween.delayedCall(gameObject, timeBetweenChase, ()=>{
  92. Vector3 start = transform.position;
  93. Vector3 end = chased.transform.position
  94. + Vector3.right * Random.Range(-randomFactor, randomFactor)
  95. + Vector3.forward * Random.Range(-randomFactor, randomFactor);
  96. float distance = (end - start).magnitude;
  97. LeanTween.value(gameObject, 0f, 1f, distance / speed + timePadding).setOnUpdate((float val)=>{
  98. transform.position = start + (end-start)*val;
  99. }).setEase(animationCurve).setOnComplete(chase);
  100. });
  101. }
  102. }
  103. }