-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
211 lines (170 loc) · 6.05 KB
/
script.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
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
const attribution = `<a href="https://www.cavesofqud.com/">Caves of Qud</a>`;
const ZLEVEL = 10;
/* Map element */
const mapEl = document.getElementById("map");
/* Zoom level at which we should transition away from the world map
* overlay and switch to tiles. */
const tileZoomThreshold_L1 = 3;
const tileZoomThreshold_L0 = tileZoomThreshold_L1 + 2;
/* Zoom level at which we should apply pixelated image rendering
* for tiles. */
const pixelatedZoomThreshold = 6;
const worldMapPixelWidth = 80 * 16;
const worldMapPixelHeight = 25 * 24;
const tiledMapWidth_L1 = 128;
const tiledMapHeight_L1 = 19;
const tiledMapWidth_L0 = 512;
const tiledMapHeight_L0 = 75;
// const tiledMapPixelWidth = tiledMapWidth_L0 * 16;
// const tiledMapPixelHeight = tiledMapHeight_L0 * 16;
const tiledMapPixelWidth_L1 = tiledMapWidth_L1 * 32;
const tiledMapPixelHeight_L1 = tiledMapHeight_L1 * 32;
function clamp(num, min, max) {
if (max !== null && num > max)
return max;
if (min !== null && num < min)
return min;
return num;
}
var bounds = [[0, 0], [worldMapPixelHeight, worldMapPixelWidth]];
var map = L.map("map", {
crs: L.CRS.Simple,
minZoom: 0,
maxZoom: 10,
zoomSnap: 1,
zoomDelta: 0.5,
center: [worldMapPixelHeight / 2, worldMapPixelWidth / 2]
});
map.fitBounds(bounds);
var startCoords = map.getCenter();
var startZoom = map.getZoom();
map.on("zoomstart", function() {
startCoords = map.getCenter();
startZoom = map.getZoom();
});
map.on("zoomend", function() {
if (map.getZoom() >= tileZoomThreshold_L1 && map.hasLayer(worldMapOverlay)) {
// Based on the previous coordinates, pan to the corresponding location
// in the tiled map.
let coords = map.getCenter();
let fracX = coords.lng / worldMapPixelWidth;
let fracY = coords.lat / worldMapPixelHeight;
let tiledMapX = fracX * tiledMapPixelWidth_L1;
let tiledMapY = fracY * tiledMapPixelHeight_L1;
map.panTo([tiledMapY, tiledMapX], {
animate: false
});
map.removeLayer(worldMapOverlay);
}
if (map.getZoom() < tileZoomThreshold_L1 && !map.hasLayer(worldMapOverlay)) {
// Based on the previous coordinates, pan to the corresponding location
// in the world map.
let coords = map.getCenter();
let fracX = coords.lng / tiledMapPixelWidth_L1;
let fracY = coords.lat / tiledMapPixelHeight_L1;
let worldMapX = fracX * worldMapPixelWidth;
let worldMapY = fracY * worldMapPixelHeight;
map.panTo([worldMapY, worldMapX], {
animate: false
});
map.addLayer(worldMapOverlay);
}
// When transitioning from L1 to L0 there's a small shift that we need to correct for
if (map.getZoom() >= tileZoomThreshold_L0 && startZoom < tileZoomThreshold_L0) {
let panRatio = 1 / 75;
let coords = map.getCenter();
let yDelta = tiledMapPixelHeight_L1 * panRatio;
map.panTo([coords.lat - yDelta, coords.lng], {
animate: false
});
}
if (map.getZoom() < tileZoomThreshold_L0 && startZoom >= tileZoomThreshold_L0) {
let panRatio = 1 / 76;
let coords = map.getCenter();
let yDelta = tiledMapPixelHeight_L1 * panRatio;
map.panTo([coords.lat + yDelta, coords.lng], {
animate: false
});
}
// When we get close enough to the individual tiles, use nearest-neighbor image scaling
// to make the tiles look the same way they look in-game.
//
// We also add nearest-neighbor scaling to the world map for the same resaon.
if (map.getZoom() >= pixelatedZoomThreshold || map.getZoom() < tileZoomThreshold_L1) {
mapEl.classList.add("pixel-perfect");
}
if (map.getZoom() < pixelatedZoomThreshold && map.getZoom() >= tileZoomThreshold_L1) {
mapEl.classList.remove("pixel-perfect");
}
});
/********************* Tile layers ***********************/
var worldMapOverlay = L.imageOverlay("tiles/world.webp", bounds);
worldMapOverlay.addTo(map);
L.TileLayer.Qud1 = L.TileLayer.extend({
options: {
minNativeZoom: tileZoomThreshold_L1,
maxNativeZoom: tileZoomThreshold_L1,
minZoom: tileZoomThreshold_L1,
maxZoom: tileZoomThreshold_L0
},
initialize: function (options) {
L.Util.setOptions(this, options);
},
getTileUrl: function(coords) {
let x = coords.x;
let y = coords.y;
let z = clamp(coords.z - tileZoomThreshold_L1, 0, null);
let normalization = 2**z;
x = Math.floor(x / normalization);
y = tiledMapHeight_L1 + Math.floor(y / normalization);
return `/tiles/tile_1_${x}_${y}_${ZLEVEL}.webp`;
},
getAttribution: function() {
return attribution;
}
});
L.tileLayer.qud1 = function() {
return new L.TileLayer.Qud1();
}
L.tileLayer.qud1().addTo(map);
L.TileLayer.Qud0 = L.TileLayer.extend({
options: {
minNativeZoom: tileZoomThreshold_L0,
maxNativeZoom: tileZoomThreshold_L0,
minZoom: tileZoomThreshold_L0,
},
initialize: function (options) {
L.Util.setOptions(this, options);
},
getTileUrl: function(coords) {
let x = coords.x;
let y = coords.y;
let z = clamp(coords.z - tileZoomThreshold_L0, 0, null);
let normalization = 2**z;
x = Math.floor(x / normalization);
y = tiledMapHeight_L0 + Math.floor(y / normalization);
const url = `/tiles/tile_0_${x}_${y}_${ZLEVEL}.webp`;
return url;
},
getAttribution: function() {
return attribution;
}
});
L.tileLayer.qud0 = function() {
return new L.TileLayer.Qud0();
}
L.tileLayer.qud0().addTo(map);
/*
L.GridLayer.DebugCoords = L.GridLayer.extend({
createTile: function (coords) {
let tile = document.createElement('div');
tile.innerHTML = [coords.x, coords.y, coords.z].join(', ');
tile.style.outline = '1px solid red';
return tile;
}
});
L.gridLayer.debugCoords = function(opts) {
return new L.GridLayer.DebugCoords(opts);
};
map.addLayer( L.gridLayer.debugCoords() );
*/