summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Decompile/BuildSlot.cs
blob: 20fadac5943dca25a9feaff644d860ef0a6ffd87 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
using System;
using System.Collections.Generic;
using I2.Loc;
using Pathfinding;
using UnityEngine;
using UnityEngine.Events;

[SelectionBase]
public class BuildSlot : MonoBehaviour, DayNightCycle.IDaytimeSensitive
{
	public enum BuildingState
	{
		Blueprint,
		Built
	}

	[Serializable]
	public class Upgrade
	{
		public string upgradeTooltip;

		[Min(0f)]
		public int cost = 3;

		public List<UpgradeBranch> upgradeBranches;
	}

	[Serializable]
	public class UpgradeBranch
	{
		[Tooltip("Only necessary when there are multiple options:")]
		public Choice choiceDetails;

		public Mesh replacementMesh;

		public int goldIncomeChange;

		public int hpChange;

		public List<GameObject> objectsToActivate = new List<GameObject>();

		public List<GameObject> objectsToDisable = new List<GameObject>();
	}

	public string buildingName;

	[Tooltip("The root is the required parent object no arrow points to, so this is pretty much always gonna be the casle center.E.g. this is set to 0 this building can not have a higher level than the castle center level.")]
	[SerializeField]
	private int requiredRootLevelDifference = -100;

	private BuildSlot requiredRoot;

	private List<BuildSlot> isRootOf = new List<BuildSlot>();

	[SerializeField]
	private bool startDeactivated = true;

	[SerializeField]
	private BuildSlot activatorBuilding;

	[Min(0f)]
	[SerializeField]
	private int activatorLevel;

	[SerializeField]
	private bool activatorUpgradesThis;

	public List<Upgrade> upgrades = new List<Upgrade>();

	private int level;

	[SerializeField]
	private GameObject buildingParent;

	[SerializeField]
	private GameObject bluepringParent;

	[SerializeField]
	private BuildingInteractor interactor;

	[SerializeField]
	private MeshFilter mainMesh;

	[SerializeField]
	private BoxCollider mainCollider;

	[SerializeField]
	private BoxCollider damageCollider;

	[SerializeField]
	private BoxCollider interactorCollider;

	[SerializeField]
	private NavmeshCut navmeshCut;

	public bool hideGizmos;

	public float navmeshCutPadding = 1f;

	public BuildingMeshTracker buildingMeshTracker;

	[HideInInspector]
	public BuildingInteractor buildingInteractor;

	private int goldIncome;

	[HideInInspector]
	public UnityEvent OnUpgrade = new UnityEvent();

	[HideInInspector]
	public UnityEvent OnParentUpgrade = new UnityEvent();

	[HideInInspector]
	public UnityEvent OnUpgradeCancel = new UnityEvent();

	private List<BuildSlot> builtSlotsThatRelyOnThisBuilding = new List<BuildSlot>();

	private bool gotActivated;

	private Upgrade upgradeSelected;

	private PlayerInteraction playerInteractionSelected;

	public string LOCIDENTIFIER_NAME => "Building/" + buildingName;

	public List<BuildSlot> IsRootOf => isRootOf;

	public BuildSlot ActivatorBuilding => activatorBuilding;

	public int ActivatorLevel
	{
		get
		{
			return activatorLevel;
		}
		set
		{
			activatorLevel = value;
		}
	}

	public bool ActivatorUpgradesThis => activatorUpgradesThis;

	public List<Upgrade> Upgrades
	{
		get
		{
			if (!activatorUpgradesThis)
			{
				return upgrades;
			}
			return activatorBuilding.Upgrades;
		}
	}

	public int Level
	{
		get
		{
			if (!activatorUpgradesThis)
			{
				return level;
			}
			return activatorBuilding.Level;
		}
	}

	public BuildingInteractor Interactor => interactor;

	public MeshFilter MainMesh => mainMesh;

	public BuildingState State
	{
		get
		{
			if (level < 1)
			{
				return BuildingState.Blueprint;
			}
			return BuildingState.Built;
		}
	}

	public int GoldIncome
	{
		get
		{
			return goldIncome;
		}
		set
		{
			goldIncome = value;
		}
	}

	public bool CanBeUpgraded
	{
		get
		{
			if (activatorUpgradesThis)
			{
				return activatorBuilding.CanBeUpgraded;
			}
			if (requiredRoot == null)
			{
				return level < upgrades.Count;
			}
			if (level < upgrades.Count)
			{
				if (requiredRoot.Level <= level + requiredRootLevelDifference && level != 0)
				{
					return requiredRoot.Level >= 3;
				}
				return true;
			}
			return false;
		}
	}

