HP Bar shows right percentage

This commit is contained in:
henriquenakashima 2020-09-17 23:14:34 -04:00
parent 6405508376
commit ea5108b0f5
4 changed files with 17 additions and 10 deletions

View file

@ -35,8 +35,7 @@ public class BattleController : MonoBehaviour
heroSprites[i].transform.position = new Vector3(-6 + 6 * i, 0, 0);
heroUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI));
heroUIs[i].target = heroSprites[i];
heroUIs[i].battleParticipant = heroes[i];
heroUIs[i].Init(heroSprites[i], heroes[i]);
}
monsters = new BattleMonster[3];
@ -53,8 +52,7 @@ public class BattleController : MonoBehaviour
monsterSprites[i].transform.position = new Vector3(-6 + 6 * i, 3, 0);
monsterUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI));
monsterUIs[i].target = monsterSprites[i];
monsterUIs[i].battleParticipant = monsters[i];
monsterUIs[i].Init(monsterSprites[i], monsters[i]);
}
}

View file

@ -16,7 +16,7 @@ public class BattleParticipant
return this.hp;
}
public int geMaxHP() {
public int getMaxHP() {
return this.maxHP;
}

View file

@ -385,7 +385,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -490, y: -245.5}
m_AnchoredPosition: {x: -490, y: -279}
m_SizeDelta: {x: 160, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &527589755
@ -414,13 +414,13 @@ MonoBehaviour:
m_BestFit: 0
m_MinSize: 10
m_MaxSize: 40
m_Alignment: 0
m_Alignment: 3
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: New Text
m_Text: 'Energy left: 3/3'
--- !u!222 &527589756
CanvasRenderer:
m_ObjectHideFlags: 0

View file

@ -5,14 +5,19 @@ using UnityEngine.UI;
public class HeroInfoUI : MonoBehaviour
{
public GameObject target;
public BattleParticipant battleParticipant;
protected GameObject target;
protected BattleParticipant battleParticipant;
public Text nameText;
public Image healthBarBgImage;
public Image healthBarFgImage;
private Camera mainCamera;
public void Init(GameObject sprite, BattleParticipant battleParticipant) {
this.target = sprite;
this.battleParticipant = battleParticipant;
}
// Start is called before the first frame update
void Start()
{
@ -25,5 +30,9 @@ 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;
}
}