-
Notifications
You must be signed in to change notification settings - Fork 1
/
gm.html
151 lines (125 loc) · 4.05 KB
/
gm.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
<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;">
Click to add Grid Bugs.
<br>(New features coming eventually.)
<br><br>
<a href="/"><b>But I'd rather be adventuring!</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 = [];
entities[1] = {};
var display = new ROT.Display({fontSize:12});
document.getElementById("canvasContainer").appendChild(display.getContainer());
var socket = io.connect('//' + location.host);
socket.emit("gm");
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); });
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;
}
}
}
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();
}
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 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);
});
display.getContainer().addEventListener("click", function(e) {
var loc = display.eventToPosition(e);
socket.emit("monster", { x: loc[0], y: loc[1]-2, name: "GridBug" });
});
</script>
</body>
</html>