forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealth.js
More file actions
106 lines (86 loc) · 2.61 KB
/
Copy pathHealth.js
File metadata and controls
106 lines (86 loc) · 2.61 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Health component provides the ability for Game Objects to have a `health` property
* that can be damaged and reset through game code.
* Requires the LifeSpan component.
*
* @class
*/
Phaser.Component.Health = function () {};
Phaser.Component.Health.prototype = {
/**
* The Game Objects health value. This is a handy property for setting and manipulating health on a Game Object.
*
* It can be used in combination with the `damage` method or modified directly.
*
* @property {number} health
* @default
*/
health: 1,
/**
* The Game Objects maximum health value. This works in combination with the `heal` method to ensure
* the health value never exceeds the maximum.
*
* @property {number} maxHealth
* @default
*/
maxHealth: 100,
/**
* Damages the Game Object. This removes the given amount of health from the `health` property.
*
* If health is taken below or is equal to zero then the `kill` method is called.
*
* @member
* @param {number} amount - The amount to subtract from the current `health` value.
* @return {Phaser.Sprite} This instance.
*/
damage: function (amount) {
if (this.alive)
{
this.health -= amount;
if (this.health <= 0)
{
this.kill();
}
}
return this;
},
/**
* Sets the health property of the Game Object to the given amount.
* Will never exceed the `maxHealth` value.
*
* @member
* @param {number} amount - The amount to set the `health` value to. The total will never exceed `maxHealth`.
* @return {Phaser.Sprite} This instance.
*/
setHealth: function (amount) {
this.health = amount;
if (this.health > this.maxHealth)
{
this.health = this.maxHealth;
}
return this;
},
/**
* Heal the Game Object. This adds the given amount of health to the `health` property.
*
* @member
* @param {number} amount - The amount to add to the current `health` value. The total will never exceed `maxHealth`.
* @return {Phaser.Sprite} This instance.
*/
heal: function (amount) {
if (this.alive)
{
this.health += amount;
if (this.health > this.maxHealth)
{
this.health = this.maxHealth;
}
}
return this;
}
};