	public bool NextUpgradeIsChoice
	{
		get
		{
			if (CanBeUpgraded)
			{
				return upgrades[level].upgradeBranches.Count > 1;
			}
			return false;
		}
	}

	public int NextUpgradeOrBuildCost
	{
		get
		{
			if (activatorUpgradesThis)
			{
				return activatorBuilding.NextUpgradeOrBuildCost;
			}
			if (CanBeUpgraded)
			{
				return upgrades[level].cost;
			}
			return 100;
		}
		set
		{
			if (activatorUpgradesThis)
			{
				activatorBuilding.NextUpgradeOrBuildCost = value;
			}
			if (CanBeUpgraded)
			{
				upgrades[level].cost = value;
			}
		}
	}

	public GameObject BuildingParent => buildingParent;

	private bool IsBlueprint => level == 0;

	public List<BuildSlot> BuiltSlotsThatRelyOnThisBuilding => builtSlotsThatRelyOnThisBuilding;

	public string GET_LOCIDENTIFIER_UPGRADE(int upgradeNumber)
	{
		return LOCIDENTIFIER_NAME + " Upgrade " + upgradeNumber;
	}

	public string GET_LOCIDENTIFIER_CHOICENAME(Choice choice)
	{
		return LOCIDENTIFIER_NAME + " Choice " + choice.name;
	}

	public string GET_LOCIDENTIFIER_CHOICEDESCRIPTION(Choice choice)
	{
		return GET_LOCIDENTIFIER_CHOICENAME(choice) + " Description";
	}

	public string ReturnTooltip()
	{
		if (!CanBeUpgraded)
		{
			return "";
		}
		string text = ((level <= 0) ? LocalizationManager.GetTranslation("Tooltip/Build") : LocalizationManager.GetTranslation("Tooltip/Upgrade Building"));
		string text2 = "<style=\"Tooltip Header\">" + text + " " + LocalizationManager.GetTranslation(LOCIDENTIFIER_NAME) + ":\n";
		text2 = text2 + "<style=\"Tooltip Default\">" + LocalizationManager.GetTranslation(GET_LOCIDENTIFIER_UPGRADE(level)) + "\n";
		text2 += "<style=\"Tooltip Numerals\">";
		int num = 0;
		Hp componentInChildren = GetComponentInChildren<Hp>(includeInactive: true);
		if ((bool)componentInChildren)
		{
			num = (int)componentInChildren.maxHp;
		}
		List<int> list = new List<int>();
		List<int> list2 = new List<int>();
		foreach (UpgradeBranch upgradeBranch in upgrades[level].upgradeBranches)
		{
			int item = goldIncome + upgradeBranch.goldIncomeChange;
			if (!list.Contains(item))
			{
				list.Add(item);
			}
			int item2 = num + upgradeBranch.hpChange;
			if (!list2.Contains(item2))
			{
				list2.Add(item2);
			}
		}
		list2.Sort();
		list.Sort();
		string text3 = "";
		string text4 = "";
		for (int i = 0; i < list.Count; i++)
		{
			if (i > 0)
			{
				text3 += "/";
			}
			text3 += list[i];
		}
		for (int j = 0; j < list2.Count; j++)
		{
			if (j > 0)
			{
				text4 += "/";
			}
			text4 += list2[j];
		}
		if (level > 0)
		{
			text2 = ((list2.Count <= 1 && list2[0] == num) ? (text2 + "\n" + num + "<sprite name=\"heart\">") : (text2 + "\n" + num + "<sprite name=\"heart\"> <sprite name=\"arrow_right\"> " + text4 + "<sprite name=\"heart\">"));
			if (list.Count > 1 || list[0] != goldIncome)
			{
				return text2 + "\n" + goldIncome + "<sprite name=\"coin\"> <sprite name=\"arrow_right\"> " + text3 + "<sprite name=\"coin\">";
			}
			return text2 + "\n" + goldIncome + "<sprite name=\"coin\">";
		}
		text2 = ((list2.Count <= 1 && list2[0] == num) ? (text2 + "\n<sprite name=\"arrow_right\"> " + num + "<sprite name=\"heart\">") : (text2 + "\n<sprite name=\"arrow_right\"> " + text4 + "<sprite name=\"heart\">"));
		if (list.Count > 1 || list[0] != goldIncome)
		{
			return text2 + "\n<sprite name=\"arrow_right\"> " + text3 + "<sprite name=\"coin\">";
		}
		return text2 + "\n<sprite name=\"arrow_right\"> " + goldIncome + "<sprite name=\"coin\">";
	}

