Life.cs 1.04 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;
Unknown's avatar
Unknown committed
5
using PlayerSpawnPointScript;
Unknown's avatar
Unknown committed
6
7
8
9
10
11

public class Life : MonoBehaviour {

    [SerializeField] private int m_MaxLife = 1;

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

    void Awake(){
        currentLife = m_MaxLife;
    }

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

    public void Death(){
mthiele2's avatar
mthiele2 committed
38
39
		if (gameObject.CompareTag("Player"))
		{
Unknown's avatar
Unknown committed
40
			//PlayerSpawnPoint.psp.SetPlayerPosition();
mthiele2's avatar
mthiele2 committed
41
			gameObject.SetActive(false);
Unknown's avatar
Unknown committed
42
			Heal(5);
mthiele2's avatar
mthiele2 committed
43
			Debug.Log("Death");
Unknown's avatar
Unknown committed
44

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

	public int getCurrentLife()
	{
		return currentLife;
	}
}