RiftMayhem/Assets/Scripts/UI/ResourceOrbUI.cs

128 lines
3.9 KiB
C#

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ResourceOrbUI : MonoBehaviour
{
[Header("Components:")]
[Header("Health:")]
[SerializeField] private Image healthFill;
[SerializeField] private TMP_Text health_TMP;
[Header("Mana:")]
[SerializeField] private Image manaFill;
[SerializeField] private TMP_Text mana_TMP;
[Header("SpiritPower:")]
[SerializeField] private Image spiritPowerFill;
[SerializeField] private TMP_Text spiritPower_TMP;
[Header("Absorb:")]
[SerializeField] private Image absorbFill;
[Header("Listeners:")]
[SerializeField] private GameEventListener_PhotonView onPlayerSpawned;
Health health;
Mana mana;
SpiritPower spiritPower;
AbsorbEffectInstance absorb;
private void Awake()
{
onPlayerSpawned.Response.AddListener(DependancyInjection);
}
// Start is called before the first frame update
void Start()
{
}
private void DependancyInjection(PhotonView spawnedPlayer)
{
if (!spawnedPlayer.IsMine) return;
health = ((RiftPlayer)spawnedPlayer.Owner.TagObject).GetComponent<Health>();
mana = ((RiftPlayer)spawnedPlayer.Owner.TagObject).GetComponent<Mana>();
absorb = ((RiftPlayer)spawnedPlayer.Owner.TagObject).GetComponent<AbsorbEffectInstance>();
spiritPower = ((RiftPlayer)spawnedPlayer.Owner.TagObject).GetComponent<SpiritPower>();
health.onResourceChanged.AddListener(UpdateCurrentHealth);
health.onMaxHealthChanged.AddListener(UpdateMaxHealth);
health.onMaxHealthChanged.AddListener((x) => UpdateCurrentAbsorb());
mana.onResourceChanged.AddListener(UpdateCurrentMana);
mana.onMaxManaChanged.AddListener(UpdateMaxMana);
spiritPower.onResourceChanged.AddListener(UpdateCurrentSpiritPower);
spiritPower.onMaxSpiritPowerChanged.AddListener(UpdateMaxSpiritPower);
absorb.OnEffectStackAddedEvent.AddListener(UpdateCurrentAbsorb);
absorb.OnAbsorbDamage.AddListener(UpdateCurrentAbsorb);
absorb.OnEffectEnded.AddListener(UpdateCurrentAbsorb);
UpdateCurrentHealth(health.GetCurrentValue());
UpdateMaxHealth(health.GetMaxValue());
UpdateCurrentMana(mana.GetCurrentValue());
UpdateMaxMana(mana.GetMaxValue());
UpdateCurrentAbsorb();
}
public void UpdateCurrentHealth(float value)
{
healthFill.fillAmount = value / health.GetMaxValue();
UpdateHealthTMP();
}
public void UpdateMaxHealth(float value)
{
healthFill.fillAmount = health.GetCurrentValue() / value;
UpdateHealthTMP();
}
public void UpdateHealthTMP()
{
health_TMP.text = $"{health.GetCurrentValue().ToString("F0")}/{health.GetMaxValue().ToString("F0")}";
}
public void UpdateCurrentMana(float value)
{
manaFill.fillAmount = value / mana.GetMaxValue();
UpdateManaTMP();
}
public void UpdateMaxMana(float value)
{
manaFill.fillAmount = mana.GetCurrentValue() / value;
UpdateManaTMP();
}
public void UpdateCurrentSpiritPower(float value)
{
spiritPowerFill.fillAmount = value / spiritPower.GetMaxValue();
UpdateSpiritPowerTMP();
}
public void UpdateMaxSpiritPower(float value)
{
spiritPowerFill.fillAmount = spiritPower.GetCurrentValue() / value;
UpdateSpiritPowerTMP();
}
public void UpdateManaTMP()
{
mana_TMP.text = $"{mana.GetCurrentValue().ToString("F0")}/{mana.GetMaxValue().ToString("F0")}";
}
public void UpdateSpiritPowerTMP()
{
spiritPower_TMP.text = $"{spiritPower.GetCurrentValue().ToString("F0")}/{spiritPower.GetMaxValue().ToString("F0")}";
}
public void UpdateCurrentAbsorb()
{
absorbFill.fillAmount = absorb.currentAmount / health.GetMaxValue();
}
}