Colored HP bar, HP text

This commit is contained in:
henriquenakashima 2020-09-17 23:23:58 -04:00
parent ea5108b0f5
commit 778228bc6b
2 changed files with 106 additions and 5 deletions

View file

@ -10,9 +10,16 @@ public class HeroInfoUI : MonoBehaviour
public Text nameText;
public Image healthBarBgImage;
public Image healthBarFgImage;
public Text hpText;
private int renderedHP = -1;
private int renderedMaxHP = -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;
@ -31,8 +38,23 @@ public class HeroInfoUI : MonoBehaviour
Vector3 targetPos = this.mainCamera.WorldToScreenPoint(target.transform.position);
transform.position = targetPos;
int hp = battleParticipant.getHP();
int maxHP = battleParticipant.getMaxHP();
healthBarFgImage.fillAmount = ((float) hp) / maxHP;
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;
}
}
}