-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerHealth.js
59 lines (49 loc) · 1.3 KB
/
PlayerHealth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma strict
var healthBar : GameObject;
healthBar = GameObject.Find("PlayerHealthBar");
var healthMeterLength:int;
healthMeterLength = healthBar.guiTexture.pixelInset.width;
public var maxHealth:float = 100;
public var currentHealth:float = 100;
private var player : Transform;
private var gameOver : boolean = false;
function Start () {
player = GameObject.FindWithTag("Player").transform;
}
// Funktion som uppdaterar hälsomätaren grafiskt.
function AdjustHealth(amount:float){
currentHealth += amount;
if(currentHealth <= 0){
GameOver();
}
if(currentHealth < 1){
currentHealth = 0;
}
if(currentHealth > maxHealth){
currentHealth = maxHealth;
}
healthBar.guiTexture.pixelInset.width = healthMeterLength * (currentHealth/maxHealth);
}
//Om hälsomätaren har nått botten.
function GameOver(){
player.gameObject.SendMessage("PlayerDie");
//Destroy(player.gameObject);
yield WaitForSeconds(1.0);
gameOver = true;
}
//GUI-meny för game over.
function OnGUI()
{
var buttonPadding : int = 30;
if(gameOver)
{
if(GUI.Button(Rect(Screen.width/2-100, Screen.height/2-100, 200, 100), "Retry level?"))
{
Application.LoadLevel("level1");
}
if(GUI.Button(Rect(Screen.width/2-100, Screen.height/2+buttonPadding, 200, 100), "Main menu"))
{
Application.LoadLevel("GameMenu");
}
}
}