1
0
forked from MTSR/mapserver
mapserver/server/static/js/overlays/TrainOverlay.js

164 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-02-20 16:17:37 +03:00
'use strict';
2019-02-24 22:06:20 +03:00
function getTrainImageUrlForType(type){
switch(type){
case "advtrains:subway_wagon":
return "pics/advtrains/advtrains_subway_wagon_inv.png";
case "advtrains:engine_japan":
return "pics/advtrains/advtrains_engine_japan_inv.png";
case "advtrains:engine_steam":
return "pics/advtrains/advtrains_engine_steam_inv.png";
case "advtrains:engine_industrial":
return "pics/advtrains/advtrains_engine_industrial_inv.png";
case "advtrains:wagon_wood":
return "pics/advtrains/advtrains_wagon_wood_inv.png";
case "advtrains:wagon_box":
return "pics/advtrains/advtrains_wagon_box_inv.png";
default:
//TODO: fallback image
return "pics/advtrains/advtrains_subway_wagon_inv.png";
}
}
2019-02-20 16:17:37 +03:00
var TrainOverlay = L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr) {
L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.currentObjects = {}; // name => marker
this.trains = [];
this.reDraw = this.reDraw.bind(this);
this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
//update players all the time
this.wsChannel.addListener("minetest-info", function(info){
this.trains = info.trains || [];
}.bind(this));
},
createMarker: function(train){
2019-02-24 22:06:20 +03:00
//search for wagin in front (whatever "front" is...)
var type;
var lowest_pos = 100;
train.wagons.forEach(function(w){
if (w.pos_in_train < lowest_pos){
lowest_pos = w.pos_in_train;
type = w.type;
}
});
var Icon = L.icon({
iconUrl: getTrainImageUrlForType(type),
iconSize: [16, 16],
iconAnchor: [8, 8],
popupAnchor: [0, -16]
});
var marker = L.marker([train.pos.z, train.pos.x], {icon: Icon});
var html = "<b>Train</b><hr>";
html += "<b>Name:</b> " + train.text_outside + "<br>";
html += "<b>Line:</b> " + train.line + "<br>";
html += "<b>Velocity:</b> "+ Math.floor(train.velocity*10)/10 + "<br>";
html += "<b>Composition: </b>";
train.wagons.forEach(function(w){
var iconUrl = getTrainImageUrlForType(w.type);
html += "<img src='"+iconUrl+"'>";
});
marker.bindPopup(html);
2019-02-20 16:17:37 +03:00
return marker;
},
2019-03-31 19:14:21 +03:00
isTrainInCurrentLayer: function(train){
var mapLayer = this.layerMgr.getCurrentLayer()
return (train.pos.y >= (mapLayer.from*16) && train.pos.y <= (mapLayer.to*16))
},
2019-02-20 16:17:37 +03:00
onMinetestUpdate: function(info){
var self = this;
this.trains.forEach(function(train){
2019-03-31 19:14:21 +03:00
var isInLayer = self.isTrainInCurrentLayer(train);
if (!isInLayer){
if (self.currentObjects[train.id]){
//train is displayed and not on the layer anymore
//Remove the marker and reference
self.currentObjects[train.id].remove();
delete self.currentObjects[train.id];
}
return;
}
2019-02-20 16:17:37 +03:00
if (self.currentObjects[train.id]){
//marker exists
self.currentObjects[train.id].setLatLng([train.pos.z, train.pos.x]);
//setPopupContent
} else {
//marker does not exist
var marker = self.createMarker(train);
marker.addTo(self);
self.currentObjects[train.id] = marker;
}
});
Object.keys(self.currentObjects).forEach(function(existingId){
var trainIsActive = self.trains.find(function(t){
return t.id == existingId;
});
if (!trainIsActive){
//train
self.currentObjects[existingId].remove();
delete self.currentObjects[existingId];
}
});
},
reDraw: function(){
var self = this;
this.currentObjects = {};
this.clearLayers();
var mapLayer = this.layerMgr.getCurrentLayer()
this.trains.forEach(function(train){
2019-03-31 19:14:21 +03:00
if (!self.isPlayerInCurrentLayer(train)){
//not in current layer
return;
}
2019-02-20 16:17:37 +03:00
var marker = self.createMarker(train);
marker.addTo(self);
self.currentObjects[train.id] = marker;
});
},
onAdd: function(map) {
this.layerMgr.addListener(this.reDraw);
this.wsChannel.addListener("minetest-info", this.onMinetestUpdate);
this.reDraw();
},
onRemove: function(map) {
this.clearLayers();
this.layerMgr.removeListener(this.reDraw);
this.wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
}
});