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

public class UnitRespawnerForBuildings : MonoBehaviour
{
	public Hp hp;

	public List<Hp> units;

	public List<float> timeToRespawnAUnitDependingOnLevel = new List<float>();

	private float timeTillNextRespawn;

	public BuildSlot myBuildSlot;

	public TaggedObject taggedObject;

	public ProductionBar productionBar;

	[Header("Quick arrange units:")]
	public Transform copyUnitPositionsFrom;

	private bool gladiatorSchool;

	private bool elliteWarriors;

	private float gladiatorSchoolSpeed;

	private float elliteWarriorSpeed;

	private float totalTrainingSpeed = 1f;

	[SerializeField]
	private Equippable gladiatorSchoolPerk;

	[SerializeField]
	private Equippable elliteWarriorsPerk;

	private bool godOfDeathActive;

	private float ResetCooldownTime => timeToRespawnAUnitDependingOnLevel[Mathf.Clamp(myBuildSlot.Level, 0, timeToRespawnAUnitDependingOnLevel.Count - 1)];

	private void Start()
	{
		godOfDeathActive = PerkManager.instance.GodOfDeathActive;
		timeTillNextRespawn = ResetCooldownTime;
		productionBar.UpdateVisual(0f);
		gladiatorSchool = PerkManager.IsEquipped(gladiatorSchoolPerk);
		elliteWarriors = PerkManager.IsEquipped(elliteWarriorsPerk);
		gladiatorSchoolSpeed = PerkManager.instance.gladiatorSchool_TrainingSpeedMultiplyer;
		elliteWarriorSpeed = PerkManager.instance.elliteWarriors_TrainingSpeedMultiplyer;
		totalTrainingSpeed = 1f * (gladiatorSchool ? gladiatorSchoolSpeed : 1f) * (elliteWarriors ? elliteWarriorSpeed : 1f);
	}

	private void Update()
	{
		if (godOfDeathActive)
		{
			return;
		}
		if (hp.KnockedOut)
		{
			productionBar.UpdateVisual(0f);
		}
		else if (AtLeastOneUnitIsKnockedOut())
		{
			timeTillNextRespawn -= Time.deltaTime;
			productionBar.UpdateVisual(1f - timeTillNextRespawn / ResetCooldownTime * totalTrainingSpeed);
			if (timeTillNextRespawn <= 0f)
			{
				timeTillNextRespawn += ResetCooldownTime;
				timeTillNextRespawn /= totalTrainingSpeed;
				RespawnAKnockedOutUnit();
			}
		}
		else
		{
			timeTillNextRespawn = ResetCooldownTime;
			timeTillNextRespawn /= totalTrainingSpeed;
			productionBar.UpdateVisual(0f);
		}
	}

	private bool AtLeastOneUnitIsKnockedOut()
	{
		for (int i = 0; i < units.Count; i++)
		{
			Hp hp = units[i];
			if (hp.gameObject.activeInHierarchy && hp.KnockedOut)
			{
				return true;
			}
		}
		return false;
	}

	private void RespawnAKnockedOutUnit()
	{
		for (int i = 0; i < units.Count; i++)
		{
			Hp hp = units[i];
			if (hp.gameObject.activeInHierarchy && hp.KnockedOut)
			{
				hp.Revive();
				PathfindMovementPlayerunit component = hp.GetComponent<PathfindMovementPlayerunit>();
				Vector3 position = taggedObject.colliderForBigOjectsToMeasureDistance.ClosestPoint(component.HopePositionOriginal);
				hp.transform.position = position;
				component.SnapToNavmesh();
				break;
			}
		}
	}
}