- using System.Collections;
- using System.Collections.Generic;
- //using UnityEditor.PackageManager;
- using UnityEngine;
- using UnityEngine.Events;
- public class HealthSystem : MonoBehaviour
- {
- private StatusEffects statusEffects;
- private float hp;
- private bool dead = false;
- [SerializeField]
- private List<string> bannedTags;
- [SerializeField]
- private UnityEvent OnDamageTaken;
- [SerializeField]
- private UnityEvent OnRegenerate;
- [SerializeField]
- private UnityEvent OnDeath;
- private UpdateManager updateManager;
- private IUpdatable[] updatables;
- public bool immortal = false;
- void Start()
- {
- statusEffects = GetComponent<StatusEffects>();
- updatables = GetComponents<IUpdatable>();
- if (statusEffects == null)
- {
- Debug.LogWarning("No status efffect script in this object!");
- return;
- }
- hp = statusEffects.maxHp;
- GameObject t = GameObject.Find("UpdateManager");
- if (t)
- {
- updateManager = t.GetComponent<UpdateManager>();
- }
-
- }
- public float GetHealth() { return hp; }
- public void EditHealth(float value)
- {
- hp = value;
- }
- public void TakeDamage(float value, GameObject source)
- {
- if(value < 0)
- {
- Debug.LogError("Take damage only accepts positive values");
- return;
- }
- if (immortal) return;
- foreach(string t in bannedTags)
- {
- if (source)
- {
- if (t == source.tag) return;
- }
- }
- if(statusEffects.shield > 0)
- {
- if(statusEffects.shield >= value )
- statusEffects.shield -= value;
- else
- {
- hp += statusEffects.shield - value;
- statusEffects.shield = 0;
- }
- }
- else { hp -= value; }
- if(hp <= 0)
- {
- Die();
- }
- OnDamageTaken.Invoke();
- }
- public void Die()
- {
- dead = true;
- //Debug.Log("The player went to sleep... Forever.");
- OnDeath.Invoke();
- }
- public void Destroy()
- {
- if (updateManager != null)
- {
- foreach (IUpdatable i in updatables)
- {
- updateManager.RemoveFromList(i);
- }
- }
- Destroy(gameObject);
- }
- public void Regenerate(float value)
- {
- if (value < 0)
- {
- Debug.LogError("Regenerate only accepts positive values");
- return;
- }
- hp += value;
- OnRegenerate.Invoke();
- }
- public float GetHealthPercentage()
- {
- return (hp / statusEffects.maxHp);
- }
- public float GetMaxHealth()
- {
- return statusEffects.maxHp;
- }
- }