diff options
author | chai <215380520@qq.com> | 2024-03-21 10:28:46 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-21 10:28:46 +0800 |
commit | 3fb2121cc0d00cbd42b2ca10b5dfb399a4df1a04 (patch) | |
tree | c1f4683fb021522b459408ab1ad61c40be77ee47 /GameCode/Pathfinder.cs | |
parent | 9ee2cfa385ed77c39003f524f5f03079124fc476 (diff) |
*misc
Diffstat (limited to 'GameCode/Pathfinder.cs')
-rw-r--r-- | GameCode/Pathfinder.cs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/GameCode/Pathfinder.cs b/GameCode/Pathfinder.cs new file mode 100644 index 0000000..5426574 --- /dev/null +++ b/GameCode/Pathfinder.cs @@ -0,0 +1,91 @@ +using UnityEngine; + +public class Pathfinder : MonoBehaviour +{ + public float distanceFromEnd = 2.1474836E+09f; + + public bool atEnd; + + public float speed = 1f; + + public Waypoint currentWaypoint; + + [SerializeField] + private Enemy enemyScript; + + private void Start() + { + if (enemyScript == null) + { + enemyScript = GetComponent<Enemy>(); + } + } + + private void FixedUpdate() + { + CheckWaypointDistance(); + Move(); + } + + private void Move() + { + Vector3 vector = currentWaypoint.transform.position - base.transform.position; + vector.Normalize(); + base.transform.Translate(vector * speed * Time.fixedDeltaTime); + distanceFromEnd -= speed * Time.fixedDeltaTime; + } + + private void CheckWaypointDistance() + { + if (Vector3.SqrMagnitude(currentWaypoint.transform.position - base.transform.position) < 4f * speed * speed * Time.fixedDeltaTime * Time.fixedDeltaTime && !GetNewWaypoint()) + { + atEnd = true; + enemyScript.AtEnd(); + } + } + + public Vector3 GetFuturePosition(float distance) + { + Vector3 position = base.transform.position; + Waypoint nextWaypoint = currentWaypoint; + float num = distance; + int num2 = 0; + while (num > 0f) + { + if (Vector3.SqrMagnitude(nextWaypoint.transform.position - position) >= num * num) + { + return position + (nextWaypoint.transform.position - position).normalized * num; + } + if (nextWaypoint.GetNextWaypoint() == nextWaypoint) + { + return nextWaypoint.transform.position; + } + num -= Vector3.Magnitude(nextWaypoint.transform.position - position); + position = nextWaypoint.transform.position; + nextWaypoint = nextWaypoint.GetNextWaypoint(); + num2++; + if (num2 > 100) + { + Debug.LogError("GetFuturePosition looping too much"); + break; + } + } + Debug.LogError("GetFuturePosition broken"); + return Vector3.zero; + } + + private bool GetNewWaypoint() + { + if (currentWaypoint.GetNextWaypoint() == currentWaypoint) + { + return false; + } + distanceFromEnd = currentWaypoint.distanceFromEnd; + currentWaypoint = currentWaypoint.GetNextWaypoint(); + if (distanceFromEnd <= 24f) + { + enemyScript.CheckBattleCries(BattleCry.BattleCryTrigger.NearEnd); + } + return true; + } +} |