From 766cdff5ffa72b65d7f106658d1603f47739b2ba Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 27 Oct 2023 11:05:14 +0800 Subject: + init --- GameCode/HealthBar.cs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 GameCode/HealthBar.cs (limited to 'GameCode/HealthBar.cs') 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(); + CharacterStatModifiers componentInParent = GetComponentInParent(); + componentInParent.WasDealtDamageAction = (Action)Delegate.Combine(componentInParent.WasDealtDamageAction, new Action(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; + } +} -- cgit v1.1-26-g67d0