forked from devops001/samsara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
63 lines (57 loc) · 1.99 KB
/
action.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
var Action = function(properties) {
Entity.call(this, properties);
properties = properties || {};
this.command = properties.command || 'rest';
this.iconName = properties.icon || 'Icon.4_18.png';
this.coolDownCost = (properties.coolDownCost!=null ? properties.coolDownCost : 1);
this.isSustained = (properties.isSustained!=null ? properties.isSustained : false);
this.image = document.createElement('img');
this.image.id = this.id;
this.image.src = 'assets/icons/'+this.iconName;
this.image.className = "actionIcon actionReady";
this.coolDownValue = 0;
this.initEvents();
};
Action.prototype = Object.create(Entity.prototype);
Action.prototype.initEvents = function() {
this.image.command = this.command;
this.image.addEventListener("mouseenter", function(e) {
var action = App.getAction(e.target.id);
Helpers.showPopup(action.getPopupHTML());
});
this.image.addEventListener("mouseleave", function(e) {
Helpers.hidePopup();
});
this.image.addEventListener("click", function(e) {
var action = App.getAction(e.target.id);
action.use();
});
};
Action.prototype.update = function() {
if (this.coolDownValue > 0) {
this.coolDownValue--;
if (this.coolDownValue == 0) {
this.image.className = "actionIcon actionReady";
}
}
};
Action.prototype.use = function() {
if (this.coolDownValue == 0) {
App.runCommand(this.command);
this.coolDownValue = this.coolDownCost;
if (this.coolDownValue != 0) {
this.image.className = "actionIcon actionNotReady";
}
}
};
Action.prototype.getPopupHTML = function() {
var html = Entity.prototype.getPopupHTML.call(this);
html += '<hr/><table>';
html += '<tr><td>id: </td><td>'+ this.id +'</td></tr>';
html += '<tr><td>cooldown cost: </td><td>'+ this.coolDownCost +'</td></tr>';
if (this.coolDownValue > 0) {
html += '<tr><td>cooldown value: </td><td>'+ this.coolDownValue +'</td></tr>';
}
html += '</table>';
return html;
};