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.

122 lines
3.5 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. void Start()
  20. {
  21. splineCursor = splineCurve.GetComponent<BGCcCursor>();
  22. splineObjectTranslate = splineCurve.GetComponent<BGCcCursorObjectTranslate>();
  23. if (splineDistantPoints == null || splineDistantPoints.Count == 0)
  24. {
  25. splineDistantPoints.Add(0f);
  26. for (int i=1; i<splineCurve.PointsCount; i++)
  27. {
  28. BGCurvePointI point = splineCurve.Points[i];
  29. float distance = 0f;
  30. splineCursor.Math.CalcPositionByClosestPoint(point.PositionWorld, out distance);
  31. splineDistantPoints.Add(distance);
  32. }
  33. splineDistantPoints.Add(splineCursor.Math.GetDistance());
  34. }
  35. splineCursor.Distance = 0f;
  36. moveToNextPoint();
  37. NotificationServer.register("statechange Searchlight", searchlightStateChanged);
  38. NotificationServer.register("restart scene", restartScene);
  39. }
  40. public void moveToNextPoint()
  41. {
  42. LeanTween.delayedCall(gameObject, timeBetweenPoints, ()=>{
  43. if (reverseDirection)
  44. targetPoint--;
  45. else
  46. targetPoint++;
  47. if (targetPoint >= splineDistantPoints.Count)
  48. {
  49. if (splineCurve.Closed)
  50. {
  51. targetPoint = 1;
  52. splineCursor.Distance = 0f;
  53. }
  54. else
  55. {
  56. targetPoint = splineDistantPoints.Count - 2;
  57. reverseDirection = !reverseDirection;
  58. }
  59. }
  60. midpoint = true;
  61. float start = splineCursor.Distance;
  62. float end = splineDistantPoints[targetPoint];
  63. float distance = Mathf.Abs(end - start);
  64. LeanTween.value(gameObject, start, end, distance / speed + timePadding).setOnUpdate((float val)=>{
  65. splineCursor.Distance = val;
  66. }).setEase(animationCurve).setOnComplete(()=>{
  67. midpoint = false;
  68. moveToNextPoint();
  69. });
  70. });
  71. }
  72. public void searchlightStateChanged()
  73. {
  74. LeanTween.cancel(gameObject, false);
  75. if (Searchlight.state == Searchlight.SearchState.Chasing)
  76. {
  77. splineObjectTranslate.enabled = false;
  78. }
  79. else if (Searchlight.state == Searchlight.SearchState.Returning)
  80. {
  81. splineObjectTranslate.enabled = false;
  82. Vector3 start = splineObjectTranslate.ObjectToManipulate.position;
  83. Vector3 end = splineCursor.CalculatePosition();
  84. float distance = (end - start).magnitude;
  85. LeanTween.move(splineObjectTranslate.ObjectToManipulate.gameObject, end, distance / speed + timePadding).setEase(animationCurve).setOnComplete(()=>{
  86. splineObjectTranslate.enabled = true;
  87. if (midpoint)
  88. {
  89. if (reverseDirection)
  90. targetPoint++;
  91. else
  92. targetPoint--;
  93. }
  94. moveToNextPoint();
  95. NotificationServer.notify("statechange Searchlight safe");
  96. });
  97. }
  98. }
  99. public void restartScene()
  100. {
  101. LeanTween.cancel(gameObject, false);
  102. Searchlight.state = Searchlight.SearchState.Spline;
  103. splineObjectTranslate.enabled = true;
  104. splineCursor.Distance = 0f;
  105. midpoint = false;
  106. reverseDirection = false;
  107. targetPoint = 0;
  108. splineObjectTranslate.ObjectToManipulate.position = splineCursor.CalculatePosition();
  109. moveToNextPoint();
  110. NotificationServer.notify("statechange Searchlight safe");
  111. }
  112. }