summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Decompile/Healthbar.cs
blob: e8e63e6b7a9cddd1549c5a611f340d7cb494995a (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
using MPUIKIT;
using UnityEngine;

public class Healthbar : MonoBehaviour
{
	[SerializeField]
	protected GameObject canvasParent;

	[SerializeField]
	protected MPImageBasic fill;

	[SerializeField]
	protected MPImageBasic delayedFill;

	[SerializeField]
	protected Hp target;

	[SerializeField]
	protected Color deathFillColor;

	[SerializeField]
	private bool flashWhenBelowThreshold;

	[SerializeField]
	[Range(0f, 1f)]
	private float flashThreshhold;

	[SerializeField]
	private float flashInterval = 0.25f;

	[SerializeField]
	private Color flashColor;

	[SerializeField]
	private MPImageBasic flashTarget;

	[SerializeField]
	private float damageFlashSize = 1.2f;

	protected Color defaultDelayedFillColor;

	protected float hpLast;

	private float hpNow;

	private float damageFlash;

	private bool shouldUnparentAndDestroyOnDeath;

	private Color flashDefaultColor;

	private float flashClock;

	private readonly float finalDrainSpeed = 0.5f;

	private readonly float maxDrainSpeed = 1f;

	private readonly float minDrainSpeed = 0.1f;

	private void Start()
	{
		target.OnHpChange.AddListener(UpdateDisplay);
		defaultDelayedFillColor = delayedFill.color;
		UpdateDisplay();
		delayedFill.fillAmount = fill.fillAmount;
		hpLast = 1f;
		shouldUnparentAndDestroyOnDeath = !target.getsKnockedOutInsteadOfDying;
		if (shouldUnparentAndDestroyOnDeath)
		{
			target.OnKillOrKnockout.AddListener(UnparentItself);
		}
		if (flashWhenBelowThreshold)
		{
			flashDefaultColor = flashTarget.color;
		}
	}

	private void Update()
	{
		hpNow = fill.fillAmount;
		bool flag = Mathf.Approximately(hpNow, 0f);
		if (!Mathf.Approximately(hpNow, delayedFill.fillAmount))
		{
			if (!flag)
			{
				delayedFill.fillAmount = Mathf.MoveTowards(delayedFill.fillAmount, fill.fillAmount, Mathf.Lerp(minDrainSpeed, maxDrainSpeed, delayedFill.fillAmount - fill.fillAmount) * Time.deltaTime);
			}
			else
			{
				delayedFill.fillAmount = Mathf.MoveTowards(delayedFill.fillAmount, fill.fillAmount, Mathf.Lerp(finalDrainSpeed, maxDrainSpeed, delayedFill.fillAmount - fill.fillAmount) * Time.deltaTime);
			}
		}
		if (hpNow < hpLast)
		{
			damageFlash = 2f;
		}
		if (flag && delayedFill.fillAmount < 0.01f)
		{
			if (shouldUnparentAndDestroyOnDeath)
			{
				Object.Destroy(base.gameObject);
			}
			else
			{
				canvasParent.SetActive(value: false);
				base.enabled = false;
			}
		}
		if (flashWhenBelowThreshold)
		{
			if (hpNow <= flashThreshhold && hpNow > 0f)
			{
				flashClock -= Time.deltaTime;
				if (flashClock <= 0f)
				{
					flashClock = flashInterval;
					if (flashTarget.color == flashColor)
					{
						flashTarget.color = flashDefaultColor;
					}
					else
					{
						flashTarget.color = flashColor;
					}
				}
			}
			else if (flashTarget.color != flashDefaultColor)
			{
				flashTarget.color = flashDefaultColor;
				flashClock = 0f;
			}
			else
			{
				flashClock = 0f;
			}
		}
		float num = Mathf.Lerp(1f, damageFlashSize, damageFlash);
		base.transform.localScale = new Vector3(num, num, num);
		damageFlash *= Mathf.Pow(0.1f, Time.deltaTime * 3f);
		hpLast = hpNow;
	}

	public virtual void UpdateDisplay()
	{
		if (target.HpPercentage == 1f)
		{
			canvasParent.SetActive(value: false);
			base.enabled = false;
			hpLast = 1f;
			delayedFill.fillAmount = hpLast;
		}
		else
		{
			canvasParent.SetActive(value: true);
			base.enabled = true;
		}
		fill.fillAmount = target.HpPercentage;
		if (Mathf.Approximately(fill.fillAmount, 0f))
		{
			delayedFill.color = deathFillColor;
		}
		else
		{
			delayedFill.color = defaultDelayedFillColor;
		}
	}

	private void UnparentItself()
	{
		base.transform.SetParent(null);
	}
}