mapserver/server/static/js/overlays/PlayerOverlay.js

137 lines
3.4 KiB
JavaScript
Raw Normal View History

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});
2019-02-22 18:28:20 +03:00
var html = "<b>" + player.name + "</b>";
html += "<hr>";
for (var i=0; i<Math.floor(player.hp / 2); i++)
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>";
for (var i=0; i<Math.floor(player.breath / 2); i++)
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
marker.bindPopup(html);
2019-02-04 23:15:00 +03:00
return marker;
},
2019-03-22 15:16:28 +03:00
isPlayerInCurrentLayer: function(player){
var mapLayer = this.layerMgr.getCurrentLayer()
return (player.pos.y >= (mapLayer.from*16) && player.pos.y <= (mapLayer.to*16))
2019-03-22 15:16:28 +03:00
},
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){
2019-03-22 15:16:28 +03:00
var isInLayer = self.isPlayerInCurrentLayer(player);
if (!isInLayer){
if (self.currentObjects[player.name]){
//player is displayed and not on the layer anymore
//Remove the marker and reference
self.currentObjects[player.name].remove();
delete self.currentObjects[player.name];
}
return;
}
2019-02-04 18:05:15 +03:00
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){
2019-03-22 15:16:28 +03:00
if (!self.isPlayerInCurrentLayer(player)){
//not in current layer
return;
}
2019-02-04 18:05:15 +03:00
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
}
});