summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Decompile/PathfindMovementPlayerunit.cs
blob: 490eb63994d10ef5347f671b7d60bf07e4336c81 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using System.Collections.Generic;
using Pathfinding;
using Pathfinding.RVO;
using UnityEngine;

[RequireComponent(typeof(Seeker))]
public class PathfindMovementPlayerunit : PathfindMovement, DayNightCycle.IDaytimeSensitive
{
	public List<TargetPriority> targetPriorities = new List<TargetPriority>();

	public float keepDistanceOf = 2f;

	public float maximumDistanceFromHome = 100000f;

	public float movementSpeed = 2f;

	public float recalculatePathInterval = 1f;

	public string backupMovementGraph = "";

	public float agroTimeWhenAttackedByPlayer = 5f;

	public float speedBoostDuringDaytime = 1.5f;

	private TaggedObject agroPlayerTarget;

	private Seeker seeker;

	private RVOController rvo;

	private Vector3 seekToTargetPosSnappedtoNavmesh = Vector3.zero;

	private Vector3 seekToTargetPos = Vector3.zero;

	private TaggedObject seekToTaggedObj;

	private List<Vector3> path = new List<Vector3>();

	private int nextPathPointIndex;

	private float recalculatePathCooldown;

	private Vector3 homePosition;

	private Vector3 homePositionOriginal;

	private bool currentlyWalkingHome = true;

	private TargetPriority targetPrio;

	private NNConstraint nearestConstraint = new NNConstraint();

	private GraphMask graphMaskOriginal;

	private Vector3 nextPathPoint;

	private SettingsManager settingsManager;

	private bool followingPlayer;

	[SerializeField]
	private GameObject holdPositionMarker;

	private Transform holdPositionMarkerTransform;

	private float slowedFor;

	private bool day = true;

	private bool holdPosition;

	private Vector3 storeRequestedTargetPos;

	public override RVOController RVO => rvo;

	public Vector3 HopePositionOriginal => homePositionOriginal;

	public Vector3 HomePosition
	{
		get
		{
			return homePosition;
		}
		set
		{
			homePosition = value;
		}
	}

	public Vector3 NextPathPoint => nextPathPoint;

	public override bool IsSlowed => slowedFor > 0f;

	public bool HoldPosition
	{
		get
		{
			return holdPosition;
		}
		set
		{
			holdPosition = value;
			holdPositionMarker.SetActive(value);
			if ((bool)holdPositionMarkerTransform)
			{
				holdPositionMarkerTransform.position = homePosition + 0.05f * Vector3.up;
			}
		}
	}

	public void FollowPlayer(bool _follow)
	{
		followingPlayer = _follow;
		HoldPosition = false;
	}

	public override void GetAgroFromObject(TaggedObject _agroTarget)
	{
	}

	private void OnDisable()
	{
		if ((bool)holdPositionMarker)
		{
			holdPositionMarker.SetActive(value: false);
		}
	}

	private void OnEnable()
	{
		HoldPosition = HoldPosition;
	}

	public void OnDusk()
	{
		day = false;
	}

	public void OnDawn_AfterSunrise()
	{
		day = true;
	}

	public void OnDawn_BeforeSunrise()
	{
		if (settingsManager.ResetUnitFormationEveryMorning)
		{
			homePosition = homePositionOriginal;
			HoldPosition = false;
		}
	}

	private void Start()
	{
		settingsManager = SettingsManager.Instance;
		holdPositionMarkerTransform = holdPositionMarker.transform;
		holdPositionMarkerTransform.SetParent(null);
		HoldPosition = false;
		seeker = GetComponent<Seeker>();
		rvo = GetComponent<RVOController>();
		recalculatePathCooldown = recalculatePathInterval * Random.value;
		homePosition = base.transform.position;
		homePositionOriginal = base.transform.position;
		nearestConstraint.graphMask = seeker.graphMask;
		graphMaskOriginal = seeker.graphMask;
		DayNightCycle.Instance.RegisterDaytimeSensitiveObject(this);
	}

	private void OriginalPathRequest()
	{
		seekToTargetPos = FindMoveToTarget();
		seekToTargetPosSnappedtoNavmesh = AstarPath.active.GetNearest(seekToTargetPos, nearestConstraint).position;
		storeRequestedTargetPos = seekToTargetPosSnappedtoNavmesh;
		seeker.StartPath(base.transform.position, seekToTargetPosSnappedtoNavmesh, OriginalOnPathComplete, graphMaskOriginal);
	}

