Life.cs 972 Bytes
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;
Unknown's avatar
Unknown committed
5
6
7
8
9
10

public class Life : MonoBehaviour {

    [SerializeField] private int m_MaxLife = 1;

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

    void Awake(){
        currentLife = m_MaxLife;
    }

    public void Damage(int amount){
Isa's avatar
HUD    
Isa committed
18
	    LostLife.Invoke();
Unknown's avatar
Unknown committed
19
20
21
22
23
24
25
26
27
28
29
        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
30
	        LostLife.Invoke();
Unknown's avatar
Unknown committed
31
32
33
34
35
36
            Mathf.Clamp(currentLife += amount, 0, m_MaxLife);
            return true;
        }
    }

    public void Death(){
mthiele2's avatar
mthiele2 committed
37
38
39
40
41
42
43
		if (gameObject.CompareTag("Player"))
		{
			gameObject.SetActive(false);
			Debug.Log("Death");
			return;
		}
		Destroy(gameObject);
Unknown's avatar
Unknown committed
44
    }
45
46
47
48
49
50

	public int getCurrentLife()
	{
		return currentLife;
	}
}