2019-02-03 20:18:14 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var PlayerIcon = L.icon({
|
|
|
|
iconUrl: 'pics/sam.png',
|
|
|
|
|
|
|
|
iconSize: [16, 32],
|
|
|
|
iconAnchor: [8, 16],
|
|
|
|
popupAnchor: [0, -16]
|
|
|
|
});
|
|
|
|
|
|
|
|
var PlayerOverlay = L.LayerGroup.extend({
|
|
|
|
initialize: function(wsChannel, layerMgr) {
|
|
|
|
L.LayerGroup.prototype.initialize.call(this);
|
|
|
|
|
|
|
|
this.layerMgr = layerMgr;
|
|
|
|
this.wsChannel = wsChannel;
|
|
|
|
|
2019-02-04 18:05:15 +03:00
|
|
|
this.currentObjects = {}; // name => marker
|
|
|
|
this.players = [];
|
2019-02-03 20:18:14 +03:00
|
|
|
|
|
|
|
this.reDraw = this.reDraw.bind(this);
|
2019-02-04 17:22:56 +03:00
|
|
|
this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
|
2019-02-04 18:05:15 +03:00
|
|
|
|
|
|
|
//update players all the time
|
|
|
|
this.wsChannel.addListener("minetest-info", function(info){
|
|
|
|
this.players = info.players || [];
|
|
|
|
}.bind(this));
|
2019-02-03 20:18:14 +03:00
|
|
|
},
|
|
|
|
|
2019-02-04 23:15:00 +03:00
|
|
|
createMarker: function(player){
|
|
|
|
var marker = L.marker([player.pos.z, player.pos.x], {icon: PlayerIcon});
|
|
|
|
marker.bindPopup(player.name);
|
|
|
|
|
|
|
|
return marker;
|
|
|
|
},
|
|
|
|
|
2019-02-03 20:18:14 +03:00
|
|
|
onMinetestUpdate: function(info){
|
2019-02-04 18:05:15 +03:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.players.forEach(function(player){
|
|
|
|
if (self.currentObjects[player.name]){
|
|
|
|
//marker exists
|
2019-02-04 23:15:00 +03:00
|
|
|
self.currentObjects[player.name].setLatLng([player.pos.z, player.pos.x]);
|
|
|
|
//setPopupContent
|
|
|
|
|
2019-02-04 18:05:15 +03:00
|
|
|
} else {
|
|
|
|
//marker does not exist
|
2019-02-04 23:15:00 +03:00
|
|
|
var marker = self.createMarker(player);
|
|
|
|
marker.addTo(self);
|
|
|
|
|
|
|
|
self.currentObjects[player.name] = marker;
|
2019-02-04 18:05:15 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(self.currentObjects).forEach(function(existingName){
|
2019-02-04 23:15:00 +03:00
|
|
|
var playerIsActive = self.players.find(function(p){
|
2019-02-04 18:05:15 +03:00
|
|
|
return p.name == existingName;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!playerIsActive){
|
|
|
|
//player
|
2019-02-04 23:15:00 +03:00
|
|
|
self.currentObjects[existingName].remove();
|
|
|
|
delete self.currentObjects[existingName];
|
2019-02-04 18:05:15 +03:00
|
|
|
}
|
|
|
|
});
|
2019-02-03 20:18:14 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
reDraw: function(){
|
|
|
|
var self = this;
|
2019-02-04 18:05:15 +03:00
|
|
|
this.currentObjects = {};
|
2019-02-03 20:18:14 +03:00
|
|
|
this.clearLayers();
|
|
|
|
|
|
|
|
var mapLayer = this.layerMgr.getCurrentLayer()
|
2019-02-04 18:05:15 +03:00
|
|
|
|
|
|
|
this.players.forEach(function(player){
|
|
|
|
//TODO: filter layer
|
|
|
|
|
2019-02-04 23:15:00 +03:00
|
|
|
var marker = self.createMarker(player);
|
|
|
|
marker.addTo(self);
|
2019-02-04 18:05:15 +03:00
|
|
|
self.currentObjects[player.name] = marker;
|
|
|
|
});
|
|
|
|
|
2019-02-03 20:18:14 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onAdd: function(map) {
|
|
|
|
this.layerMgr.addListener(this.reDraw);
|
2019-02-04 17:22:56 +03:00
|
|
|
this.wsChannel.addListener("minetest-info", this.onMinetestUpdate);
|
|
|
|
this.reDraw();
|
2019-02-03 20:18:14 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onRemove: function(map) {
|
|
|
|
this.clearLayers();
|
|
|
|
this.layerMgr.removeListener(this.reDraw);
|
2019-02-04 17:22:56 +03:00
|
|
|
this.wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
|
2019-02-03 20:18:14 +03:00
|
|
|
}
|
|
|
|
});
|