Life.cs 1.01 KB
Newer Older
Unknown's avatar
Unknown committed
1 2 3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Isa's avatar
HUD  
Isa committed
4
using UnityEngine.Events;
Michael But's avatar
Michael But committed
5
using UnityEngine.SceneManagement;
Unknown's avatar
Unknown committed
6
using PlayerSpawnPointScript;
Unknown's avatar
Unknown committed
7 8 9 10 11 12

public class Life : MonoBehaviour {

    [SerializeField] private int m_MaxLife = 1;

    private int currentLife;
Isa's avatar
HUD  
Isa committed
13
    public UnityEvent LostLife;
Unknown's avatar
Unknown committed
14 15 16 17 18 19

    void Awake(){
        currentLife = m_MaxLife;
    }

    public void Damage(int amount){
Isa's avatar
HUD  
Isa committed
20
	    LostLife.Invoke();
Unknown's avatar
Unknown committed
21 22 23 24 25 26 27 28 29 30 31
        currentLife -= amount;
        if(currentLife <= 0){
            currentLife = 0;
            Death();
        }
    }

    public bool Heal(int amount){
        if(currentLife == m_MaxLife){
            return false;
        }else{
Isa's avatar
HUD  
Isa committed
32
	        LostLife.Invoke();
Unknown's avatar
Unknown committed
33 34 35 36 37 38
            Mathf.Clamp(currentLife += amount, 0, m_MaxLife);
            return true;
        }
    }

    public void Death(){
mthiele2's avatar
mthiele2 committed
39 40
		if (gameObject.CompareTag("Player"))
		{
Michael But's avatar
Michael But committed
41 42
			SceneManager.LoadScene("MainLevel");
			Heal(3);
Unknown's avatar
Unknown committed
43

mthiele2's avatar
mthiele2 committed
44 45 46
			return;
		}
		Destroy(gameObject);
Unknown's avatar
Unknown committed
47
    }
48 49 50 51 52 53

	public int getCurrentLife()
	{
		return currentLife;
	}
}