From 4e17aefb345c77cbf29f16b0d61d14f2e0b92cdf Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 22 Apr 2018 12:52:43 +0200 Subject: [PATCH] healthbar now calculates based on percentage of live --- Assets/Scripts/Entities/Mob.cs | 4 ++++ Assets/Scripts/HealthbarController.cs | 27 ++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Assets/Scripts/Entities/Mob.cs b/Assets/Scripts/Entities/Mob.cs index 61587f7..d73af56 100644 --- a/Assets/Scripts/Entities/Mob.cs +++ b/Assets/Scripts/Entities/Mob.cs @@ -48,4 +48,8 @@ public abstract class Mob : Entity { public int getHealth() { return currentHP; } + + public int getMaxHealth() { + return maxHP; + } } diff --git a/Assets/Scripts/HealthbarController.cs b/Assets/Scripts/HealthbarController.cs index 0220fd3..617788d 100644 --- a/Assets/Scripts/HealthbarController.cs +++ b/Assets/Scripts/HealthbarController.cs @@ -5,25 +5,38 @@ using UnityEngine; public class HealthbarController : MonoBehaviour { float currentRotation; + float maxRotation; private Player player; // Use this for initialization void Start () { - player = GameController.instance.GetPlayer(); - currentRotation = 100f; + UpdatePlayer(); } // Update is called once per frame void Update () { // if player alive and spawned if (player != null) { - gameObject.transform.Rotate(Vector3.forward, -(currentRotation - player.getHealth())); - currentRotation = player.getHealth(); + UpdatePointer(player.getHealth()); } else { //if player dead or not spawned - gameObject.transform.Rotate(Vector3.forward, -currentRotation); - currentRotation = 0f; - player = GameController.instance.GetPlayer(); + UpdatePointer(0); + UpdatePlayer(); + } } + + private void UpdatePointer(float playerLife) { + float offset = playerLife - currentRotation; + gameObject.transform.Rotate(Vector3.forward, offset); + currentRotation += offset; + } + + private void UpdatePlayer() { + player = GameController.instance.GetPlayer(); + if (player != null) { + maxRotation = player.getMaxHealth(); + currentRotation = player.getHealth(); + } + } }