	private void OnEnable()
	{
		foreach (Upgrade upgrade in upgrades)
		{
			foreach (UpgradeBranch upgradeBranch in upgrade.upgradeBranches)
			{
				foreach (GameObject item in upgradeBranch.objectsToActivate)
				{
					item.SetActive(value: false);
				}
			}
		}
	}

	public List<BuildSlot> GetBuildSlotsThatWillUnlockWhenUpgraded()
	{
		List<BuildSlot> list = new List<BuildSlot>();
		for (int i = 0; i < builtSlotsThatRelyOnThisBuilding.Count; i++)
		{
			if (builtSlotsThatRelyOnThisBuilding[i].activatorLevel == level)
			{
				list.Add(builtSlotsThatRelyOnThisBuilding[i]);
			}
		}
		return list;
	}

	public List<MeshFilter> GetBlueprintPreviewsThatWillUnlockWhenUpgraded()
	{
		List<MeshFilter> list = new List<MeshFilter>();
		for (int i = 0; i < builtSlotsThatRelyOnThisBuilding.Count; i++)
		{
			if (builtSlotsThatRelyOnThisBuilding[i].activatorLevel == level)
			{
				if (builtSlotsThatRelyOnThisBuilding[i].State == BuildingState.Built || builtSlotsThatRelyOnThisBuilding[i].activatorUpgradesThis)
				{
					MeshFilter[] componentsInChildren = builtSlotsThatRelyOnThisBuilding[i].buildingParent.GetComponentsInChildren<MeshFilter>();
					list.AddRange(componentsInChildren);
				}
				else
				{
					MeshFilter[] componentsInChildren2 = builtSlotsThatRelyOnThisBuilding[i].bluepringParent.GetComponentsInChildren<MeshFilter>();
					list.AddRange(componentsInChildren2);
				}
			}
		}
		return list;
	}

	public List<GameObject> GetGameObjectsThatWillUnlockWhenUpgraded(int _upgradeBranch)
	{
		if (level >= upgrades.Count)
		{
			return new List<GameObject>();
		}
		return upgrades[level].upgradeBranches[_upgradeBranch % upgrades[level].upgradeBranches.Count].objectsToActivate;
	}

	private void Start()
	{
		requiredRoot = this;
		while (requiredRoot.activatorBuilding != null)
		{
			requiredRoot = requiredRoot.activatorBuilding;
		}
		if (requiredRoot != this)
		{
			requiredRoot.isRootOf.Add(this);
		}
		if ((bool)activatorBuilding)
		{
			activatorBuilding.builtSlotsThatRelyOnThisBuilding.Add(this);
		}
		if (startDeactivated)
		{
			base.gameObject.SetActive(value: false);
			if ((bool)activatorBuilding)
			{
				activatorBuilding.OnUpgrade.AddListener(Activate);
			}
		}
		else
		{
			Activate();
		}
		DayNightCycle.Instance.RegisterDaytimeSensitiveObject(this);
	}

	public void Activate()
	{
		if (!gotActivated && (!startDeactivated || activatorBuilding.Level > activatorLevel))
		{
			gotActivated = true;
			base.gameObject.SetActive(value: true);
			ActivateOrDeactivateBuliding(State);
		}
	}

	private void ActivateOrDeactivateBuliding(BuildingState nextState)
	{
		switch (nextState)
		{
		case BuildingState.Blueprint:
			buildingParent.SetActive(value: false);
			bluepringParent.SetActive(value: true);
			break;
		case BuildingState.Built:
			buildingParent.SetActive(value: true);
			bluepringParent.SetActive(value: false);
			break;
		}
	}

