summaryrefslogtreecommitdiffstats
path: root/viewer/MinedMap.js
blob: 959e3ed66ec2b5ab9c9182245c8ca791263486d3 (plain)
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
var MinedMapLayer = L.GridLayer.extend({
	initialize: function (info, regions) {
		this._info = info;
		this._regions = regions;

		this.options.attribution = 'Generated by <a href="http://git.universe-factory.net/MinedMap/">MinedMap</a>';

		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 = '';

		if (coords.x >= this._info.minX && coords.x <= this._info.maxX &&
		    coords.y >= this._info.minZ && coords.y <= this._info.maxZ &&
		    this._regions[coords.y-this._info.minZ][coords.x-this._info.minX])
			tile.src = 'data/r.'+coords.x+'.'+coords.y+'.png';

		return tile;
	},

	_tileOnLoad: function (done, tile) {
		done(null, tile);
	},

	_tileOnError: function (done, tile, e) {
		done(e, tile);
	},

	_getTileSize: function () {
		var map = this._map, zoom = map.getZoom();

		if (zoom > 0)
			return Math.round(map.getZoomScale(map.getZoom(), 0) * 512);
		else
			return 512;
	},

	_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 () {
		var res = JSON.parse(this.responseText),
		    info = res.info,
		    regions = res.regions,
		    spawn = res.spawn;

		var map = L.map('map', {
			center: [-spawn.z, spawn.x],
			zoom: 0,
			minZoom: 0,
			maxZoom: 3,
			crs: L.CRS.Simple,
			maxBounds: [
				[-512*(info.maxZ+1), 512*info.minX],
				[-512*info.minZ, 512*(info.maxX+1)],
			],
		});

		(new MinedMapLayer(info, regions)).addTo(map);
	};

	xhr.open('GET', 'data/info.json', true);
	xhr.send();
}