	private void OriginalOnPathComplete(Path _p)
	{
		if (backupMovementGraph != "")
		{
			if (_p.error)
			{
				BackupPathRequest();
				return;
			}
			if ((storeRequestedTargetPos - _p.vectorPath[_p.vectorPath.Count - 1]).magnitude > 0.1f)
			{
				BackupPathRequest();
				return;
			}
		}
		else if (_p.error)
		{
			return;
		}
		path = _p.vectorPath;
		nextPathPointIndex = 0;
	}

	private void BackupPathRequest()
	{
		seekToTargetPos = FindMoveToTarget();
		seekToTargetPosSnappedtoNavmesh = AstarPath.active.GetNearest(seekToTargetPos, nearestConstraint).position;
		storeRequestedTargetPos = seekToTargetPosSnappedtoNavmesh;
		seeker.StartPath(base.transform.position, seekToTargetPosSnappedtoNavmesh, BackupOnPathComplete, GraphMask.FromGraphName(backupMovementGraph));
	}

	private void BackupOnPathComplete(Path _p)
	{
		if (!_p.error)
		{
			path = _p.vectorPath;
			nextPathPointIndex = 0;
		}
	}

	private Vector3 FindMoveToTarget()
	{
		if (followingPlayer)
		{
			seekToTaggedObj = null;
			targetPrio = null;
			currentlyWalkingHome = true;
			return homePosition;
		}
		if (holdPosition)
		{
			seekToTaggedObj = null;
			targetPrio = null;
			currentlyWalkingHome = true;
			return homePosition;
		}
		currentlyWalkingHome = false;
		for (int i = 0; i < targetPriorities.Count; i++)
		{
			targetPrio = targetPriorities[i];
			seekToTaggedObj = targetPrio.FindTaggedObjectCloseToHome(base.transform.position, homePosition, maximumDistanceFromHome, out var _outPosition);
			if (!(seekToTaggedObj == null))
			{
				return _outPosition;
			}
		}
		seekToTaggedObj = null;
		targetPrio = null;
		currentlyWalkingHome = true;
		return homePosition;
	}

	private void Update()
	{
		recalculatePathCooldown -= Time.deltaTime;
		if (recalculatePathCooldown <= 0f)
		{
			recalculatePathCooldown = recalculatePathInterval;
			OriginalPathRequest();
		}
		if (followingPlayer)
		{
			if (path.Count > 0)
			{
				path[path.Count - 1] = homePosition;
			}
			seekToTargetPos = homePosition;
		}
		FollowPathUpdate();
	}

	public override void Slow(float _duration)
	{
		slowedFor = Mathf.Max(_duration, slowedFor);
	}

	private void FollowPathUpdate()
	{
		if (path.Count <= nextPathPointIndex)
		{
			if (!followingPlayer || path.Count <= 0)
			{
				return;
			}
			nextPathPointIndex = path.Count - 1;
		}
		nextPathPoint = path[nextPathPointIndex];
		Vector3 vector;
		if ((base.transform.position - seekToTargetPos).magnitude < keepDistanceOf && !currentlyWalkingHome)
		{
			vector = Vector3.zero;
			nextPathPoint = base.transform.position;
		}
		else
		{
			vector = nextPathPoint - base.transform.position;
			if (vector.magnitude <= 1f)
			{
				nextPathPointIndex++;
				nextPathPointIndex = Mathf.Clamp(nextPathPointIndex, 0, path.Count - 1);
				nextPathPoint = path[nextPathPointIndex];
				vector = nextPathPoint - base.transform.position;
			}
		}
		rvo.priority = vector.magnitude;
		float num = (day ? (movementSpeed * speedBoostDuringDaytime) : movementSpeed);
		if (slowedFor > 0f)
		{
			rvo.SetTarget(nextPathPoint, vector.magnitude * 3f, num * 0.33f);
			slowedFor -= Time.deltaTime;
		}
		else
		{
			rvo.SetTarget(nextPathPoint, vector.magnitude * 3f, num);
		}
		Vector3 position = base.transform.position + rvo.CalculateMovementDelta(Time.deltaTime);
		Vector3 position2 = AstarPath.active.GetNearest(position, nearestConstraint).position;
		base.transform.position = position2;
	}

	public void SnapToNavmesh()
	{
		base.transform.position = AstarPath.active.GetNearest(base.transform.position, nearestConstraint).position;
	}

	public override void ClearCurrentPath()
	{
		path.Clear();
	}
}