Unit World UI
@ -1,15 +1,25 @@
|
|||||||
using System;
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Serialization;
|
||||||
|
|
||||||
public class HealthSystem : MonoBehaviour {
|
public class HealthSystem : MonoBehaviour {
|
||||||
[SerializeField] private int health = 100;
|
|
||||||
|
private int health;
|
||||||
|
[SerializeField] private int maxHealth = 100;
|
||||||
|
public float HealthNormalized => (float)health / maxHealth;
|
||||||
|
|
||||||
public event EventHandler OnDead;
|
public event EventHandler OnDead;
|
||||||
|
public event EventHandler OnDamage;
|
||||||
|
|
||||||
|
private void Awake() => health = maxHealth;
|
||||||
|
|
||||||
public void Damage(int damageAmount) {
|
public void Damage(int damageAmount) {
|
||||||
health -= damageAmount;
|
health -= damageAmount;
|
||||||
if (health < 0) health = 0;
|
if (health < 0) health = 0;
|
||||||
if (health == 0) Die();
|
if (health == 0) Die();
|
||||||
Debug.Log($"{gameObject.name} {health}");
|
OnDamage?.Invoke(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Die() => OnDead?.Invoke(this, EventArgs.Empty);
|
private void Die() => OnDead?.Invoke(this, EventArgs.Empty);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,19 +1,28 @@
|
|||||||
using System;
|
using System;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
namespace UI {
|
namespace UI {
|
||||||
public class UnitWorldUI : MonoBehaviour {
|
public class UnitWorldUI : MonoBehaviour {
|
||||||
[SerializeField] private TextMeshProUGUI actionPointsText;
|
[SerializeField] private TextMeshProUGUI actionPointsText;
|
||||||
[SerializeField] private Unit unit;
|
[SerializeField] private Unit unit;
|
||||||
|
[SerializeField] private Image healthBarImage;
|
||||||
|
[SerializeField] private HealthSystem healthSystem;
|
||||||
|
|
||||||
private void Start() {
|
private void Start() {
|
||||||
Unit.OnAnyActionPointsChanged += Unit_OnAnyActionPointsChanged;
|
Unit.OnAnyActionPointsChanged += Unit_OnAnyActionPointsChanged;
|
||||||
|
healthSystem.OnDamage += HealthSystem_OnDamage;
|
||||||
UpdateActionPointsText();
|
UpdateActionPointsText();
|
||||||
|
UpdateHealthBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void HealthSystem_OnDamage(object sender, EventArgs e) => UpdateHealthBar();
|
||||||
|
|
||||||
private void Unit_OnAnyActionPointsChanged(object sender, EventArgs e) => UpdateActionPointsText();
|
private void Unit_OnAnyActionPointsChanged(object sender, EventArgs e) => UpdateActionPointsText();
|
||||||
|
|
||||||
private void UpdateActionPointsText() => actionPointsText.text = unit.ActionPoints.ToString();
|
private void UpdateActionPointsText() => actionPointsText.text = unit.ActionPoints.ToString();
|
||||||
|
|
||||||
|
private void UpdateHealthBar() => healthBarImage.fillAmount = healthSystem.HealthNormalized;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 779 B |
|
After Width: | Height: | Size: 816 B |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 646 B |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 358 B |