forked from NodiumHosting/VaultMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webmap-v1.html
237 lines (194 loc) · 5.47 KB
/
webmap-v1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>VaultMapper Integration</title>
</head>
<body>
<canvas id="map" width="399px" height="399px"></canvas>
<script>
const WEBMAP_VERSION = 1;
const canvas = document.getElementById('map');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
const middle = Math.floor(width / 2);
const cellSize = Math.floor(width / 49);
let ws = null;
function tryConnectWebSocket() {
if (ws === null || ws.readyState === WebSocket.CLOSED) {
ws = new WebSocket('ws://localhost:58008');
ws.onopen = () => {
console.log('WebSocket connection established');
};
ws.onmessage = (event) => onMessage(event);
ws.onclose = () => {
console.log('WebSocket connection closed');
ws = null;
setTimeout(tryConnectWebSocket, 1000);
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
ws.close();
};
}
}
if (ws === null) {
tryConnectWebSocket();
}
// save cell data
class Cell {
x;
y;
color;
type; // room, tunnelX, tunnelZ
constructor(x, y, color, type) {
this.x = x;
this.y = y;
this.color = color;
this.type = type;
}
}
class arrow {
x;
y;
yaw;
player;
color;
constructor(x, y, yaw, player, color) {
this.x = x;
this.y = y;
this.yaw = yaw;
this.player = player;
this.color = color;
}
}
const cells = localStorage.getItem('cells') ? JSON.parse(localStorage.getItem('cells')) : [];
const arrows = localStorage.getItem('arrows') ? JSON.parse(localStorage.getItem('arrows')) : [];
doRender();
function onMessage(event) {
//console.log('Received data:', event.data);
const data = event.data.split(':');
const type = data[0]; // room, tunnelX, tunnelZ
if (type === 'version') {
const version = parseInt(data[1]);
if (version !== WEBMAP_VERSION) {
console.error('WebMap version mismatch');
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = 'black';
ctx.font = '20px Arial';
ctx.fillText(
'WebMap version mismatch',
middle - 100,
middle,
);
ws.close();
return;
}
}
if (event.data.startsWith('reset')) {
cells.length = 0;
arrows.length = 0;
localStorage.setItem('cells', JSON.stringify([]));
localStorage.setItem('arrows', JSON.stringify([]));
doRender();
return;
}
if (type === 'player') {
const x = parseInt(data[1]);
const y = parseInt(data[2]);
const yaw = parseInt(data[3]);
const player = data[4];
const color = data[5]; // hex color
const arr = new arrow(x, y, yaw, player, color);
if (arrows.find((a) => a.player === player)) {
arrows.splice(
arrows.findIndex((a) => a.player === player),
1,
);
}
arrows.push(arr);
localStorage.setItem('arrows', JSON.stringify(arrows));
doRender();
return;
}
const x = parseInt(data[1]);
const y = parseInt(data[2]);
const color = data[3]; // hex color
const cell = new Cell(x, y, color, type);
if (cells.find((c) => c.x === x && c.y === y)) {
cells.splice(
cells.findIndex((c) => c.x === x && c.y === y),
1,
);
}
cells.push(cell);
localStorage.setItem('cells', JSON.stringify(cells));
doRender();
}
function fillCell(x, y, color, type) {
x += 24;
y += 24;
ctx.fillStyle = color;
switch (type) {
case 'room':
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
break;
case 'tunnelX':
ctx.fillRect(
x * cellSize,
y * cellSize + cellSize / 4,
cellSize,
(cellSize / 4) * 2,
);
break;
case 'tunnelZ':
ctx.fillRect(
x * cellSize + cellSize / 4,
y * cellSize,
(cellSize / 4) * 2,
cellSize,
);
break;
}
}
function doRender() {
ctx.clearRect(0, 0, width, height);
for (const cell of cells) {
fillCell(cell.x, cell.y, cell.color, cell.type);
}
for (const arrow of arrows) {
const x = arrow.x + 24 + 1.5;
const y = arrow.y + 24 + 0.5;
const yaw = arrow.yaw;
const player = arrow.player;
const color = arrow.color;
//make a rotated triangle
const x1 = -1;
const y1 = -0.66;
const x2 = -1;
const y2 = 0.66;
const x3 = 1;
const y3 = 0;
const cx = -1;
const cy = 0;
const radangle = (yaw + 90) * (Math.PI / 180);
const x1r = cx + (x1 - cx) * Math.cos(radangle) - (y1 - cy) * Math.sin(radangle);
const y1r = cy + (x1 - cx) * Math.sin(radangle) + (y1 - cy) * Math.cos(radangle);
const x2r = cx + (x2 - cx) * Math.cos(radangle) - (y2 - cy) * Math.sin(radangle);
const y2r = cy + (x2 - cx) * Math.sin(radangle) + (y2 - cy) * Math.cos(radangle);
const x3r = cx + (x3 - cx) * Math.cos(radangle) - (y3 - cy) * Math.sin(radangle);
const y3r = cy + (x3 - cx) * Math.sin(radangle) + (y3 - cy) * Math.cos(radangle);
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo((x + x1r) * cellSize, (y + y1r) * cellSize);
ctx.lineTo((x + x2r) * cellSize, (y + y2r) * cellSize);
ctx.lineTo((x + x3r) * cellSize, (y + y3r) * cellSize);
ctx.fill();
}
}
</script>
</body>
</html>