-
Notifications
You must be signed in to change notification settings - Fork 34
/
info.js
131 lines (122 loc) · 3.72 KB
/
info.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function Info(message, character) {
this.data = message.Data;
this.character = character;
this.text = message.Text;
this.type = message.Type;
this.time = Date.now();
this.x = 0;
this.y = 0;
this.duration = 2500;
this.value = null;
if (game.help)
game.help.runHook(this);
switch(this.type) {
case "damage-gain":
this.value = -this.data;
break;
case "damage-deal":
this.value = this.data.Dmg;
break;
case "heal":
case "exp-gain":
this.value = this.data;
break;
case "item-gain":
case "craft-success":
game.controller.highlight("inventory");
var item = Entity.get(this.data);
if (!item) {
game.sendError("(Info.js) Cannot find item %d", this.data);
return
}
// it's possible when we replace item e.g. in onCraft
var container = game.containers[item.Container];
if (!container) {
game.sendError("(Info.js) Cannot find container %d for item %d", item.Container, item.Id);
return;
}
container.reload();
var slot = container.slots[container.contents.indexOf(item.Id)];
if (!slot)
console.trace(container, item)
slot.classList.add("new");
break;
case "build-open":
var blank = Entity.get(this.data);
game.controller.build.open(blank);
return;
}
if (!this.text)
return;
if (game.chat)
game.chat.addMessage(this.text);
else
console.warn(this.text, "Chat is null")
if (!this.data) {
var record = document.createElement("div");
record.textContent = this.text;
document.getElementById("messages").appendChild(record);
setTimeout(function() {
record.parentNode.removeChild(record);
}, this.duration);
}
//TODO: cache
this.damageTexture = game.loader.loadImage("damage.png");
this.frame = 0;
};
Info.prototype = {
update: function(k) {
if (this.time + this.duration < Date.now()) {
this.character.info.splice(this.character.info.indexOf(this), 1);
return;
}
switch(this.type) {
case "text":
break;
default:
this.y -= 10 * k;
}
},
draw: function(x, y) {
switch(this.type) {
case "heal":
this.drawValue(x, y, "#0c0", "+" + this.value + "hp");
break;
case "exp-gain":
this.drawValue(x, y, "#ff0", "+" + this.value + "xp");
break;
case "damage-deal": {
//TODO: fixme
var character = game.characters[this.data.To]; //on teleport/death can be empty
if (character) {
var p = character.screen();
this.drawValue(p.x, p.y - (CELL_SIZE + FONT_SIZE), "#0ff", null, character);
}
break;
}
case "damage-gain":
this.drawValue(x, y, "#f00", null, game.player);
break;
}
},
drawValue: function(x, y, color, text, target) {
//TODO: omfg
if (target && this.damageTexture && this.frame * 64 < this.damageTexture.width) {
game.ctx.drawImage(
this.damageTexture,
64 * this.frame++,
0,
64,
63,
Math.round(x - 32),
Math.round(y - ((target.Type == "human") ? 64 : 32)),
64,
63
);
}
text = text || this.value.toFixed(2);
x = x + this.x - game.ctx.measureText(text).width / 2;
game.ctx.fillStyle = color;
game.drawStrokedText(text, x, y + this.y);
},
}