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.

70 lines
1.8 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 int targetPoint = 0;
  16. private bool reverseDirection = false;
  17. void Start()
  18. {
  19. splineCursor = splineCurve.GetComponent<BGCcCursor>();
  20. if (splineDistantPoints == null || splineDistantPoints.Count == 0)
  21. {
  22. splineDistantPoints.Add(0f);
  23. for (int i=1; i<splineCurve.PointsCount; i++)
  24. {
  25. BGCurvePointI point = splineCurve.Points[i];
  26. float distance = 0f;
  27. Vector3 temp = splineCursor.Math.CalcPositionByClosestPoint(point.PositionWorld, out distance);
  28. splineDistantPoints.Add(distance);
  29. }
  30. splineDistantPoints.Add(splineCursor.Math.GetDistance());
  31. }
  32. splineCursor.Distance = 0f;
  33. moveToNextPoint();
  34. }
  35. public void moveToNextPoint()
  36. {
  37. LeanTween.delayedCall(gameObject, timeBetweenPoints, ()=>{
  38. if (reverseDirection)
  39. targetPoint--;
  40. else
  41. targetPoint++;
  42. if (targetPoint >= splineDistantPoints.Count)
  43. {
  44. if (splineCurve.Closed)
  45. {
  46. targetPoint = 1;
  47. splineCursor.Distance = 0f;
  48. }
  49. else
  50. {
  51. targetPoint = splineDistantPoints.Count - 2;
  52. reverseDirection = !reverseDirection;
  53. }
  54. }
  55. float start = splineCursor.Distance;
  56. float end = splineDistantPoints[targetPoint];
  57. float distance = Mathf.Abs(end - start);
  58. LeanTween.value(gameObject, start, end, distance / speed + timePadding).setOnUpdate((float val)=>{
  59. splineCursor.Distance = val;
  60. }).setEase(animationCurve).setOnComplete(moveToNextPoint);
  61. });
  62. }
  63. }