summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Decompile/CommandUnits.cs
blob: d43986724a0512498df67fe896a06130ae3fb546 (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
using System.Collections.Generic;
using Pathfinding;
using Rewired;
using UnityEngine;

public class CommandUnits : MonoBehaviour
{
	public static CommandUnits instance;

	public UnitCommandRadiusAnimation rangeIndicator;

	public GameObject commandingIndicator;

	public ParticleSystem dropWaypointFx;

	public int drowWaypointParticleCount = 100;

	public float attractRange;

	public string graphNameOfPlayerUnits;

	public float unitDistanceFromEachOther = 2f;

	public float unitDistanceMoveStep = 0.5f;

	public int maxPositioningRepeats = 5;

	public float holdToHoldPositionTime = 1f;

	private ThronefallAudioManager audioManager;

	private AudioSet audioSet;

	private NNConstraint nearestConstraint = new NNConstraint();

	private List<PathfindMovementPlayerunit> playerUnitsCommanding = new List<PathfindMovementPlayerunit>();

	private List<PathfindMovementPlayerunit> playerUnitsCommandingBuffer = new List<PathfindMovementPlayerunit>();

	private Player input;

	[HideInInspector]
	public bool commanding;

	private TagManager tagManager;

	private AstarPath astarPath;

	private PlayerUpgradeManager playerUPgradeManager;

	private Hp hpPlayer;

	private float timeSincePlace;

	private bool switchedToHold;

	private List<AutoAttack> autoAttacksToEnable = new List<AutoAttack>();

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		audioManager = ThronefallAudioManager.Instance;
		audioSet = audioManager.audioContent;
		input = ReInput.players.GetPlayer(0);
		tagManager = TagManager.instance;
		astarPath = AstarPath.active;
		nearestConstraint.graphMask = GraphMask.FromGraphName(graphNameOfPlayerUnits);
		playerUPgradeManager = PlayerUpgradeManager.instance;
		hpPlayer = GetComponent<Hp>();
	}

	private void Update()
	{
		if (!commanding)
		{
			if (input.GetButtonDown("Command Units"))
			{
				rangeIndicator.Activate();
			}
			if (input.GetButton("Command Units") && hpPlayer.HpValue > 0f)
			{
				foreach (TaggedObject playerUnit in TagManager.instance.PlayerUnits)
				{
					if (tagManager.MeasureDistanceToTaggedObject(playerUnit, base.transform.position) <= attractRange)
					{
						OnUnitAdd(playerUnit);
					}
				}
			}
			else if (playerUnitsCommanding.Count > 0)
			{
				commanding = true;
			}
		}
		else
		{
			if (input.GetButtonDown("Command Units") || hpPlayer.HpValue <= 0f)
			{
				PlaceCommandedUnitsAndCalculateTargetPositions();
				timeSincePlace = 0f;
				switchedToHold = false;
			}
			if (input.GetButton("Command Units") && hpPlayer.HpValue > 0f)
			{
				timeSincePlace += Time.deltaTime;
				if (timeSincePlace > holdToHoldPositionTime && !switchedToHold)
				{
					switchedToHold = true;
					MakeUnitsInBufferHoldPosition();
				}
			}
			if (input.GetButtonUp("Command Units") || hpPlayer.HpValue <= 0f)
			{
				commanding = false;
				timeSincePlace = 0f;
			}
		}
		for (int num = playerUnitsCommanding.Count - 1; num >= 0; num--)
		{
			PathfindMovementPlayerunit pathfindMovementPlayerunit = playerUnitsCommanding[num];
			pathfindMovementPlayerunit.HomePosition = base.transform.position;
			if (!pathfindMovementPlayerunit.enabled)
			{
				playerUnitsCommanding.RemoveAt(num);
				OnUnitRemove(pathfindMovementPlayerunit);
			}
		}
		if (playerUnitsCommanding.Count > 0 && !input.GetButton("Command Units"))
		{
			commandingIndicator.SetActive(value: true);
		}
		else
		{
			commandingIndicator.SetActive(value: false);
		}
		if (!input.GetButton("Command Units") && rangeIndicator.Active)
		{
			rangeIndicator.Deactivate();
		}
	}

	public void MakeUnitsInBufferHoldPosition()
	{
		if (playerUnitsCommandingBuffer.Count > 0)
		{
			audioManager.PlaySoundAsOneShot(audioSet.HoldPosition, 0.45f, 0.9f + Random.value * 0.2f, audioManager.mgSFX, 10);
		}
		foreach (PathfindMovementPlayerunit item in playerUnitsCommandingBuffer)
		{
			item.HoldPosition = true;
		}
	}

	public void ForceCommandingEnd()
	{
		if (commanding)
		{
			PlaceCommandedUnitsAndCalculateTargetPositions();
		}
	}

	public void PlaceCommandedUnitsAndCalculateTargetPositions()
	{
		if (!commanding)
		{
			return;
		}
		if (playerUnitsCommanding.Count > 0)
		{
			audioManager.PlaySoundAsOneShot(audioSet.PlaceCommandingUnits, 0.35f, 0.9f + Random.value * 0.2f, audioManager.mgSFX, 10);
		}
		dropWaypointFx.Emit(drowWaypointParticleCount);
		foreach (PathfindMovementPlayerunit item in playerUnitsCommanding)
		{
			OnUnitRemove(item);
		}
		foreach (AutoAttack item2 in autoAttacksToEnable)
		{
			if ((bool)item2 && item2.GetComponent<Hp>().HpValue > 0f)
			{
				item2.enabled = true;
			}
		}
		autoAttacksToEnable.Clear();
		for (int i = 0; i < playerUnitsCommanding.Count; i++)
		{
			Vector3 vector = Quaternion.AngleAxis((float)(i / playerUnitsCommanding.Count) * 360f, Vector3.up) * Vector3.right * unitDistanceMoveStep;
			playerUnitsCommanding[i].HomePosition = astarPath.GetNearest(base.transform.position + vector + new Vector3(Random.value - 0.5f, 0f, Random.value - 0.5f) * unitDistanceMoveStep * 0.1f, nearestConstraint).position;
		}
		for (int j = 0; j < maxPositioningRepeats; j++)
		{
			bool flag = false;
			for (int k = 0; k < playerUnitsCommanding.Count; k++)
			{
				for (int l = k + 1; l < playerUnitsCommanding.Count; l++)
				{
					if (!((playerUnitsCommanding[k].HomePosition - playerUnitsCommanding[l].HomePosition).magnitude > unitDistanceFromEachOther))
					{
						Vector3 vector2 = (playerUnitsCommanding[k].HomePosition - playerUnitsCommanding[l].HomePosition).normalized * unitDistanceMoveStep;
						playerUnitsCommanding[k].HomePosition = astarPath.GetNearest(playerUnitsCommanding[k].HomePosition + vector2, nearestConstraint).position;
						playerUnitsCommanding[l].HomePosition = astarPath.GetNearest(playerUnitsCommanding[l].HomePosition - vector2, nearestConstraint).position;
						flag = true;
					}
				}
			}
			if (!flag)
			{
				break;
			}
		}
		playerUnitsCommandingBuffer.Clear();
		playerUnitsCommandingBuffer.AddRange(playerUnitsCommanding);
		playerUnitsCommanding.Clear();
	}

	public void OnUnitAdd(TaggedObject _t)
	{
		PathfindMovementPlayerunit pathfindMovementPlayerunit = (PathfindMovementPlayerunit)_t.Hp.PathfindMovement;
		if (!playerUnitsCommanding.Contains(pathfindMovementPlayerunit))
		{
			audioManager.PlaySoundAsOneShot(audioSet.AddedUnitToCommanding, 0.55f, 0.7f + (float)playerUnitsCommanding.Count * 0.025f, audioManager.mgSFX, 50);
			playerUnitsCommanding.Add(pathfindMovementPlayerunit);
			pathfindMovementPlayerunit.FollowPlayer(_follow: true);
			MaterialFlasherFX componentInChildren = pathfindMovementPlayerunit.GetComponentInChildren<MaterialFlasherFX>();
			if ((bool)componentInChildren)
			{
				componentInChildren.SetSelected(_selected: true);
			}
			_t.Tags.Add(TagManager.ETag.AUTO_Commanded);
			if (playerUPgradeManager.commander)
			{
				pathfindMovementPlayerunit.movementSpeed *= UpgradeCommander.instance.moveSpeedMultiplicator;
			}
			AutoAttack[] components = _t.GetComponents<AutoAttack>();
			foreach (AutoAttack autoAttack in components)
			{
				autoAttack.enabled = false;
				autoAttacksToEnable.Add(autoAttack);
			}
		}
	}

	public void OnUnitRemove(PathfindMovementPlayerunit _p)
	{
		_p.FollowPlayer(_follow: false);
		MaterialFlasherFX componentInChildren = _p.GetComponentInChildren<MaterialFlasherFX>();
		if ((bool)componentInChildren)
		{
			componentInChildren.SetSelected(_selected: false);
		}
		_p.GetComponent<TaggedObject>().Tags.Remove(TagManager.ETag.AUTO_Commanded);
		if (playerUPgradeManager.commander)
		{
			_p.movementSpeed /= UpgradeCommander.instance.moveSpeedMultiplicator;
		}
	}
}