diff options
Diffstat (limited to 'Assets/Scripts/Player/PlayerHealthScript.cs')
-rwxr-xr-x | Assets/Scripts/Player/PlayerHealthScript.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/Assets/Scripts/Player/PlayerHealthScript.cs b/Assets/Scripts/Player/PlayerHealthScript.cs new file mode 100755 index 0000000..38e354a --- /dev/null +++ b/Assets/Scripts/Player/PlayerHealthScript.cs @@ -0,0 +1,66 @@ +using UnityEngine; +using System.Collections; +using UnityEngine.UI; +using UnityEngine.SceneManagement; + +public class PlayerHealthScript : MonoBehaviour { + + public float Health; + public float MaxHealth = 100; + bool Damaged; + + //HUD + GameObject HUD; + Text HealthText; + Text GameOverText; + Image DamageIndicator; + + void Start () + { + HUD = GameObject.FindGameObjectWithTag("HUD"); + HealthText = HUD.transform.Find("HealthText").GetComponent<Text>(); + GameOverText = HUD.transform.Find("GameOverText").GetComponent<Text>(); + DamageIndicator = HUD.transform.Find("DamageIndicatorImage").GetComponent<Image>(); + Health = 100; + HealthText.text = Health + "/100"; + } + + + void Update () + { + //Damage Animation + if (Damaged) + { + DamageIndicator.color = new Color(1, 0, 0, .1f); + } + else + { + DamageIndicator.color = Color.Lerp(DamageIndicator.color, Color.clear, 1f * Time.deltaTime); + } + Damaged = false; + } + + public void TakeDamage(float damage) + { + Health -= damage; + if (Health <= 0) Health = 0; + HealthText.text = Health + "/100"; + + Damaged = true; + + if (Health <= 0) + { + Health = 0; + gameObject.GetComponent<PlayerMovementScript>().enabled = false; + gameObject.GetComponentInChildren<PlayerShootingScript>().enabled = false; + StartCoroutine(Restart()); + } + } + + IEnumerator Restart() + { + GameOverText.text = "Game Over"; + yield return new WaitForSeconds(3f); + SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name); + } +} |