1
0
forked from MTSR/mapserver
mapserver/public/js/map/overlays/MinecartOverlay.js

119 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-09-18 12:53:58 +03:00
import wsChannel from '../../WebSocketChannel.js';
import layerMgr from '../../LayerManager.js';
let minecarts = [];
//update minecarts all the time
wsChannel.addListener("minetest-info", function(info){
minecarts = info.minecarts || [];
});
2019-04-24 17:21:04 +03:00
2019-06-11 16:01:16 +03:00
export default L.LayerGroup.extend({
2019-09-18 12:53:58 +03:00
initialize: function() {
2019-04-24 17:21:04 +03:00
L.LayerGroup.prototype.initialize.call(this);
this.currentObjects = {}; // name => marker
this.reDraw = this.reDraw.bind(this);
this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
},
createMarker: function(cart){
var Icon = L.icon({
iconUrl: "pics/minecart_logo.png",
iconSize: [32, 32],
iconAnchor: [16, 16],
popupAnchor: [0, -32]
});
var marker = L.marker([cart.pos.z, cart.pos.x], {icon: Icon});
var html = "<b>Minecart</b><hr>";
2019-05-04 22:16:57 +03:00
html += "<b>Id: </b> " + cart.id + "<br>";
2019-04-24 17:21:04 +03:00
marker.bindPopup(html);
return marker;
},
isCartInCurrentLayer: function(cart){
2019-09-18 12:53:58 +03:00
var mapLayer = layerMgr.getCurrentLayer();
2019-04-24 17:21:04 +03:00
return (cart.pos.y >= (mapLayer.from*16) && cart.pos.y <= (mapLayer.to*16));
},
2019-06-11 16:01:16 +03:00
onMinetestUpdate: function(/*info*/){
2019-04-24 17:21:04 +03:00
var self = this;
2019-09-18 12:53:58 +03:00
minecarts.forEach(function(cart){
2019-04-24 17:21:04 +03:00
var isInLayer = self.isCartInCurrentLayer(cart);
if (!isInLayer){
2019-05-04 22:16:57 +03:00
if (self.currentObjects[cart.id]){
//cart is displayed and not on the layer anymore
2019-04-24 17:21:04 +03:00
//Remove the marker and reference
2019-05-04 22:16:57 +03:00
self.currentObjects[cart.id].remove();
delete self.currentObjects[cart.id];
2019-04-24 17:21:04 +03:00
}
return;
}
2019-05-04 22:16:57 +03:00
if (self.currentObjects[cart.id]){
2019-04-24 17:21:04 +03:00
//marker exists
2019-05-04 22:16:57 +03:00
self.currentObjects[cart.id].setLatLng([cart.pos.z, cart.pos.x]);
2019-04-24 17:21:04 +03:00
//setPopupContent
} else {
//marker does not exist
2019-05-04 22:16:57 +03:00
var marker = self.createMarker(cart);
2019-04-24 17:21:04 +03:00
marker.addTo(self);
2019-05-04 22:16:57 +03:00
self.currentObjects[cart.id] = marker;
2019-04-24 17:21:04 +03:00
}
});
Object.keys(self.currentObjects).forEach(function(existingId){
2019-09-18 12:53:58 +03:00
var cartIsActive = minecarts.find(function(t){
2019-04-24 17:21:04 +03:00
return t.id == existingId;
});
2019-05-04 22:16:57 +03:00
if (!cartIsActive){
2019-04-24 17:21:04 +03:00
self.currentObjects[existingId].remove();
delete self.currentObjects[existingId];
}
});
},
reDraw: function(){
var self = this;
this.currentObjects = {};
this.clearLayers();
2019-09-18 12:53:58 +03:00
minecarts.forEach(function(cart){
2019-05-04 22:16:57 +03:00
if (!self.isCartInCurrentLayer(cart)){
2019-04-24 17:21:04 +03:00
//not in current layer
return;
}
2019-05-04 22:16:57 +03:00
var marker = self.createMarker(cart);
2019-04-24 17:21:04 +03:00
marker.addTo(self);
2019-05-04 22:16:57 +03:00
self.currentObjects[cart.id] = marker;
2019-04-24 17:21:04 +03:00
});
},
2019-06-11 16:01:16 +03:00
onAdd: function(/*map*/) {
2019-09-18 12:53:58 +03:00
wsChannel.addListener("minetest-info", this.onMinetestUpdate);
2019-04-24 17:21:04 +03:00
this.reDraw();
},
2019-06-11 16:01:16 +03:00
onRemove: function(/*map*/) {
2019-04-24 17:21:04 +03:00
this.clearLayers();
2019-09-18 12:53:58 +03:00
wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
2019-04-24 17:21:04 +03:00
}
});