2019-02-03 20:18:14 +03:00
|
|
|
|
|
|
|
var PlayerIcon = L.icon({
|
|
|
|
iconUrl: 'pics/sam.png',
|
|
|
|
|
|
|
|
iconSize: [16, 32],
|
|
|
|
iconAnchor: [8, 16],
|
|
|
|
popupAnchor: [0, -16]
|
|
|
|
});
|
|
|
|
|
2019-06-11 16:01:16 +03:00
|
|
|
export default L.LayerGroup.extend({
|
2019-02-03 20:18:14 +03:00
|
|
|
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-06-17 08:29:33 +03:00
|
|
|
createPopup: function(player){
|
2019-02-22 18:28:20 +03:00
|
|
|
var html = "<b>" + player.name + "</b>";
|
|
|
|
html += "<hr>";
|
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
for (let i=0; i<Math.floor(player.hp / 2); i++)
|
2019-02-22 18:28:20 +03:00
|
|
|
html += "<img src='pics/heart.png'>";
|
|
|
|
|
|
|
|
if (player.hp % 2 == 1)
|
2019-02-25 10:15:58 +03:00
|
|
|
html += "<img src='pics/heart_half.png'>";
|
2019-02-22 18:28:20 +03:00
|
|
|
|
|
|
|
html += "<br>";
|
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
for (let i=0; i<Math.floor(player.breath / 2); i++)
|
2019-02-22 18:28:20 +03:00
|
|
|
html += "<img src='pics/bubble.png'>";
|
|
|
|
|
|
|
|
if (player.breath % 2 == 1)
|
2019-02-25 10:15:58 +03:00
|
|
|
html += "<img src='pics/bubble_half.png'>";
|
2019-02-22 18:28:20 +03:00
|
|
|
|
2019-06-17 08:32:58 +03:00
|
|
|
html += `
|
|
|
|
<br>
|
2019-06-20 08:17:34 +03:00
|
|
|
<b>RTT:</b> ${Math.floor(player.rtt*1000)} ms
|
2019-06-17 08:32:58 +03:00
|
|
|
<br>
|
|
|
|
<b>Protocol version:</b> ${player.protocol_version}
|
|
|
|
`;
|
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
return html;
|
|
|
|
},
|
|
|
|
|
|
|
|
createMarker: function(player){
|
|
|
|
var marker = L.marker([player.pos.z, player.pos.x], {icon: PlayerIcon});
|
2019-02-22 18:28:20 +03:00
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
marker.bindPopup(this.createPopup(player));
|
2019-02-04 23:15:00 +03:00
|
|
|
return marker;
|
|
|
|
},
|
|
|
|
|
2019-03-22 15:16:28 +03:00
|
|
|
isPlayerInCurrentLayer: function(player){
|
2019-04-04 11:49:55 +03:00
|
|
|
var mapLayer = this.layerMgr.getCurrentLayer();
|
2019-03-22 15:16:28 +03:00
|
|
|
|
2019-04-04 11:49:55 +03:00
|
|
|
return (player.pos.y >= (mapLayer.from*16) && player.pos.y <= (mapLayer.to*16));
|
2019-03-22 15:16:28 +03:00
|
|
|
},
|
|
|
|
|
2019-06-11 16:01:16 +03:00
|
|
|
onMinetestUpdate: function(/*info*/){
|
2019-02-04 18:05:15 +03:00
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
this.players.forEach(player => {
|
|
|
|
var isInLayer = this.isPlayerInCurrentLayer(player);
|
2019-03-22 15:16:28 +03:00
|
|
|
|
|
|
|
if (!isInLayer){
|
2019-06-17 08:29:33 +03:00
|
|
|
if (this.currentObjects[player.name]){
|
2019-03-22 15:16:28 +03:00
|
|
|
//player is displayed and not on the layer anymore
|
|
|
|
//Remove the marker and reference
|
2019-06-17 08:29:33 +03:00
|
|
|
this.currentObjects[player.name].remove();
|
|
|
|
delete this.currentObjects[player.name];
|
2019-03-22 15:16:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
if (this.currentObjects[player.name]){
|
2019-02-04 18:05:15 +03:00
|
|
|
//marker exists
|
2019-06-17 08:29:33 +03:00
|
|
|
let marker = this.currentObjects[player.name];
|
|
|
|
marker.setLatLng([player.pos.z, player.pos.x]);
|
|
|
|
marker.setPopupContent(this.createPopup(player));
|
2019-02-04 23:15:00 +03:00
|
|
|
|
2019-02-04 18:05:15 +03:00
|
|
|
} else {
|
|
|
|
//marker does not exist
|
2019-06-17 08:29:33 +03:00
|
|
|
let marker = this.createMarker(player);
|
|
|
|
marker.addTo(this);
|
2019-02-04 23:15:00 +03:00
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
this.currentObjects[player.name] = marker;
|
2019-02-04 18:05:15 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
Object.keys(this.currentObjects).forEach(existingName => {
|
|
|
|
var playerIsActive = this.players.find(function(p){
|
2019-02-04 18:05:15 +03:00
|
|
|
return p.name == existingName;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!playerIsActive){
|
|
|
|
//player
|
2019-06-17 08:29:33 +03:00
|
|
|
this.currentObjects[existingName].remove();
|
|
|
|
delete this.currentObjects[existingName];
|
2019-02-04 18:05:15 +03:00
|
|
|
}
|
|
|
|
});
|
2019-02-03 20:18:14 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
reDraw: function(){
|
2019-02-04 18:05:15 +03:00
|
|
|
this.currentObjects = {};
|
2019-02-03 20:18:14 +03:00
|
|
|
this.clearLayers();
|
|
|
|
|
2019-04-04 11:49:55 +03:00
|
|
|
var mapLayer = this.layerMgr.getCurrentLayer();
|
2019-02-04 18:05:15 +03:00
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
this.players.forEach(player => {
|
|
|
|
if (!this.isPlayerInCurrentLayer(player)){
|
2019-03-22 15:16:28 +03:00
|
|
|
//not in current layer
|
|
|
|
return;
|
|
|
|
}
|
2019-02-04 18:05:15 +03:00
|
|
|
|
2019-06-17 08:29:33 +03:00
|
|
|
var marker = this.createMarker(player);
|
|
|
|
marker.addTo(this);
|
|
|
|
this.currentObjects[player.name] = marker;
|
2019-02-04 18:05:15 +03:00
|
|
|
});
|
|
|
|
|
2019-02-03 20:18:14 +03:00
|
|
|
},
|
|
|
|
|
2019-06-11 16:01:16 +03:00
|
|
|
onAdd: function(/*map*/) {
|
2019-02-03 20:18:14 +03:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-06-11 16:01:16 +03:00
|
|
|
onRemove: function(/*map*/) {
|
2019-02-03 20:18:14 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|