2015-02-02 09:08:36 +01:00
|
|
|
var MinedMapLayer = L.GridLayer.extend({
|
2015-02-03 20:15:58 +01:00
|
|
|
initialize: function (mipmaps, layer) {
|
2015-02-04 06:35:31 +01:00
|
|
|
this.mipmaps = mipmaps;
|
|
|
|
this.layer = layer;
|
|
|
|
|
|
|
|
this.zoomOffset = L.Browser.retina ? 1 : 0;
|
2015-02-02 09:08:36 +01:00
|
|
|
|
2015-02-02 09:34:38 +01:00
|
|
|
this.options.attribution = 'Generated by <a href="http://git.universe-factory.net/MinedMap/">MinedMap</a>';
|
|
|
|
|
2015-02-02 09:08:36 +01:00
|
|
|
if (!L.Browser.android) {
|
|
|
|
this.on('tileunload', this._onTileRemove);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
createTile: function (coords, done) {
|
|
|
|
var tile = document.createElement('img');
|
|
|
|
|
|
|
|
tile.onload = L.bind(this._tileOnLoad, this, done, tile);
|
|
|
|
tile.onerror = L.bind(this._tileOnError, this, done, tile);
|
|
|
|
|
|
|
|
tile.alt = '';
|
|
|
|
|
2015-02-04 06:35:31 +01:00
|
|
|
var z = -(coords.z + this.zoomOffset);
|
2015-02-03 20:15:58 +01:00
|
|
|
if (z < 0)
|
|
|
|
z = 0;
|
|
|
|
|
2015-02-04 06:35:31 +01:00
|
|
|
var mipmap = this.mipmaps[z];
|
2015-02-03 20:15:58 +01:00
|
|
|
|
|
|
|
if (coords.x >= mipmap.info.minX && coords.x <= mipmap.info.maxX &&
|
|
|
|
coords.y >= mipmap.info.minZ && coords.y <= mipmap.info.maxZ &&
|
|
|
|
mipmap.regions[coords.y-mipmap.info.minZ][coords.x-mipmap.info.minX])
|
2015-02-04 06:35:31 +01:00
|
|
|
tile.src = 'data/'+this.layer+'/'+z+'/r.'+coords.x+'.'+coords.y+'.png';
|
2015-02-03 20:15:58 +01:00
|
|
|
|
2015-02-04 06:51:33 +01:00
|
|
|
if (z == 0)
|
2015-02-03 20:15:58 +01:00
|
|
|
L.DomUtil.addClass(tile, 'overzoomed');
|
2015-02-02 09:08:36 +01:00
|
|
|
|
|
|
|
return tile;
|
|
|
|
},
|
|
|
|
|
|
|
|
_tileOnLoad: function (done, tile) {
|
|
|
|
done(null, tile);
|
|
|
|
},
|
|
|
|
|
|
|
|
_tileOnError: function (done, tile, e) {
|
|
|
|
done(e, tile);
|
|
|
|
},
|
|
|
|
|
|
|
|
_getTileSize: function () {
|
2015-02-04 06:35:31 +01:00
|
|
|
var map = this._map, zoom = map.getZoom() + this.zoomOffset;
|
|
|
|
|
|
|
|
var base = (L.Browser.retina ? 256 : 512);
|
2015-02-02 10:53:23 +01:00
|
|
|
|
|
|
|
if (zoom > 0)
|
2015-02-04 06:35:31 +01:00
|
|
|
return Math.round(map.getZoomScale(zoom, 0) * base);
|
2015-02-02 10:53:23 +01:00
|
|
|
else
|
2015-02-04 06:35:31 +01:00
|
|
|
return base;
|
2015-02-02 09:08:36 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
_onTileRemove: function (e) {
|
|
|
|
e.tile.onload = null;
|
|
|
|
e.tile.src = L.Util.emptyImageUrl;
|
|
|
|
},
|
|
|
|
|
|
|
|
_abortLoading: function () {
|
|
|
|
var i, tile;
|
|
|
|
for (i in this._tiles) {
|
|
|
|
tile = this._tiles[i].el;
|
|
|
|
|
|
|
|
tile.onload = L.Util.falseFn;
|
|
|
|
tile.onerror = L.Util.falseFn;
|
|
|
|
|
|
|
|
if (!tile.complete) {
|
|
|
|
tile.src = L.Util.emptyImageUrl;
|
|
|
|
L.DomUtil.remove(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
window.createMap = function () {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.onload = function () {
|
2015-02-02 18:39:44 +01:00
|
|
|
var res = JSON.parse(this.responseText),
|
2015-02-03 20:15:58 +01:00
|
|
|
mipmaps = res.mipmaps,
|
2015-02-02 18:39:44 +01:00
|
|
|
spawn = res.spawn;
|
2015-02-02 09:08:36 +01:00
|
|
|
|
|
|
|
var map = L.map('map', {
|
2015-02-02 18:39:44 +01:00
|
|
|
center: [-spawn.z, spawn.x],
|
2015-02-02 09:58:41 +01:00
|
|
|
zoom: 0,
|
2015-02-03 20:15:58 +01:00
|
|
|
minZoom: -(mipmaps.length-1),
|
2015-02-02 14:18:04 +01:00
|
|
|
maxZoom: 3,
|
2015-02-02 09:08:36 +01:00
|
|
|
crs: L.CRS.Simple,
|
|
|
|
maxBounds: [
|
2015-02-03 20:15:58 +01:00
|
|
|
[-512*(mipmaps[0].info.maxZ+1), 512*mipmaps[0].info.minX],
|
|
|
|
[-512*mipmaps[0].info.minZ, 512*(mipmaps[0].info.maxX+1)],
|
2015-02-02 09:08:36 +01:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2015-02-03 20:15:58 +01:00
|
|
|
var mapLayer = new MinedMapLayer(mipmaps, 'map');
|
|
|
|
var lightLayer = new MinedMapLayer(mipmaps, 'light');
|
2015-02-03 13:17:36 +01:00
|
|
|
|
|
|
|
mapLayer.addTo(map);
|
|
|
|
|
|
|
|
var overlayMaps = {
|
|
|
|
"Illumination": lightLayer,
|
|
|
|
};
|
|
|
|
|
|
|
|
L.control.layers({}, overlayMaps).addTo(map);
|
2015-02-02 09:08:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.open('GET', 'data/info.json', true);
|
|
|
|
xhr.send();
|
|
|
|
}
|