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.

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