Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / Health.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.Events;


public class Health : MonoBehaviour
{
    public float hp;

    public Vector3 deathPos;

    public float totalHealth;
    private float previousHealth;
    public int needles;
    private Text needleCounter;

    [SerializeField] private float timer;

    private Rigidbody rb;
    private float knockBackForce = 5;

    public bool immortality = false;

    public bool cheatImortality = false;
    [HideInInspector]
    public bool parry = false;

    [HideInInspector]
    public bool gotHit = false;

    [HideInInspector]
    public bool regenerating = false;

    public UnityEvent<float> onDamageTaken;
    public UnityEvent<float> onHealthUpdated;
    
    void Start()
    {
        totalHealth = hp;
        rb = GetComponent<Rigidbody>();
        if (gameObject.tag == "Player")
        {
                needleCounter = GameObject.FindGameObjectWithTag("NeedleCounter").GetComponent<Text>();
                needleCounter.text =  needles.ToString();
        }
     //   healthBar.fillAmount = hp / totalHealth;

    }
    private void Update()
    {
        if (parry)
        {
            if (timer > 0) timer -= Time.deltaTime;
            else { immortality = false; parry = false; timer = 1; }
        }
      //  if(regenerating) healthBar.fillAmount = hp / totalHealth;
        if (hp >= totalHealth) regenerating = false;
     //   healthBar.fillAmount = hp / totalHealth;

        if (previousHealth != hp)
        {
            onHealthUpdated.Invoke(hp- previousHealth);
            previousHealth = hp;
        }

    }

    public void AddHealth(float v)
    {
        hp = Mathf.Clamp(hp+v,0,totalHealth);
 
    }
    public void RestoreHealth()
    {
        hp = totalHealth;

    }

    public void LooseHp(float damage, Vector3 direction)
    {
        if (cheatImortality) { return; }
        if (!immortality)
        {
                 
            if (hp > damage)
            {
                hp -= damage;
                
                
                rb.AddForce(direction * knockBackForce, ForceMode.Impulse);
                gotHit = true;
                //healthBar.fillAmount = hp / totalHealth;
            }
            else
            {
                Die();
            }
            if (onDamageTaken != null)
            {
                onDamageTaken.Invoke(damage);
            }
        }
    }
    public void Die()
    {
        if (gameObject.tag != "Player")
        {
            Destroy(gameObject);
        }
        else {
            if (needles > 1)
            {
                needles--;
                needleCounter.text = "Needles:\n" + needles;
                hp = totalHealth;
                transform.position = deathPos;
            }
            else SceneManager.LoadScene(1);
        }
        
    }
    
}