Slash Hero Game

16.534 lượt xem;
1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class PlayerHealth : MonoBehaviour {
6
7     
public GameObject[] healthBar;
8
9     
private int health;
10     
private int maxHealth;
11
12     
void Awake(){
13         maxHealth = healthBar.Length;
14         health = maxHealth;
15     }
16
17     
// Use this for initialization
18     
void Start () {
19         InitializeHealthVariable ();
20     }
21     
22     
// Update is called once per frame
23     
void Update () {
24         
25     }
26
27     
void InitializeHealthVariable(){
28         
for (int i = 0; i < maxHealth; i++) {
29             
if (health > i) {
30                 healthBar [i].SetActive (
true);
31             }
else {
32                 healthBar [i].SetActive (
false);
33             }
34         }
35     }
36
37
38     
public void Health(int damage){
39         
if(health > 0){
40             health -= damage;
41             InitializeHealthVariable ();
42         }
43
44         
if(health <= 0){
45             
if (!GameplayController.instance.playerDied) {
46                 PlayerDied ();
47             }
48         }
49     }
50
51     
void PlayerDied(){
52         gameObject.transform.parent.gameObject.GetComponent<Animator> ().Play (
"Death");
53         gameObject.transform.parent.gameObject.GetComponent<PlayerController> ().speed =
0;
54         GameplayController.instance.GameOver ();
55     }
56
57     
public void AddHealth(int health){
58         
if(health <= maxHealth){
59             
this.health += health;
60             
if(this.health >= maxHealth){
61                 
this.health = maxHealth;
62             }
63         }
64
65         InitializeHealthVariable ();
66     }
67         
68 }


Gõ tìm kiếm nhanh...