-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
357 lines (309 loc) · 9.57 KB
/
index.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<html>
<head>
<style>
#messages p {
margin:1;
}
</style>
</head>
<body>
<div id="canvasContainer" style="float:left; border: thick solid black;"></div>
<div id="messages" style="float:left; width: 300px; height: 384px; padding: 5px; background-color: black; color: white; font-family: courier; font-size: 12px; overflow-y: scroll;"></div>
<div style="float:left;">
<div style="width:400px; padding: 10px;">
<ul>
<li><b>h,j,k,l</b>: left, down, up, right</li>
<li><b>i</b>: show inventory</li>
<li><b>,</b> (comma): pick up items</li>
<li><b>o</b> <i>[direction]</i>: open door in [direction]</li>
<li><b>c</b> <i>[direction]</i>: close door in [direction]</li>
<li><b>d</b> <i>[number]</i>: drop item in slot [number]</li>
<li><b>s</b> <i>[number] [direction]</i>: shoot item [number] in [direction] (e.g., <b>s2h</b> shoots item in slot 5 to the left)</li>
<li><b>s</b> <i>[direction]</i>: shoot the first shootable item in your inventory in [direction]</li>
</ul>
<a href="/gm.html"><b>But I'd rather be GMing!</b></a>
</div>
</div>
<script src="rot.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
ROT.RNG.setSeed(12345);
ROT.DEFAULT_WIDTH = 83;
ROT.DEFAULT_HEIGHT = 32;
var id, level = 1;
var mapData = [];
var inventory = [];
var inventoryUp = false;
var health = 10;
var entities = [];
var display = new ROT.Display({fontSize:12});
document.getElementById("canvasContainer").appendChild(display.getContainer());
var socket = io.connect('//' + location.host);
socket.emit("player");
socket.on("map+pos", function(updates) { redraw(updates.map, updates.pos); });
socket.on("map", function(mapUpdates) { redraw(mapUpdates, {}); });
socket.on("pos", function(pos) { redraw({}, pos); });
socket.on("id", function(myid) {
id = myid;
display.clear();
level = 1;
mapData = [];
inventory = [];
inventoryUp = false;
health = 10;
entities = [];
entities[1] = {};
messages = [];
// init empty map
initMapLevel(1);
});
socket.on("inventory", function(data) {
if(data.change == "add") {
inventory[data.slot] = data.item;
} else if(data.change == "remove") {
delete inventory[data.slot];
}
});
socket.on("health", function(data) { health = data.value; drawHealth(data.value); });
function drawHealth(value) {
for(var i=0; i < value; ++i) {
display.draw(82, 10-i, "o", "#F00", "#000");
}
for(;i<10; ++i) {
display.draw(82, 10-i, "-", "#F00", "#000");
}
}
function initMapLevel(level) {
mapData[level] = [];
for(var i=0; i < ROT.DEFAULT_WIDTH; ++i) {
for(var j=0; j < ROT.DEFAULT_HEIGHT; ++j) {
if(mapData[level][i] == null) { mapData[level][i] = []; }
mapData[level][i][j] = -1;
}
}
}
// Translation between keys and directions
var dirToVector = {};
dirToVector[ROT.VK_H] = { x:-1, y:0};
dirToVector[ROT.VK_L] = { x:1, y:0};
dirToVector[ROT.VK_K] = { x:0, y:-1};
dirToVector[ROT.VK_J] = { x:0, y:1};
dirToVector[ROT.VK_Y] = { x:-1, y:-1};
dirToVector[ROT.VK_U] = { x:1, y:-1};
dirToVector[ROT.VK_B] = { x:-1, y:1};
dirToVector[ROT.VK_N] = { x:1, y:1};
// Translation between keys and item numbers
var keyToItem = {};
for (i = 0; i < 10; i++) {
keyToItem[i + "0".charCodeAt(0)] = i;
}
// Concise list of commands
var interpreters = {}
interpreters[ROT.VK_I] =
function () {
showInventory();
interpCallback = function (key) {
hideInventory();
interpCallback = defaultInterpreter;
if (key != ROT.VK_I) {
defaultInterpreter(key);
}
};
};
interpreters[ROT.VK_Z] = DirCommand("zap");
interpreters[ROT.VK_A] = DirCommand("mine");
interpreters[ROT.VK_S] = ItemDirCommand("shoot");
interpreters[ROT.VK_O] = DirCommand("open");
interpreters[ROT.VK_C] = DirCommand("close");
interpreters[ROT.VK_COMMA] = Command("pickup");
interpreters[ROT.VK_D] = ItemCommand("drop");
// Include move commands
for (moveKey in dirToVector) {
interpreters[moveKey] = (function (key) {
return function () { socket.emit("move", dirToVector[key]); }
})(moveKey);
}
// Include a clear command to abort any other command
interpreters[ROT.VK_ESCAPE] = function () {
hideInventory();
interpCallback = defaultInterpreter;
}
// Default key interpreting function
var defaultInterpreter = function (key) {
if (key in interpreters) {
interpreters[key]();
}
}
// Currently active callback
var interpCallback = defaultInterpreter;
// To make sure the active call back always gets back to
// the default eventually, a callback must return something
// truthy or the callback will revert.
function makeCB(f) {
return function (k) {
if (!f(k)) {
interpCallback = defaultInterpreter;
}
}
}
// Class for actions taking no parameters
function Command (action) {
return function () {
socket.emit(action);
}
}
// Class for actions taking only a direction
function DirCommand (action) {
return function () {
interpCallback = makeCB( function (key) {
vec = dirToVector[key];
if (vec) {
socket.emit(action, vec);
}
});
}
}
// Class for actions taking an item
function ItemCommand (action) {
return function () {
interpCallback = makeCB( function (key) {
item = keyToItem[key];
if (item <= 9 && item >= 0) {
socket.emit(action, {"itemNum":item});
}
});
}
}
// Class for actions taking an item and a direction
function ItemDirCommand (action) {
return function () {
interpCallback = makeCB( function (key) {
item = keyToItem[key];
if (item == undefined) {
item = defaultItemForAction(action);
vec = dirToVector[key]
if (vec) {
vec.itemNum = item;
socket.emit(action, vec);
}
}
else if (item <= 9 && item >= 0) {
interpCallback = makeCB( function (key) {
vec = dirToVector[key];
if (vec) {
vec.itemNum = item;
socket.emit(action, vec);
}
});
return true; // Don't go back to default handler yet
}
});
}
}
window.addEventListener("keydown", function (e) {
var code = e.keyCode;
interpCallback(code);
});
function drawMap(mapUpdates) {
if(typeof mapData[level] == 'undefined') { initMapLevel(level); }
// update map state with new data
for(i in mapUpdates) {
var coords = i.split(',');
mapData[level][coords[0]][coords[1]] = mapUpdates[i];
}
// redraw map
for(i=0; i < ROT.DEFAULT_WIDTH; ++i) {
for(j=0; j < ROT.DEFAULT_HEIGHT; ++j) {
drawMapChar(i, j, mapData[level][i][j]==0?".":" ", "#fff", ["#000", "#777"][mapData[level][i][j]]);
}
}
}
function updateEntities(posData) {
var i;
if(posData.clear) { entities[level] = {}; }
if(posData["add"]) {
for(i in posData["add"]) {
entities[level][i] = posData["add"][i];
}
}
if(posData["remove"]) {
for(i=0; i<posData["remove"].length; ++i) {
delete entities[level][posData["remove"][i]];
}
}
}
function drawEntities() {
for(i in entities[level]) {
var pos = entities[level][i];
// if entity is a blocking entity (player, boulder), draw it
if(pos.blocking) { drawMapChar(pos.x, pos.y, pos.symbol, pos.color); }
else {
// if entity is non-blocking, defer to any blocking entities on the space
var hasBlocking = false;
for(var j in entities[level]) {
if(entities[level][j].blocking && entities[level][j].x == pos.x && entities[level][j].y == pos.y) {
hasBlocking = true;
break;
}
}
if(!hasBlocking) { drawMapChar(pos.x, pos.y, pos.symbol, pos.color); }
}
}
}
function redraw(map, posData) {
if(posData["add"] && posData["add"][id] != undefined) {
level = posData["add"][id].z;
if(entities[level] == undefined) { entities[level] = {}; }
}
updateEntities(posData);
drawMap(map);
drawEntities();
drawHealth(health);
}
function drawMapChar(x,y,ch,fg,bg) {
if(!inventoryUp || ((x < 2 || x > 36) && (y < 4 || y > 14))) {
display.draw(x, y+2, ch, fg, bg);
}
}
function showInventory() {
inventoryUp = true;
var thirtySpaces = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
display.drawText(2, 3, "--------------------------------");
display.drawText(2, 4, ("| Inventory" + thirtySpaces).substr(0,30) + " |");
var invCount = 0;
for(var i=0; i<10; ++i) {
if(inventory[i]) {
display.drawText(2, invCount+5, ("| " + i + ": " + inventory[i].name + thirtySpaces).substr(0,30) + " |", 34);
invCount++;
}
}
for(i=0; i<10 - invCount; ++i) {
display.drawText(2, invCount+i+5, "| " + thirtySpaces + " |", 34);
}
display.drawText(2, invCount+i+5, "--------------------------------");
}
function hideInventory() {
inventoryUp = false;
redraw({}, {});
}
function defaultItemForAction(actionName) {
if(actionName == "shoot") {
for(var i=0; i<inventory.length; ++i) {
if(inventory[i] && inventory[i].shootable) return i;
}
}
return null;
}
function showMessage(m) {
var p = document.createElement("p");
var messages = document.getElementById("messages");
p[(p.innerText == undefined)?"textContent":"innerText"] = m;
messages.appendChild(p);
messages.scrollTop = messages.scrollHeight;
}
socket.on("output", function(m) {
showMessage(m);
});
</script>
</body>
</html>