summaryrefslogtreecommitdiff
path: root/GameCode/HealthBar.cs
blob: 98e08ecd7116216e87483be113bba92d3d763949 (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
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;
	}
}