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.

128 lines
3.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using BansheeGz.BGSpline.Components;
  5. using BansheeGz.BGSpline.Curve;
  6. public class Lighthouse : MonoBehaviour
  7. {
  8. public BGCurve splineCurve;
  9. public List<float> splineDistantPoints = new List<float>();
  10. public float speed = 200f;
  11. public float timePadding = 0.5f;
  12. public float timeBetweenPoints = 3f;
  13. public AnimationCurve animationCurve;
  14. private BGCcCursor splineCursor;
  15. private BGCcCursorObjectTranslate splineObjectTranslate;
  16. private int targetPoint = 0;
  17. private bool reverseDirection = false;
  18. private bool midpoint = false;
  19. private Searchlight.SearchState searchState;
  20. void Start()
  21. {
  22. splineCursor = splineCurve.GetComponent<BGCcCursor>();
  23. splineObjectTranslate = splineCurve.GetComponent<BGCcCursorObjectTranslate>();
  24. if (splineDistantPoints == null || splineDistantPoints.Count == 0)
  25. {
  26. splineDistantPoints.Add(0f);
  27. for (int i=1; i<splineCurve.PointsCount; i++)
  28. {
  29. BGCurvePointI point = splineCurve.Points[i];
  30. float distance = 0f;
  31. splineCursor.Math.CalcPositionByClosestPoint(point.PositionWorld, out distance);
  32. splineDistantPoints.Add(distance);
  33. }
  34. splineDistantPoints.Add(splineCursor.Math.GetDistance());
  35. }
  36. splineCursor.Distance = 0f;
  37. searchState = Searchlight.SearchState.Spline;
  38. moveToNextPoint();
  39. NotificationServer.register("statechange Searchlight", searchlightStateChanged);
  40. NotificationServer.register("restart scene", restartScene);
  41. }
  42. public void moveToNextPoint()
  43. {
  44. LeanTween.delayedCall(gameObject, timeBetweenPoints, ()=>{
  45. if (reverseDirection)
  46. targetPoint--;
  47. else
  48. targetPoint++;
  49. if (targetPoint >= splineDistantPoints.Count)
  50. {
  51. if (splineCurve.Closed)
  52. {
  53. targetPoint = 1;
  54. splineCursor.Distance = 0f;
  55. }
  56. else
  57. {
  58. targetPoint = splineDistantPoints.Count - 2;
  59. reverseDirection = !reverseDirection;
  60. }
  61. }
  62. midpoint = true;
  63. float start = splineCursor.Distance;
  64. float end = splineDistantPoints[targetPoint];
  65. float distance = Mathf.Abs(end - start);
  66. LeanTween.value(gameObject, start, end, distance / speed + timePadding).setOnUpdate((float val)=>{
  67. splineCursor.Distance = val;
  68. }).setEase(animationCurve).setOnComplete(()=>{
  69. midpoint = false;
  70. moveToNextPoint();
  71. });
  72. });
  73. }
  74. public void searchlightStateChanged()
  75. {
  76. if (searchState == Searchlight.state)
  77. return;
  78. searchState = Searchlight.state;
  79. LeanTween.cancel(gameObject, false);
  80. if (Searchlight.state == Searchlight.SearchState.Chasing)
  81. {
  82. splineObjectTranslate.enabled = false;
  83. }
  84. else if (Searchlight.state == Searchlight.SearchState.Returning)
  85. {
  86. splineObjectTranslate.enabled = false;
  87. Vector3 start = splineObjectTranslate.ObjectToManipulate.position;
  88. Vector3 end = splineCursor.CalculatePosition();
  89. float distance = (end - start).magnitude;
  90. LeanTween.move(splineObjectTranslate.ObjectToManipulate.gameObject, end, distance / speed + timePadding).setEase(animationCurve).setOnComplete(()=>{
  91. splineObjectTranslate.enabled = true;
  92. if (midpoint)
  93. {
  94. if (reverseDirection)
  95. targetPoint++;
  96. else
  97. targetPoint--;
  98. }
  99. moveToNextPoint();
  100. NotificationServer.notify("statechange Searchlight safe");
  101. });
  102. }
  103. }
  104. public void restartScene()
  105. {
  106. LeanTween.cancel(gameObject, false);
  107. Searchlight.state = Searchlight.SearchState.Spline;
  108. splineObjectTranslate.enabled = true;
  109. splineCursor.Distance = 0f;
  110. midpoint = false;
  111. reverseDirection = false;
  112. targetPoint = 0;
  113. splineObjectTranslate.ObjectToManipulate.position = splineCursor.CalculatePosition();
  114. searchState = Searchlight.SearchState.Spline;
  115. moveToNextPoint();
  116. NotificationServer.notify("statechange Searchlight safe");
  117. }
  118. }