using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HeroInfoUI : MonoBehaviour { protected GameObject target; protected BattleParticipant battleParticipant; public Text nameText; public Image healthBarBgImage; public Image healthBarFgImage; public Text hpText; public GameObject blockGroup; public Text blockText; public StatusUI statusUI; private int renderedHP = -1; private int renderedMaxHP = -1; private int renderedBlock = -1; private Camera mainCamera; private static Color HIGH_HP_COLOR = new Color(0f, 0.8f, 0f); private static Color MED_HP_COLOR = new Color(0.8f, 0.8f, 0f); private static Color LOW_HP_COLOR = new Color(0.8f, 0f, 0f); public void Init(GameObject sprite, BattleParticipant battleParticipant) { this.target = sprite; this.battleParticipant = battleParticipant; this.statusUI.Init(battleParticipant); } // Start is called before the first frame update void Start() { this.mainCamera = Camera.main; this.nameText.text = this.battleParticipant.GetDisplayName(); } // Update is called once per frame void Update() { if (!battleParticipant.IsAlive()) { this.gameObject.SetActive(false); } Vector3 targetPos = this.mainCamera.WorldToScreenPoint(target.transform.position); transform.position = targetPos; int hp = this.battleParticipant.GetHP(); int maxHP = this.battleParticipant.GetMaxHP(); if (this.renderedHP != hp || this.renderedMaxHP != maxHP) { this.renderedHP = hp; this.renderedMaxHP = maxHP; float percentage = ((float) hp) / maxHP; this.healthBarFgImage.fillAmount = percentage; if (percentage >= 0.66) { this.healthBarFgImage.color = HIGH_HP_COLOR; } else if (percentage >= 0.33) { this.healthBarFgImage.color = MED_HP_COLOR; } else { this.healthBarFgImage.color = LOW_HP_COLOR; } this.hpText.text = "" + hp + " / " + maxHP; } int block = this.battleParticipant.GetBlock(); if (this.renderedBlock != block) { this.renderedBlock = block; if (block == 0) { this.blockGroup.SetActive(false); } else { this.blockGroup.SetActive(true); this.blockText.text = "" + block; } } } }