2019-09-18 11:53:58 +02:00
|
|
|
import wsChannel from '../../WebSocketChannel.js';
|
|
|
|
import layerMgr from '../../LayerManager.js';
|
|
|
|
|
|
|
|
let players = [];
|
|
|
|
|
|
|
|
//update players all the time
|
|
|
|
wsChannel.addListener("minetest-info", function(info){
|
|
|
|
players = info.players || [];
|
|
|
|
});
|
2019-02-03 18:18:14 +01:00
|
|
|
|
|
|
|
var PlayerIcon = L.icon({
|
|
|
|
iconUrl: 'pics/sam.png',
|
|
|
|
|
|
|
|
iconSize: [16, 32],
|
|
|
|
iconAnchor: [8, 16],
|
|
|
|
popupAnchor: [0, -16]
|
|
|
|
});
|
|
|
|
|
2019-06-11 15:01:16 +02:00
|
|
|
export default L.LayerGroup.extend({
|
2019-09-18 11:53:58 +02:00
|
|
|
initialize: function() {
|
2019-02-03 18:18:14 +01:00
|
|
|
L.LayerGroup.prototype.initialize.call(this);
|
|
|
|
|
2019-02-04 16:05:15 +01:00
|
|
|
this.currentObjects = {}; // name => marker
|
2019-02-03 18:18:14 +01:00
|
|
|
|
|
|
|
this.reDraw = this.reDraw.bind(this);
|
2019-02-04 15:22:56 +01:00
|
|
|
this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
|
2019-02-03 18:18:14 +01:00
|
|
|
},
|
|
|
|
|
2019-06-17 07:29:33 +02:00
|
|
|
createPopup: function(player){
|
2022-12-13 20:40:26 +02:00
|
|
|
let html = "<b>" + player.name + "</b>";
|
2019-02-22 16:28:20 +01:00
|
|
|
html += "<hr>";
|
|
|
|
|
2019-06-17 07:29:33 +02:00
|
|
|
for (let i=0; i<Math.floor(player.hp / 2); i++)
|
2019-02-22 16:28:20 +01:00
|
|
|
html += "<img src='pics/heart.png'>";
|
|
|
|
|
|
|
|
if (player.hp % 2 == 1)
|
2019-02-25 08:15:58 +01:00
|
|
|
html += "<img src='pics/heart_half.png'>";
|
2019-02-22 16:28:20 +01:00
|
|
|
|
|
|
|
html += "<br>";
|
|
|
|
|
2019-06-17 07:29:33 +02:00
|
|
|
for (let i=0; i<Math.floor(player.breath / 2); i++)
|
2019-02-22 16:28:20 +01:00
|
|
|
html += "<img src='pics/bubble.png'>";
|
|
|
|
|
|
|
|
if (player.breath % 2 == 1)
|
2019-02-25 08:15:58 +01:00
|
|
|
html += "<img src='pics/bubble_half.png'>";
|
2019-02-22 16:28:20 +01:00
|
|
|
|
2019-06-17 07:32:58 +02:00
|
|
|
html += `
|
|
|
|
<br>
|
2019-06-20 07:17:34 +02:00
|
|
|
<b>RTT:</b> ${Math.floor(player.rtt*1000)} ms
|
2019-06-17 07:32:58 +02:00
|
|
|
<br>
|
|
|
|
<b>Protocol version:</b> ${player.protocol_version}
|
|
|
|
`;
|
|
|
|
|
2019-06-17 07:29:33 +02:00
|
|
|
return html;
|
|
|
|
},
|
|
|
|
|
|
|
|
createMarker: function(player){
|
2022-12-13 20:40:26 +02:00
|
|
|
const marker = L.marker([player.pos.z, player.pos.x], {icon: this.getIcon(player)});
|
2019-02-22 16:28:20 +01:00
|
|
|
|
2019-06-17 07:29:33 +02:00
|
|
|
marker.bindPopup(this.createPopup(player));
|
2019-02-04 21:15:00 +01:00
|
|
|
return marker;
|
|
|
|
},
|
|
|
|
|
2022-12-13 20:40:26 +02:00
|
|
|
getIcon: function(player) {
|
|
|
|
/*
|
|
|
|
compatibility with mapserver_mod without `yaw` attribute - value will be 0.
|
|
|
|
if a player manages to look exactly north, the indicator will also disappear
|
|
|
|
but aligning view at 0.0 is difficult/unlikely during normal gameplay.
|
|
|
|
*/
|
|
|
|
if (player.yaw === 0) return PlayerIcon;
|
|
|
|
|
|
|
|
const icon = 'pics/sam.png';
|
|
|
|
const indicator = player.velocity.x !== 0 || player.velocity.z !== 0 ? 'pics/sam_dir_move.png' : 'pics/sam_dir.png';
|
|
|
|
return L.divIcon({
|
|
|
|
html: `<div style="display:inline-block;width:48px;height:48px">
|
|
|
|
<img src="${icon}" style="position:absolute;top:8px;left:16px;width:16px;height:32px;" alt="${player.name}">
|
|
|
|
<img src="${indicator}" style="position:absolute;top:0;left:0;width:48px;height:48px;transform:rotate(${player.yaw*-1}rad)" alt="${player.name}">
|
|
|
|
</div>`,
|
|
|
|
className: '', // don't use leaflet default of a white block
|
|
|
|
iconSize: [48, 48],
|
|
|
|
iconAnchor: [24, 24],
|
|
|
|
popupAnchor: [0, -24]
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-03-22 13:16:28 +01:00
|
|
|
isPlayerInCurrentLayer: function(player){
|
2022-12-13 20:40:26 +02:00
|
|
|
const mapLayer = layerMgr.getCurrentLayer();
|
2019-03-22 13:16:28 +01:00
|
|
|
|
2019-12-01 10:50:40 +01:00
|
|
|
return (
|
|
|
|
player.pos.y >= (mapLayer.from*16) &&
|
|
|
|
player.pos.y <= ((mapLayer.to*16) + 15)
|
|
|
|
);
|
2019-03-22 13:16:28 +01:00
|
|
|
},
|
|
|
|
|
2019-06-11 15:01:16 +02:00
|
|
|
onMinetestUpdate: function(/*info*/){
|
2019-02-04 16:05:15 +01:00
|
|
|
|
2019-09-18 11:53:58 +02:00
|
|
|
players.forEach(player => {
|
2022-12-13 20:40:26 +02:00
|
|
|
const isInLayer = this.isPlayerInCurrentLayer(player);
|
2019-03-22 13:16:28 +01:00
|
|
|
|
|
|
|
if (!isInLayer){
|
2019-06-17 07:29:33 +02:00
|
|
|
if (this.currentObjects[player.name]){
|
2019-03-22 13:16:28 +01:00
|
|
|
//player is displayed and not on the layer anymore
|
|
|
|
//Remove the marker and reference
|
2019-06-17 07:29:33 +02:00
|
|
|
this.currentObjects[player.name].remove();
|
|
|
|
delete this.currentObjects[player.name];
|
2019-03-22 13:16:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-17 07:29:33 +02:00
|
|
|
if (this.currentObjects[player.name]){
|
2019-02-04 16:05:15 +01:00
|
|
|
//marker exists
|
2022-12-13 20:40:26 +02:00
|
|
|
const marker = this.currentObjects[player.name];
|
2019-06-17 07:29:33 +02:00
|
|
|
marker.setLatLng([player.pos.z, player.pos.x]);
|
2022-12-13 20:40:26 +02:00
|
|
|
marker.setIcon(this.getIcon(player));
|
2019-06-17 07:29:33 +02:00
|
|
|
marker.setPopupContent(this.createPopup(player));
|
2019-02-04 21:15:00 +01:00
|
|
|
|
2019-02-04 16:05:15 +01:00
|
|
|
} else {
|
|
|
|
//marker does not exist
|
2022-12-13 20:40:26 +02:00
|
|
|
const marker = this.createMarker(player);
|
2019-06-17 07:29:33 +02:00
|
|
|
marker.addTo(this);
|
2019-02-04 21:15:00 +01:00
|
|
|
|
2019-06-17 07:29:33 +02:00
|
|
|
this.currentObjects[player.name] = marker;
|
2019-02-04 16:05:15 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-17 07:29:33 +02:00
|
|
|
Object.keys(this.currentObjects).forEach(existingName => {
|
2022-12-13 20:40:26 +02:00
|
|
|
const playerIsActive = players.find(function(p){
|
2019-02-04 16:05:15 +01:00
|
|
|
return p.name == existingName;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!playerIsActive){
|
|
|
|
//player
|
2019-06-17 07:29:33 +02:00
|
|
|
this.currentObjects[existingName].remove();
|
|
|
|
delete this.currentObjects[existingName];
|
2019-02-04 16:05:15 +01:00
|
|
|
}
|
|
|
|
});
|
2019-02-03 18:18:14 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
reDraw: function(){
|
2019-02-04 16:05:15 +01:00
|
|
|
this.currentObjects = {};
|
2019-02-03 18:18:14 +01:00
|
|
|
this.clearLayers();
|
|
|
|
|
2019-09-18 11:53:58 +02:00
|
|
|
players.forEach(player => {
|
2019-06-17 07:29:33 +02:00
|
|
|
if (!this.isPlayerInCurrentLayer(player)){
|
2019-03-22 13:16:28 +01:00
|
|
|
//not in current layer
|
|
|
|
return;
|
|
|
|
}
|
2019-02-04 16:05:15 +01:00
|
|
|
|
2022-12-13 20:40:26 +02:00
|
|
|
const marker = this.createMarker(player);
|
2019-06-17 07:29:33 +02:00
|
|
|
marker.addTo(this);
|
|
|
|
this.currentObjects[player.name] = marker;
|
2019-02-04 16:05:15 +01:00
|
|
|
});
|
|
|
|
|
2019-02-03 18:18:14 +01:00
|
|
|
},
|
|
|
|
|
2019-06-11 15:01:16 +02:00
|
|
|
onAdd: function(/*map*/) {
|
2019-09-18 11:53:58 +02:00
|
|
|
wsChannel.addListener("minetest-info", this.onMinetestUpdate);
|
2019-02-04 15:22:56 +01:00
|
|
|
this.reDraw();
|
2019-02-03 18:18:14 +01:00
|
|
|
},
|
|
|
|
|
2019-06-11 15:01:16 +02:00
|
|
|
onRemove: function(/*map*/) {
|
2019-02-03 18:18:14 +01:00
|
|
|
this.clearLayers();
|
2019-09-18 11:53:58 +02:00
|
|
|
wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
|
2019-02-03 18:18:14 +01:00
|
|
|
}
|
|
|
|
});
|