-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmeter.js
100 lines (82 loc) · 2.12 KB
/
meter.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var inherits = require('inherits');
var Entity = require('crtrdg-entity');
module.exports = Meter;
function Meter(options) {
Entity.call(this);
var self = this;
this.level = 100;
this.rate = 1;
this.size = { y: 11 };
this.listeners = [];
if (options.hasOwnProperty("name")) {
this.name = options.name;
}
if (options.hasOwnProperty("color")) {
this.color = options.color;
}
if (options.hasOwnProperty("game")) {
this.game = options.game;
}
if (options.hasOwnProperty("camera")) {
this.camera = options.camera;
}
this.position = options.position;
this.game.addIntervalEvent(this);
this.everySecond = function() {
if (this.level > 0) {
this.level -= 1;
this.listeners.forEach(function(listener, index, array) {
listener.changeEvent("remove", 1);
});
}
//console.log(this.name + " meter level: "+ this.level);
};
this.on('draw', function(c) {
c.save();
var label = this.name;
c.fillStyle = "white";
c.font = "12px Arial";
c.fillText(label, self.position.x - 5, self.position.y + 9);
c.beginPath();
c.lineWidth = 2;
c.strokeStyle = "black";
c.fillStyle = self.color;
c.rect(
self.position.x + 45,// - self.camera.position.x + 45,
self.position.y,// - self.camera.position.y,
102,
self.size.y + 2
);
c.stroke();
c.fillRect(
self.position.x + 46, // - self.camera.position.x + 46,
self.position.y + 1, // - self.camera.position.y + 1,
self.level,
self.size.y
);
c.restore();
});
};
inherits(Meter, Entity);
// Add value to this meter
Meter.prototype.add = function(value) {
var new_level = this.level += value;
if (new_level > 100) {
new_level = 100;
}
this.level = new_level;
}
// Remove value to this meter
Meter.prototype.remove = function(value) {
var new_level = this.level -= value;
if (new_level < 0) {
new_level = 0;
}
this.level = new_level;
this.listeners.forEach(function(listener, index, array) {
listener("remove", value);
});
}
Meter.prototype.addListener = function(listener) {
this.listeners.push(listener);
}