summaryrefslogtreecommitdiff
path: root/GameCode/Pathfinder.cs
blob: 54265741071f54a202ea3a9152208a84c9dcf879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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;
	}
}