From 3fb2121cc0d00cbd42b2ca10b5dfb399a4df1a04 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 21 Mar 2024 10:28:46 +0800 Subject: *misc --- GameCode/Waypoint.cs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 GameCode/Waypoint.cs (limited to 'GameCode/Waypoint.cs') diff --git a/GameCode/Waypoint.cs b/GameCode/Waypoint.cs new file mode 100644 index 0000000..b250079 --- /dev/null +++ b/GameCode/Waypoint.cs @@ -0,0 +1,62 @@ +using System.Collections.Generic; +using UnityEngine; + +public class Waypoint : MonoBehaviour +{ + [SerializeField] + private Waypoint next; + + [SerializeField] + private List previous = new List(); + + public float distanceFromEnd; + + public bool trueDistance; + + private void Start() + { + UpdateDistance(); + } + + public Waypoint GetNextWaypoint() + { + if (next != null) + { + return next; + } + return this; + } + + public void SetNextWaypoint(Waypoint newNext) + { + next = newNext; + } + + public void AddPreviousWaypoint(Waypoint previousWaypoint) + { + previous.Add(previousWaypoint); + } + + public Waypoint[] GetPreviousWaypoints() + { + return previous.ToArray(); + } + + public void UpdateDistance() + { + if (next == null) + { + trueDistance = true; + return; + } + if (!next.trueDistance) + { + next.UpdateDistance(); + } + if (!trueDistance) + { + distanceFromEnd = next.distanceFromEnd + Vector3.Magnitude(next.transform.position - base.transform.position); + trueDistance = true; + } + } +} -- cgit v1.1-26-g67d0