Files

70 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using Base;
using Entity;
using Managers;
using UnityEngine;
namespace UI
{
public class NanorobotsHPUI:MonoBehaviour,ITickUI
{
public BarUI hpBar;
public List<Entity.Entity> nanorobots;
public string nanorobotAffiliation;
private void Start()
{
EntityManager.Instance.OnCreateEntity += OnCreateEntity;
nanorobotAffiliation = Configs.ConfigManager.Instance.GetValue<string>("NanorobotsAffiliation");
}
private void OnDestroy()
{
EntityManager.Instance.OnCreateEntity -= OnCreateEntity;
}
private void OnEnable()
{
Clock.AddTickUI(this);
}
private void OnDisable()
{
Clock.RemoveTickUI(this);
}
public void TickUI()
{
float totalHP = 0;
float remainHP = 0;
foreach (var entity in nanorobots)
{
remainHP += entity.AttributesNow.health;
totalHP += entity.BaseAttributes.health;
}
if (totalHP == 0)
{
hpBar.Progress = 1;
}
else
{
hpBar.Progress = remainHP / totalHP;
}
}
private void OnCreateEntity(Entity.Entity entity)
{
if (entity.affiliation == nanorobotAffiliation)
{
nanorobots.Add(entity);
entity.OnEntityDiedEvent += OnDied;
}
}
private void OnDied(Entity.Entity entity)
{
nanorobots.Remove(entity);
}
}
}