2015-03-01 07:00:17 +00:00
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2015 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.
|
2015-03-01 07:00:17 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
2015-02-17 05:15:04 +00:00
|
|
|
Phaser.Component.Health = function () {};
|
|
|
|
|
|
|
|
Phaser.Component.Health.prototype = {
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* 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
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
|
|
|
health: 1,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* 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.
|
2015-02-17 05:15:04 +00:00
|
|
|
*
|
2015-03-01 07:00:17 +00:00
|
|
|
* @member
|
2015-03-23 23:27:14 +00:00
|
|
|
* @param {number} amount - The amount to subtract from the current `health` value.
|
|
|
|
* @return {Phaser.Sprite} This instance.
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
|
|
|
damage: function(amount) {
|
|
|
|
|
|
|
|
if (this.alive)
|
|
|
|
{
|
|
|
|
this.health -= amount;
|
|
|
|
|
|
|
|
if (this.health <= 0)
|
|
|
|
{
|
|
|
|
this.kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|