-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.js
86 lines (61 loc) · 2.27 KB
/
health.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
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
'use strict';
var inherits = require('inherits');
var EventEmitter = require('events').EventEmitter;
module.exports = function(game, opts) {
return new Health(game, opts);
};
module.exports.pluginInfo = {
loadAfter: ['voxel-commands']
};
function Health(game, opts) {
this.maxHealth = opts.maxHealth || 10;
this.minHealth = opts.minHealth || 0;
this.startHealth = opts.startHealth || this.maxHealth;
this.value = this.startHealth;
this.game = game;
this.enable();
}
inherits(Health, EventEmitter);
Health.prototype.enable = function() {
if (this.game.plugins && this.game.plugins.isEnabled('voxel-commands')) {
this.game.plugins.get('voxel-commands').registerCommand('heal', this.onHeal = this.heal.bind(this, this.maxHealth), '', 'sets health to maximum');
}
};
Health.prototype.disable = function() {
if (this.game.plugins && this.game.plugins.isEnabled('voxel-commands')) {
this.game.plugins.get('voxel-commands').unregisterCommand('heal', this.onHeal);
}
};
Health.prototype.hurt = function(amount) {
if (amount < 0) return this.heal(-amount);
if (amount !== Infinity && !Number.isFinite(amount)) throw new Error('voxel-health hurt('+amount+') called with non-finite amount');
var oldValue = this.value;
this.value -= amount;
if (this.value < this.minHealth)
this.value = this.minHealth;
var effectiveAmount = this.value - oldValue;
this.emit('health', this.value, oldValue);
this.emit('hurt', effectiveAmount, amount, this.value, oldValue);
if (this.value === this.minHealth) {
this.emit('die');
}
return effectiveAmount;
};
Health.prototype.heal = function(amount) {
if (amount < 0) return this.hurt(-amount);
if (amount !== Infinity && !Number.isFinite(amount)) throw new Error('voxel-health heal('+amount+') called with non-finite amount');
var oldValue = this.value;
this.value += amount;
if (this.value > this.maxHealth)
this.value = this.maxHealth;
var effectiveAmount = oldValue - this.value;
this.emit('health', this.value, oldValue);
this.emit('heal', effectiveAmount, amount, this.value, oldValue);
return effectiveAmount;
};
Health.prototype.scaledValue = function() {
return this.value / this.maxHealth;
};
Health.prototype.percentage = function() {
return this.scaledValue() * 100.0;
};