1
0
forked from MTSR/mapserver

player popup / es6ify

This commit is contained in:
NatureFreshMilk 2019-06-17 07:29:33 +02:00
parent c4f52ee923
commit c7e4a99419

View File

@ -26,14 +26,11 @@ export default L.LayerGroup.extend({
}.bind(this)); }.bind(this));
}, },
createMarker: function(player){ createPopup: function(player){
var marker = L.marker([player.pos.z, player.pos.x], {icon: PlayerIcon});
var i;
var html = "<b>" + player.name + "</b>"; var html = "<b>" + player.name + "</b>";
html += "<hr>"; html += "<hr>";
for (i=0; i<Math.floor(player.hp / 2); i++) for (let i=0; i<Math.floor(player.hp / 2); i++)
html += "<img src='pics/heart.png'>"; html += "<img src='pics/heart.png'>";
if (player.hp % 2 == 1) if (player.hp % 2 == 1)
@ -41,14 +38,19 @@ export default L.LayerGroup.extend({
html += "<br>"; html += "<br>";
for (i=0; i<Math.floor(player.breath / 2); i++) for (let i=0; i<Math.floor(player.breath / 2); i++)
html += "<img src='pics/bubble.png'>"; html += "<img src='pics/bubble.png'>";
if (player.breath % 2 == 1) if (player.breath % 2 == 1)
html += "<img src='pics/bubble_half.png'>"; html += "<img src='pics/bubble_half.png'>";
return html;
},
marker.bindPopup(html); createMarker: function(player){
var marker = L.marker([player.pos.z, player.pos.x], {icon: PlayerIcon});
marker.bindPopup(this.createPopup(player));
return marker; return marker;
}, },
@ -59,65 +61,64 @@ export default L.LayerGroup.extend({
}, },
onMinetestUpdate: function(/*info*/){ onMinetestUpdate: function(/*info*/){
var self = this;
this.players.forEach(function(player){ this.players.forEach(player => {
var isInLayer = self.isPlayerInCurrentLayer(player); var isInLayer = this.isPlayerInCurrentLayer(player);
if (!isInLayer){ if (!isInLayer){
if (self.currentObjects[player.name]){ if (this.currentObjects[player.name]){
//player is displayed and not on the layer anymore //player is displayed and not on the layer anymore
//Remove the marker and reference //Remove the marker and reference
self.currentObjects[player.name].remove(); this.currentObjects[player.name].remove();
delete self.currentObjects[player.name]; delete this.currentObjects[player.name];
} }
return; return;
} }
if (self.currentObjects[player.name]){ if (this.currentObjects[player.name]){
//marker exists //marker exists
self.currentObjects[player.name].setLatLng([player.pos.z, player.pos.x]); let marker = this.currentObjects[player.name];
//setPopupContent marker.setLatLng([player.pos.z, player.pos.x]);
marker.setPopupContent(this.createPopup(player));
} else { } else {
//marker does not exist //marker does not exist
var marker = self.createMarker(player); let marker = this.createMarker(player);
marker.addTo(self); marker.addTo(this);
self.currentObjects[player.name] = marker; this.currentObjects[player.name] = marker;
} }
}); });
Object.keys(self.currentObjects).forEach(function(existingName){ Object.keys(this.currentObjects).forEach(existingName => {
var playerIsActive = self.players.find(function(p){ var playerIsActive = this.players.find(function(p){
return p.name == existingName; return p.name == existingName;
}); });
if (!playerIsActive){ if (!playerIsActive){
//player //player
self.currentObjects[existingName].remove(); this.currentObjects[existingName].remove();
delete self.currentObjects[existingName]; delete this.currentObjects[existingName];
} }
}); });
}, },
reDraw: function(){ reDraw: function(){
var self = this;
this.currentObjects = {}; this.currentObjects = {};
this.clearLayers(); this.clearLayers();
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = this.layerMgr.getCurrentLayer();
this.players.forEach(function(player){ this.players.forEach(player => {
if (!self.isPlayerInCurrentLayer(player)){ if (!this.isPlayerInCurrentLayer(player)){
//not in current layer //not in current layer
return; return;
} }
var marker = self.createMarker(player); var marker = this.createMarker(player);
marker.addTo(self); marker.addTo(this);
self.currentObjects[player.name] = marker; this.currentObjects[player.name] = marker;
}); });
}, },