-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
418 lines (357 loc) · 14.5 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE html>
<html>
<head>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://openlayers.org/en/v6.1.1/css/ol.css"
/>
<link
href="https://fonts.googleapis.com/css?family=Roboto&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css"
integrity="sha256-46qynGAkLSFpVbEBog43gvNhfrOj+BmwXdxFgVK/Kvc="
crossorigin="anonymous" />
<style>
#map {
background: #202020;
font-size: 16px;
font-family: "Roboto", sans-serif;
}
body,
html,
#map {
margin: 0px;
width: 100%;
height: 100%;
}
.layer-select {
position: absolute;
top: 0.5em;
right: 0.5em;
width: 150px;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body margin="0" padding="0">
<script src="https://openlayers.org/en/v6.1.1/build/ol.js"></script>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"
></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"
></script>
<script src="playersData.js"></script>
<div id="map"></div>
<script>
var layers = {
dim0: {
name: "Overworld",
attribution:
'<a href="https://github.com/mjungnickel18/papyruscs">PapyrusCS</a>',
minNativeZoom: 15,
minZoom: 15,
maxNativeZoom: 20,
maxZoom: 22,
noWrap: true,
tileSize: 512,
folder: "dim0",
fileExtension: "png"
}
};
var config = {
factor: 1
};
layers = {"dim0":{"name":"Overworld","attribution":"Generated by <a href=\"https://github.com/mjungnickel18/papyruscs\">PapyrusCS</a>","minNativeZoom":13,"maxNativeZoom":20,"noWrap":true,"tileSize":1024,"folder":"dim0","fileExtension":"png"}};
config = {"factor":65536.0,"globalMinZoom":13,"globalMaxZoom":20,"tileSize":1024,"blocksPerTile":64};
// ======
// For the extent sizes specified below, OpenLayers tries to fit
// the whole extents given in a space [0, 0] -> [2^8, 2^8] visually
// on screen at zoom level 0.
//
// This constant represents that internal tile size that OpenLayers
// will aim for, so that we can calculate zoom levels correctly later.
const openLayersInternalTileSize = Math.pow(2, 8); // 256
// The actual size of the tiles that we're using in Papyrus.
const tileSize = config.tileSize;
// The maximum positive extents of the whole map. The units for this
// value are "number of tiles at the most zoomed out level generated
// by Papyrus". That is, if Papyrus generates a minimum zoom level
// of 15, then the units for this extent size are the number of (Papyrus)
// zoom level 15 tiles to display at (OpenLayers) zoom level 0.
//
// The minimum zoom level that Papyrus is using is in config.globalMinZoom.
const maximumExtentSize = 10000;
// The minimum negative extents of the whole map. Uses the same units
// as maximumExtentSize.
const minimumExtentSize = -10000;
// Computes the resolutions array to use for OpenLayers. For each OpenLayers
// zoom level (0 through 42), this computes the ratio such that a single tile
// at OpenLayers zoom level 0 is a single tile at the minimum Papyrus zoom level.
const papyrusMinimumZoomScale = Math.pow(2, config.globalMinZoom);
const convertedFromTilesToPixelsUsingTileSize = papyrusMinimumZoomScale / tileSize;
const resolutions = new Array(43);
for (let z = 0; z < 43; ++z) {
resolutions[z] = convertedFromTilesToPixelsUsingTileSize / Math.pow(2, z);
}
// When we calculate resolutions above, we've effectively saying that zoom level 0 is
// zoom level N, where N is the lowest zoom level. This zoom level N also becomes
// the range of [0, 0] -> [1, 1] in the coordinate system. We need to be able to translate
// coordinates in that "zoomed out" coordinate system, back down to the maximum zoom level
// where each tile represents 32x32 (depending on tilesize) Minecraft tiles.
const zoomRatioForMaximumZoom = 1 / Math.pow(2, config.globalMaxZoom - config.globalMinZoom);
const minecraftTilesAtMostZoomedInLevel = config.blocksPerTile;
// Use a projection where pixels have a 1:1 ratio with the screen at zoom level 0.
const projection = new ol.proj.Projection({
code: "ZOOMIFY",
units: "pixels",
extent: [
0,
0,
openLayersInternalTileSize / tileSize,
openLayersInternalTileSize / tileSize
]
});
// Construct the tile grid using the desired maximum and minimum extents listed above,
// set the origin to [0, 0] (the center of the map), and the calculated resolutions array.
const tilegrid = new ol.tilegrid.TileGrid({
extent: [
minimumExtentSize,
minimumExtentSize,
maximumExtentSize,
maximumExtentSize
],
origin: [0, 0],
resolutions: resolutions,
tileSize: [tileSize, tileSize]
});
let map;
let locationElement;
const tileLayers = Object.keys(layers)
.sort()
.map(function(layerKey, idx) {
const layer = layers[layerKey];
const tileLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
tileUrlFunction: function(tileCoord, pixelRatio, projection) {
const z = tileCoord[0];
const x = tileCoord[1];
const y = tileCoord[2];
return (
"./" +
layer.folder +
"/" +
z +
"/" +
x +
"/" +
y +
"." +
layer.fileExtension
);
},
projection: projection,
tileGrid: tilegrid,
attributions: layer.attribution
}),
visible: idx == 0
});
tileLayer.metaLayerKey = layerKey;
return tileLayer;
});
const initialLayer = layers[Object.keys(layers).sort()[0]];
if (Object.keys(layers).sort()[0] == "dim0_stronghold") {
document.getElementById("map").style.background = "#fff";
} else {
document.getElementById("map").style.background = "#202020";
}
const view = new ol.View({
projection: projection,
center: [0, 0],
zoom: 0,
minZoom: 0,
maxZoom: config.globalMaxZoom - config.globalMinZoom
});
const PapyrusControls = (function(Control) {
function PapyrusControls(opt_options) {
const options = opt_options || {};
const element = document.createElement("div");
element.className = "layer-select ol-unselectable";
const card = document.createElement("div");
card.className = "card";
element.appendChild(card);
const cardBody = document.createElement("div");
cardBody.className = "card-body p-3 px-3";
card.appendChild(cardBody);
const form = document.createElement("form");
cardBody.appendChild(form);
let currentSelectedLayer = Object.keys(layers).sort()[0];
let rememberedCenters = {};
let rememberedZoom = {};
Object.keys(layers)
.sort()
.forEach(function(layerKey, idx) {
const layer = layers[layerKey];
const radioContainer = document.createElement("div");
radioContainer.className = "custom-control custom-radio";
const radioInput = document.createElement("input");
radioInput.type = "radio";
radioInput.id = layerKey;
radioInput.name = "layers";
radioInput.className = "custom-control-input";
radioInput.checked = idx == 0;
radioInput.value = layerKey;
radioContainer.appendChild(radioInput);
const radioLabel = document.createElement("label");
radioLabel.htmlFor = layerKey;
radioLabel.className = "custom-control-label";
radioLabel.innerText = layer.name;
radioContainer.appendChild(radioLabel);
const selectLayer = function(e) {
if (layerKey == "dim0_stronghold") {
document.getElementById("map").style.background = "#fff";
} else {
document.getElementById("map").style.background = "#202020";
}
if (currentSelectedLayer != layerKey) {
const runtimeLayers = map.getLayers();
runtimeLayers.forEach(function(runtimeLayer) {
runtimeLayer.setVisible(
runtimeLayer.metaLayerKey == layerKey
);
});
const oldFocusGroup = currentSelectedLayer.substr(0, 4);
const newFocusGroup = layerKey.substr(0, 4);
rememberedCenters[oldFocusGroup] = view.getCenter();
if (rememberedCenters[newFocusGroup] === undefined) {
// refocus the map to 0, 0
view.setCenter([0, 0]);
} else {
// set back to where we were
view.setCenter(rememberedCenters[newFocusGroup]);
}
rememberedZoom[oldFocusGroup] = view.getZoom();
view.setMinZoom(layer.minNativeZoom - config.globalMinZoom);
view.setMaxZoom(layer.maxNativeZoom - config.globalMinZoom);
if (rememberedZoom[newFocusGroup] === undefined) {
// rezoom the map to minimum zoom
view.setZoom(layer.minNativeZoom - config.globalMinZoom);
} else {
// set back to where we were
view.setZoom(rememberedZoom[newFocusGroup]);
}
const radios = $("input[name='layers']");
radios.each(function(idx, elem) {
if (elem.value == layerKey) {
elem.checked = true;
} else {
elem.checked = false;
}
});
currentSelectedLayer = layerKey;
}
};
radioInput.addEventListener(
"click",
selectLayer.bind(this),
false
);
form.appendChild(radioContainer);
});
const hr = document.createElement("hr");
form.appendChild(hr);
locationElement = document.createElement("div");
locationElement.innerText = "X: 0, Z: 0";
form.appendChild(locationElement);
Control.call(this, {
element: element,
target: options.target
});
}
if (Control) PapyrusControls.__proto__ = Control;
PapyrusControls.prototype = Object.create(Control && Control.prototype);
PapyrusControls.prototype.constructor = PapyrusControls;
return PapyrusControls;
})(ol.control.Control);
map = new ol.Map({
target: "map",
layers: tileLayers,
view: view,
controls: [
new ol.control.Zoom(),
new ol.control.Attribution(),
new PapyrusControls()
]
});
map.on("pointermove", function(event) {
var x = Math.floor(
(event.coordinate[0] / zoomRatioForMaximumZoom) *
minecraftTilesAtMostZoomedInLevel
);
var z = Math.floor(
(-event.coordinate[1] / zoomRatioForMaximumZoom) *
minecraftTilesAtMostZoomedInLevel
);
locationElement.innerText = "X: " + x + " Z: " + z;
});
if (typeof(playersData) !== "undefined") {
var playerFeatures = [];
for (var playerIndex in playersData.players)
{
var player = playersData.players[playerIndex];
if (!player.visible) {
continue;
}
if (player.dimensionId !== 0) {
// TODO: Currently I'm only adding player markers who are in the Overworld
// We'll want to show players depending on which dimension is being viewed
// Maybe add a separate layer for players in each dimension
continue;
}
var style = new ol.style.Style({
text: new ol.style.Text({
text: player.name + "\n\uf041", // map-marker
font: "900 18px 'Font Awesome 5 Free'",
textBaseline: "bottom",
fill: new ol.style.Fill({color: player.color}),
stroke: new ol.style.Stroke({color: "white", width: 2})
})
});
var playerFeature = new ol.Feature({
geometry: new ol.geom.Point([
(player.position[0] * zoomRatioForMaximumZoom) / minecraftTilesAtMostZoomedInLevel,
(-player.position[2] * zoomRatioForMaximumZoom) / minecraftTilesAtMostZoomedInLevel
])
});
playerFeature.setStyle(style);
playerFeatures.push(playerFeature);
}
var vectorSource = new ol.source.Vector({
features: playerFeatures
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
map.addLayer(vectorLayer);
}
</script>
</body>
</html>