Health system + Water Spirit AI!

This commit is contained in:
MindfulMinun committed 2022-02-25 13:12:42 -06:00
1 parent f3985d657e
commit 827588bb18
7 files changed
+281 -33

No files matched your search

+15 -14
View File
@@ -1,40 +1,41 @@
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
// @MindfulMinun
public class Health : MonoBehaviour {
public int startingHealth = 3;
public float damageTimeout = 1f;
public int currentHealth;
public bool isInvincible;
public bool isDead;
public Animator healthBarAnimator;
public UnityEvent OnDead;
public UnityEvent OnHit;
public UnityEvent OnDead = new UnityEvent();
public UnityEvent OnHit = new UnityEvent();
void Start() {
OnDead = new UnityEvent();
OnHit = new UnityEvent();
if (healthBarAnimator == null) {
Debug.Log("HealthBarAnimator not found! Please attach the Health bar's animator to the Health script so the health bar can be animated.");
}
currentHealth = startingHealth;
}
void Update() {
if (Input.GetKeyDown(KeyCode.E)) {
takeHit();
OnHit.Invoke();
}
if (healthBarAnimator != null) {
healthBarAnimator.SetInteger("Health", currentHealth);
}
}
void takeHit() {
public void takeHit() {
if (isInvincible) return;
currentHealth = Mathf.Clamp(currentHealth - 1, 0, startingHealth);
isDead = currentHealth <= 0;
OnHit.Invoke();
if (isDead) OnDead.Invoke();
StartCoroutine(damageTimer());
}
// Waits for a second before allowing the player to take damage again
private IEnumerator damageTimer() {
bool wasInvincible = isInvincible;
isInvincible = true;
yield return new WaitForSeconds(damageTimeout);
isInvincible = wasInvincible;
}
}