-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomHealthBar.cs
More file actions
90 lines (77 loc) · 2.66 KB
/
Copy pathCustomHealthBar.cs
File metadata and controls
90 lines (77 loc) · 2.66 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using HarmonyLib;
using UnboundLib;
using System.Reflection.Emit;
using System.Reflection;
namespace ModsPlus
{
public class CustomHealthBar : MonoBehaviour
{
private HealthBar healthBar;
/// <summary>
/// Current health value, clamped to the range <c>[0, <see cref="MaxHealth"/>]</c>
/// </summary>
public float CurrentHealth { get => _currentHealth; set => SetCurrentHealth(value); }
private float _currentHealth = 100;
/// <summary>
/// Max health value, clamped to the range <c>[0, <see cref="float.PositiveInfinity"/>]</c>
/// </summary>
public float MaxHealth { get => _maxHealth; set => SetMaxHealth(value); }
private float _maxHealth = 100;
/// <summary>
/// Override the values of <c>CurrentHealth</c> and <c>MaxHealth</c>
/// </summary>
/// <param name="currentHealth"></param>
/// <param name="maxHealth"></param>
public void SetValues(float currentHealth, float maxHealth)
{
MaxHealth = maxHealth;
CurrentHealth = currentHealth;
}
private void Awake()
{
healthBar = Instantiate(Assets.BaseHealthBar.gameObject, transform).GetComponent<HealthBar>();
}
private void Start()
{
healthBar.transform.Find("Canvas/PlayerName").gameObject.SetActive(false);
}
private void SetCurrentHealth(float value)
{
if (_currentHealth == value) return;
_currentHealth = Math.Max(0, Math.Min(MaxHealth, value));
UpdateHealthBar();
}
private void SetMaxHealth(float value)
{
if (_maxHealth == value) return;
_maxHealth = Math.Max(0, value);
UpdateHealthBar();
}
private void UpdateHealthBar()
{
healthBar.TakeDamage(Vector2.zero, false);
}
/// <summary>
/// Returns the underlying <c>HealthBar</c> managed by this instance
/// </summary>
public HealthBar GetBaseHealthBar()
{
return healthBar;
}
/// <summary>
/// Sets the fill color of the bar
/// </summary>
/// <param name="color"></param>
public void SetColor(Color color)
{
GetBaseHealthBar().transform.Find("Canvas/Image/Health").GetComponent<Image>().color = color;
}
}
}