summaryrefslogtreecommitdiff
path: root/GameCode/HealthBar.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2023-10-27 11:05:14 +0800
committerchai <215380520@qq.com>2023-10-27 11:05:14 +0800
commit766cdff5ffa72b65d7f106658d1603f47739b2ba (patch)
tree34d7799a94dfa9be182825577583c0fa6dc935f7 /GameCode/HealthBar.cs
+ init
Diffstat (limited to 'GameCode/HealthBar.cs')
-rw-r--r--GameCode/HealthBar.cs58
1 files changed, 58 insertions, 0 deletions
diff --git a/GameCode/HealthBar.cs b/GameCode/HealthBar.cs
new file mode 100644
index 0000000..98e08ec
--- /dev/null
+++ b/GameCode/HealthBar.cs
@@ -0,0 +1,58 @@
+using System;
+using UnityEngine;
+using UnityEngine.UI;
+
+public class HealthBar : MonoBehaviour
+{
+ public Image hp;
+
+ public Image white;
+
+ private float drag = 25f;
+
+ private float spring = 25f;
+
+ private float hpCur;
+
+ private float hpVel;
+
+ private float hpTarg;
+
+ private float whiteCur;
+
+ private float whiteVel;
+
+ private float whiteTarg;
+
+ private float sinceDamage;
+
+ private CharacterData data;
+
+ private void Start()
+ {
+ data = GetComponentInParent<CharacterData>();
+ CharacterStatModifiers componentInParent = GetComponentInParent<CharacterStatModifiers>();
+ componentInParent.WasDealtDamageAction = (Action<Vector2, bool>)Delegate.Combine(componentInParent.WasDealtDamageAction, new Action<Vector2, bool>(TakeDamage));
+ }
+
+ private void Update()
+ {
+ hpTarg = data.health / data.maxHealth;
+ sinceDamage += TimeHandler.deltaTime;
+ hpVel = FRILerp.Lerp(hpVel, (hpTarg - hpCur) * spring, drag);
+ whiteVel = FRILerp.Lerp(whiteVel, (whiteTarg - whiteCur) * spring, drag);
+ hpCur += hpVel * TimeHandler.deltaTime;
+ whiteCur += whiteVel * TimeHandler.deltaTime;
+ hp.fillAmount = hpCur;
+ white.fillAmount = whiteCur;
+ if (sinceDamage > 0.5f)
+ {
+ whiteTarg = hpTarg;
+ }
+ }
+
+ public void TakeDamage(Vector2 dmg, bool selfDmg)
+ {
+ sinceDamage = 0f;
+ }
+}