	public void TryToBuildOrUpgradeAndPay(PlayerInteraction player, bool _presentChoice = true)
	{
		if (!CanBeUpgraded)
		{
			return;
		}
		if (activatorUpgradesThis)
		{
			activatorBuilding.TryToBuildOrUpgradeAndPay(player, _presentChoice);
			return;
		}
		for (int i = 0; i < builtSlotsThatRelyOnThisBuilding.Count; i++)
		{
			BuildSlot buildSlot = builtSlotsThatRelyOnThisBuilding[i];
			if (buildSlot.ActivatorUpgradesThis && buildSlot.activatorBuilding == this)
			{
				buildSlot.gameObject.SetActive(value: true);
				buildSlot.ExecuteBuildOrUpgrade(player, _presentChoice: false);
			}
		}
		ExecuteBuildOrUpgrade(player, _presentChoice);
		for (int j = 0; j < builtSlotsThatRelyOnThisBuilding.Count; j++)
		{
			BuildSlot buildSlot2 = builtSlotsThatRelyOnThisBuilding[j];
			if ((bool)buildSlot2.buildingInteractor)
			{
				buildSlot2.buildingInteractor.UpdateInteractionState();
			}
		}
		if ((bool)buildingInteractor)
		{
			buildingInteractor.UpdateInteractionState();
		}
	}

	public void ExecuteBuildOrUpgrade(PlayerInteraction _player, bool _presentChoice)
	{
		if (CanBeUpgraded)
		{
			ExecuteUpgrade(upgrades[level], _player, _presentChoice);
		}
	}

	private void ExecuteUpgrade(Upgrade _upg, PlayerInteraction _player, bool _presentChoice)
	{
		if (ChoiceManager.instance.ChoiceCoroutineRunning)
		{
			return;
		}
		List<Choice> list = new List<Choice>();
		foreach (UpgradeBranch upgradeBranch in _upg.upgradeBranches)
		{
			list.Add(upgradeBranch.choiceDetails);
		}
		upgradeSelected = _upg;
		playerInteractionSelected = _player;
		if (_presentChoice)
		{
			ChoiceManager.instance.PresentChoices(list, this, OnUpgradeChoiceComplete);
		}
		else
		{
			OnUpgradeChoiceComplete(_upg.upgradeBranches[UnityEngine.Random.Range(0, _upg.upgradeBranches.Count)].choiceDetails);
		}
	}

	public void OnUpgradeChoiceComplete(Choice _choiceMade)
	{
		if (_choiceMade == null)
		{
			OnUpgradeCancel.Invoke();
			return;
		}
		if (level == 0)
		{
			ActivateOrDeactivateBuliding(BuildingState.Built);
		}
		level++;
		UpgradeBranch upgradeBranch = null;
		foreach (UpgradeBranch upgradeBranch2 in upgradeSelected.upgradeBranches)
		{
			if (upgradeBranch2.choiceDetails == _choiceMade)
			{
				upgradeBranch = upgradeBranch2;
			}
		}
		buildingMeshTracker.Unfreeze();
		buildingMeshTracker.FreezeMeshWithDelay();
		if (upgradeBranch.replacementMesh != null)
		{
			mainMesh.mesh = upgradeBranch.replacementMesh;
		}
		goldIncome += upgradeBranch.goldIncomeChange;
		Hp component = buildingParent.GetComponent<Hp>();
		if ((bool)component)
		{
			component.maxHp += upgradeBranch.hpChange;
			component.Heal(upgradeBranch.hpChange);
		}
		foreach (GameObject item in upgradeBranch.objectsToActivate)
		{
			item.SetActive(value: true);
		}
		foreach (GameObject item2 in upgradeBranch.objectsToDisable)
		{
			item2.SetActive(value: false);
		}
		foreach (BuildSlot item3 in isRootOf)
		{
			BuildingInteractor[] componentsInChildren = item3.GetComponentsInChildren<BuildingInteractor>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].UpdateInteractionState();
			}
		}
		if (activatorUpgradesThis)
		{
			OnParentUpgrade.Invoke();
			return;
		}
		OnUpgrade.Invoke();
		TooltipManager.instance.SetInteractorRefreshFlag();
	}

	public void DEBUGUpgradeToMax()
	{
		while (CanBeUpgraded)
		{
			TryToBuildOrUpgradeAndPay(null, _presentChoice: false);
		}
		if ((bool)interactor)
		{
			interactor.UpdateInteractionState();
		}
	}

	public void OnDusk()
	{
		if (IsBlueprint)
		{
			bluepringParent.SetActive(value: false);
		}
	}

	public void OnDawn_AfterSunrise()
	{
		if (IsBlueprint)
		{
			bluepringParent.SetActive(value: true);
		}
	}

	public void OnDawn_BeforeSunrise()
	{
	}
}