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.

107 lines
3.0 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. }
  39. public void moveToNextPoint()
  40. {
  41. LeanTween.delayedCall(gameObject, timeBetweenPoints, ()=>{
  42. if (reverseDirection)
  43. targetPoint--;
  44. else
  45. targetPoint++;
  46. if (targetPoint >= splineDistantPoints.Count)
  47. {
  48. if (splineCurve.Closed)
  49. {
  50. targetPoint = 1;
  51. splineCursor.Distance = 0f;
  52. }
  53. else
  54. {
  55. targetPoint = splineDistantPoints.Count - 2;
  56. reverseDirection = !reverseDirection;
  57. }
  58. }
  59. midpoint = true;
  60. float start = splineCursor.Distance;
  61. float end = splineDistantPoints[targetPoint];
  62. float distance = Mathf.Abs(end - start);
  63. LeanTween.value(gameObject, start, end, distance / speed + timePadding).setOnUpdate((float val)=>{
  64. splineCursor.Distance = val;
  65. }).setEase(animationCurve).setOnComplete(()=>{
  66. midpoint = false;
  67. moveToNextPoint();
  68. });
  69. });
  70. }
  71. public void searchlightStateChanged()
  72. {
  73. LeanTween.cancel(gameObject, false);
  74. if (Searchlight.state == Searchlight.SearchState.Chasing)
  75. {
  76. splineObjectTranslate.enabled = false;
  77. }
  78. else if (Searchlight.state == Searchlight.SearchState.Returning)
  79. {
  80. splineObjectTranslate.enabled = false;
  81. Vector3 start = splineObjectTranslate.ObjectToManipulate.position;
  82. Vector3 end = splineCursor.CalculatePosition();
  83. float distance = (end - start).magnitude;
  84. LeanTween.move(splineObjectTranslate.ObjectToManipulate.gameObject, end, distance / speed + timePadding).setEase(animationCurve).setOnComplete(()=>{
  85. splineObjectTranslate.enabled = true;
  86. if (midpoint)
  87. {
  88. if (reverseDirection)
  89. targetPoint++;
  90. else
  91. targetPoint--;
  92. }
  93. moveToNextPoint();
  94. NotificationServer.notify("statechange Searchlight safe");
  95. });
  96. }
  97. }
  98. }