2020-12-12 22:04:56 +00:00
|
|
|
import {MarkerOptions, DivIcon, DomUtil} from 'leaflet';
|
2020-11-24 01:52:31 +00:00
|
|
|
import {DynmapPlayer} from "@/dynmap";
|
2020-12-10 02:21:42 +00:00
|
|
|
import Util from '@/util';
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-13 13:23:32 +00:00
|
|
|
const playerImage = require('@/assets/images/player_face.png');
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
const noSkinImage: HTMLImageElement = document.createElement('img');
|
|
|
|
noSkinImage.height = 16;
|
|
|
|
noSkinImage.width = 16;
|
2020-12-10 02:21:42 +00:00
|
|
|
|
|
|
|
const smallImage: HTMLImageElement = document.createElement('img');
|
|
|
|
smallImage.height = 16;
|
|
|
|
smallImage.width = 16;
|
|
|
|
|
|
|
|
const largeImage: HTMLImageElement = document.createElement('img');
|
|
|
|
largeImage.height = 32;
|
|
|
|
largeImage.width = 32;
|
|
|
|
|
|
|
|
const bodyImage: HTMLImageElement = document.createElement('img');
|
|
|
|
bodyImage.height = 32;
|
|
|
|
bodyImage.width = 32;
|
|
|
|
|
2020-12-13 13:23:32 +00:00
|
|
|
noSkinImage.src = smallImage.src = largeImage.src = bodyImage.src = playerImage;
|
2020-12-10 02:21:42 +00:00
|
|
|
noSkinImage.className = smallImage.className = largeImage.className = bodyImage.className = 'player__icon';
|
2020-12-01 23:20:38 +00:00
|
|
|
|
|
|
|
export interface PlayerIconOptions extends MarkerOptions {
|
2020-11-24 01:52:31 +00:00
|
|
|
smallFace: boolean,
|
|
|
|
showSkinFace: boolean,
|
|
|
|
showBody: boolean,
|
|
|
|
showHealth: boolean,
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:04:56 +00:00
|
|
|
export class PlayerIcon extends DivIcon {
|
2020-11-24 01:52:31 +00:00
|
|
|
private readonly _player: DynmapPlayer;
|
|
|
|
private _container?: HTMLDivElement;
|
|
|
|
private _playerImage?: HTMLImageElement;
|
2020-12-10 02:21:42 +00:00
|
|
|
private _playerInfo?: HTMLSpanElement;
|
2020-11-24 01:52:31 +00:00
|
|
|
private _playerName?: HTMLSpanElement;
|
|
|
|
|
|
|
|
private _playerHealth?: HTMLDivElement;
|
|
|
|
private _playerHealthBar?: HTMLDivElement;
|
2020-12-10 02:21:42 +00:00
|
|
|
private _playerArmor?: HTMLDivElement;
|
|
|
|
private _playerArmorBar?: HTMLDivElement;
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
options: PlayerIconOptions;
|
|
|
|
|
|
|
|
constructor(player: DynmapPlayer, options: PlayerIconOptions) {
|
|
|
|
super(options);
|
|
|
|
this._player = player;
|
|
|
|
}
|
|
|
|
|
|
|
|
createIcon(oldIcon: HTMLElement) {
|
|
|
|
if (oldIcon) {
|
2020-12-12 22:04:56 +00:00
|
|
|
DomUtil.remove(oldIcon);
|
2020-11-24 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const player = this._player;
|
|
|
|
|
|
|
|
this._container = document.createElement('div');
|
|
|
|
|
2020-12-10 02:21:42 +00:00
|
|
|
this._container.classList.add('marker', 'marker--player', 'leaflet-marker-icon');
|
|
|
|
|
|
|
|
this._playerInfo = document.createElement('div');
|
|
|
|
this._playerInfo.className = 'marker__label';
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
this._playerName = document.createElement('span');
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerName.className = 'player__name';
|
2020-12-14 00:28:09 +00:00
|
|
|
this._playerName.innerHTML = player.name;
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
if (this.options.showSkinFace) {
|
2020-12-10 02:21:42 +00:00
|
|
|
let size;
|
|
|
|
|
|
|
|
if (this.options.smallFace) {
|
|
|
|
this._playerImage = smallImage.cloneNode() as HTMLImageElement;
|
|
|
|
size = '16';
|
|
|
|
} else if(this.options.showBody) {
|
|
|
|
this._playerImage = bodyImage.cloneNode() as HTMLImageElement;
|
|
|
|
size = 'body';
|
|
|
|
} else {
|
|
|
|
this._playerImage = largeImage.cloneNode() as HTMLImageElement;
|
|
|
|
size = '32';
|
|
|
|
}
|
|
|
|
|
|
|
|
Util.getMinecraftHead(player, size).then(head => {
|
|
|
|
this._playerImage!.src = head.src;
|
|
|
|
});
|
2020-12-01 23:20:38 +00:00
|
|
|
} else {
|
|
|
|
this._playerImage = noSkinImage.cloneNode(false) as HTMLImageElement;
|
2020-11-24 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
this._container.appendChild(this._playerImage);
|
2020-12-10 02:21:42 +00:00
|
|
|
this._container.appendChild(this._playerInfo);
|
|
|
|
this._playerInfo.appendChild(this._playerName);
|
2020-12-01 23:20:38 +00:00
|
|
|
|
2020-11-24 01:52:31 +00:00
|
|
|
if (this.options.showHealth) {
|
|
|
|
this._playerHealth = document.createElement('div');
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerHealth.className = 'player__health';
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerArmor = document.createElement('div');
|
|
|
|
this._playerArmor.className = 'player__armor';
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerInfo.appendChild(this._playerHealth);
|
|
|
|
this._playerInfo.appendChild(this._playerArmor);
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerHealthBar = document.createElement('div');
|
|
|
|
this._playerHealthBar.className = 'player__health-bar';
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerArmorBar = document.createElement('div');
|
|
|
|
this._playerArmorBar.className = 'player__armor-bar';
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerHealth.hidden = this._playerArmor.hidden = true;
|
2020-11-24 01:52:31 +00:00
|
|
|
} else {
|
|
|
|
this._playerName.classList.add('playerNameNoHealth');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._container;
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
2020-12-01 23:20:38 +00:00
|
|
|
if(!this._container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:22:59 +00:00
|
|
|
this._playerName!.innerHTML = this._player!.name;
|
2020-11-24 01:52:31 +00:00
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
if(this.options.showHealth) {
|
|
|
|
if (this._player.health !== undefined && this._player.armor !== undefined) {
|
|
|
|
this._playerHealth!.hidden = false;
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerArmor!.hidden = false;
|
2020-12-01 23:20:38 +00:00
|
|
|
this._playerHealthBar!.style.width = Math.ceil(this._player.health * 2.5) + 'px';
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerArmorBar!.style.width = Math.ceil(this._player.armor * 2.5) + 'px';
|
2020-12-01 23:20:38 +00:00
|
|
|
} else {
|
|
|
|
this._playerHealth!.hidden = true;
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerArmor!.hidden = true;
|
2020-12-01 23:20:38 +00:00
|
|
|
}
|
2020-11-24 01:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|