mirror of
https://github.com/frizbee19/FebGameJam.git
synced 2026-11-11 04:22:40 +00:00
Health system + Water Spirit AI!
This commit is contained in:
7 files changed
+281
-33
No files matched your search
+15
-14
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user