1 | var REGIONSIZE = 512;
|
---|
2 | var CHUNKSIZE = 16;
|
---|
3 | var TILESIZE = 128;
|
---|
4 | var MAXZOOM = 4;
|
---|
5 |
|
---|
6 | SDTD_Projection = {
|
---|
7 | project: function (latlng) {
|
---|
8 | return new L.Point(latlng.lng / 16, latlng.lat / 16);
|
---|
9 | },
|
---|
10 |
|
---|
11 | unproject: function (point) {
|
---|
12 | return new L.LatLng(point.y * 16, point.x * 16);
|
---|
13 | }
|
---|
14 | };
|
---|
15 |
|
---|
16 | SDTD_CRS = L.extend({}, L.CRS.Simple, {
|
---|
17 | projection: SDTD_Projection,
|
---|
18 | transformation: new L.Transformation(1, 0, -1, 0),
|
---|
19 |
|
---|
20 | scale: function (zoom) {
|
---|
21 | return Math.pow(2, zoom);
|
---|
22 | }
|
---|
23 | });
|
---|
24 |
|
---|
25 | var CoordToChunk = function(latlng) {
|
---|
26 | var x = Math.floor(((latlng.lng + 16777216) / CHUNKSIZE) - (16777216 / CHUNKSIZE));
|
---|
27 | var y = Math.floor(((latlng.lat + 16777216) / CHUNKSIZE) - (16777216 / CHUNKSIZE));
|
---|
28 | return L.latLng(y, x);
|
---|
29 | }
|
---|
30 |
|
---|
31 | var CoordToRegion = function(latlng) {
|
---|
32 | var x = Math.floor(((latlng.lng + 16777216) / REGIONSIZE) - (16777216 / REGIONSIZE));
|
---|
33 | var y = Math.floor(((latlng.lat + 16777216) / REGIONSIZE) - (16777216 / REGIONSIZE));
|
---|
34 | return L.latLng(y, x);
|
---|
35 | }
|
---|
36 |
|
---|
37 | var FormatCoord = function(latlng) {
|
---|
38 | return Math.abs(latlng.lat)+ (latlng.lat>=0 ? " N" : " S") + " / " + Math.abs(latlng.lng) + (latlng.lng>=0 ? " E" : " W");
|
---|
39 | }
|
---|
40 |
|
---|
41 | var FormatRegionFileName = function(latlng) {
|
---|
42 | return "r." + latlng.lng + "." + latlng.lat + ".7rg";
|
---|
43 | }
|
---|
44 |
|
---|
45 | var map = L.map('map', {
|
---|
46 | zoomControl: true,
|
---|
47 | attributionControl: false,
|
---|
48 | crs: SDTD_CRS
|
---|
49 | }).setView([0, 0], 0);
|
---|
50 |
|
---|
51 | var tileLayer = L.tileLayer('../../map/{z}/{x}/{y}.png?t={time}', {
|
---|
52 | maxZoom: MAXZOOM+1,
|
---|
53 | minZoom: 0,
|
---|
54 | maxNativeZoom: MAXZOOM,
|
---|
55 | tileSize: TILESIZE,
|
---|
56 | continuousWorld: true,
|
---|
57 | tms: true,
|
---|
58 | unloadInvisibleTiles: true,
|
---|
59 | time: function() { return new Date().getTime(); }
|
---|
60 | });
|
---|
61 |
|
---|
62 | var regionLayer = L.tileLayer.canvas({
|
---|
63 | maxZoom: MAXZOOM+1,
|
---|
64 | minZoom: 0,
|
---|
65 | maxNativeZoom: MAXZOOM+1,
|
---|
66 | tileSize: TILESIZE,
|
---|
67 | continuousWorld: true
|
---|
68 | });
|
---|
69 |
|
---|
70 | regionLayer.drawTile = function(canvas, tilePoint, zoom) {
|
---|
71 | var blockWorldSize = TILESIZE * Math.pow(2, MAXZOOM-zoom);
|
---|
72 | var tileLeft = tilePoint.x * blockWorldSize;
|
---|
73 | var tileBottom = (-1-tilePoint.y) * blockWorldSize;
|
---|
74 | var blockPos = L.latLng(tileBottom, tileLeft);
|
---|
75 |
|
---|
76 | var ctx = canvas.getContext('2d');
|
---|
77 |
|
---|
78 | ctx.strokeStyle = "lightblue";
|
---|
79 | ctx.fillStyle = "lightblue";
|
---|
80 | ctx.lineWidth = 1;
|
---|
81 | ctx.font="14px Arial";
|
---|
82 |
|
---|
83 | var lineCount = blockWorldSize / REGIONSIZE;
|
---|
84 | if (lineCount >= 1) {
|
---|
85 | var pos = 0;
|
---|
86 | while (pos < TILESIZE) {
|
---|
87 | // Vertical
|
---|
88 | ctx.beginPath();
|
---|
89 | ctx.moveTo(pos, 0);
|
---|
90 | ctx.lineTo(pos, TILESIZE);
|
---|
91 | ctx.stroke();
|
---|
92 |
|
---|
93 | // Horizontal
|
---|
94 | ctx.beginPath();
|
---|
95 | ctx.moveTo(0, pos);
|
---|
96 | ctx.lineTo(TILESIZE, pos);
|
---|
97 | ctx.stroke();
|
---|
98 |
|
---|
99 | pos += TILESIZE / lineCount;
|
---|
100 | }
|
---|
101 | ctx.fillText(FormatRegionFileName(CoordToRegion(blockPos)), 4, TILESIZE-5);
|
---|
102 | } else {
|
---|
103 | if ((tileLeft % REGIONSIZE) == 0) {
|
---|
104 | // Vertical
|
---|
105 | ctx.beginPath();
|
---|
106 | ctx.moveTo(0, 0);
|
---|
107 | ctx.lineTo(0, TILESIZE);
|
---|
108 | ctx.stroke();
|
---|
109 | }
|
---|
110 | if ((tileBottom % REGIONSIZE) == 0) {
|
---|
111 | // Horizontal
|
---|
112 | ctx.beginPath();
|
---|
113 | ctx.moveTo(0, TILESIZE);
|
---|
114 | ctx.lineTo(TILESIZE, TILESIZE);
|
---|
115 | ctx.stroke();
|
---|
116 | }
|
---|
117 | if ((tileLeft % REGIONSIZE) == 0 && (tileBottom % REGIONSIZE) == 0) {
|
---|
118 | ctx.fillText(FormatRegionFileName(CoordToRegion(blockPos)), 4, TILESIZE-5);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | var pos = tileLeft;
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 | var playersOnlineMarkerGroup = L.layerGroup();
|
---|
127 | var playersOfflineMarkerGroup = L.layerGroup();
|
---|
128 |
|
---|
129 | var baseLayers = {
|
---|
130 | //"Map": tileLayer
|
---|
131 | };
|
---|
132 |
|
---|
133 | var overlays = {
|
---|
134 | "Region files": regionLayer,
|
---|
135 | "Players (online)" : playersOnlineMarkerGroup,
|
---|
136 | "Players (offline)" : playersOfflineMarkerGroup
|
---|
137 | };
|
---|
138 |
|
---|
139 | tileLayer.addTo(map);
|
---|
140 | L.control.layers(baseLayers, overlays, {
|
---|
141 | collapsed: false
|
---|
142 | }).addTo(map);
|
---|
143 |
|
---|
144 | map.on('mousemove', function(e) {
|
---|
145 | L.DomUtil.get('pos').textContent = FormatCoord(e.latlng);
|
---|
146 | });
|
---|
147 |
|
---|
148 | var playersMappingList = {};
|
---|
149 |
|
---|
150 | var setPlayerMarkers = function(data) {
|
---|
151 | $.each( data, function( key, val ) {
|
---|
152 | var marker;
|
---|
153 | if (playersMappingList.hasOwnProperty(val.steamid)) {
|
---|
154 | marker = playersMappingList[val.steamid].currentPosMarker;
|
---|
155 | marker.setLatLng([val.position.z, val.position.x]);
|
---|
156 | } else {
|
---|
157 | marker = L.marker([val.position.z, val.position.x]).bindPopup(val.name);
|
---|
158 | playersOnlineMarkerGroup.addLayer(marker);
|
---|
159 | playersMappingList[val.steamid] = { online: !val.online };
|
---|
160 | }
|
---|
161 | if (playersMappingList[val.steamid].online != val.online) {
|
---|
162 | if (val.online) {
|
---|
163 | marker.setOpacity(1.0);
|
---|
164 | playersOfflineMarkerGroup.removeLayer(marker);
|
---|
165 | playersOnlineMarkerGroup.addLayer(marker);
|
---|
166 | } else {
|
---|
167 | marker.setOpacity(0.5);
|
---|
168 | playersOnlineMarkerGroup.removeLayer(marker);
|
---|
169 | playersOfflineMarkerGroup.addLayer(marker);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | val.currentPosMarker = marker;
|
---|
173 | playersMappingList[val.steamid] = val;
|
---|
174 | });
|
---|
175 | }
|
---|
176 |
|
---|
177 | var updatePlayerEvent = function() {
|
---|
178 | $.getJSON( "../api/getplayerslocation")
|
---|
179 | .done(setPlayerMarkers)
|
---|
180 | .fail(function() {
|
---|
181 | console.log("Error fetching players list");
|
---|
182 | })
|
---|
183 | .always(function() {
|
---|
184 | window.setTimeout(updatePlayerEvent, 2000);
|
---|
185 | });
|
---|
186 | }
|
---|
187 |
|
---|
188 | window.setTimeout(updatePlayerEvent, 500);
|
---|
189 |
|
---|