2020-12-16 16:54:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 James Lyne
|
|
|
|
*
|
|
|
|
* Some portions of this file were taken from https://github.com/webbukkit/dynmap.
|
|
|
|
* These portions are Copyright 2020 Dynmap Contributors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-12-12 22:04:56 +00:00
|
|
|
import {MarkerOptions, DivIcon, DomUtil} from 'leaflet';
|
2021-01-26 20:37:29 +00:00
|
|
|
import {getMinecraftHead} from '@/util';
|
2021-05-15 19:25:03 +00:00
|
|
|
import playerImage from '@/assets/images/player_face.png';
|
2021-07-24 01:27:25 +00:00
|
|
|
import {LiveAtlasPlayer} from "@/index";
|
2020-12-13 13:23:32 +00:00
|
|
|
|
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 {
|
2021-07-24 01:27:25 +00:00
|
|
|
private readonly _player: LiveAtlasPlayer;
|
2020-11-24 01:52:31 +00:00
|
|
|
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;
|
|
|
|
|
2021-01-05 23:48:56 +00:00
|
|
|
private _currentName?: string;
|
|
|
|
|
2020-11-24 01:52:31 +00:00
|
|
|
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;
|
|
|
|
|
2021-07-24 01:27:25 +00:00
|
|
|
constructor(player: LiveAtlasPlayer, options: PlayerIconOptions) {
|
2020-11-24 01:52:31 +00:00
|
|
|
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;
|
2021-01-05 21:06:10 +00:00
|
|
|
let offset = 8;
|
2020-11-24 01:52:31 +00:00
|
|
|
|
|
|
|
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';
|
2021-07-24 01:27:25 +00:00
|
|
|
this._playerName.innerHTML = this._currentName = player.displayName;
|
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';
|
2021-01-05 21:06:10 +00:00
|
|
|
offset = 8;
|
2020-12-10 02:21:42 +00:00
|
|
|
} else if(this.options.showBody) {
|
|
|
|
this._playerImage = bodyImage.cloneNode() as HTMLImageElement;
|
|
|
|
size = 'body';
|
2021-01-05 21:06:10 +00:00
|
|
|
offset = 16;
|
2020-12-10 02:21:42 +00:00
|
|
|
} else {
|
|
|
|
this._playerImage = largeImage.cloneNode() as HTMLImageElement;
|
|
|
|
size = '32';
|
2021-01-05 21:06:10 +00:00
|
|
|
offset = 16;
|
2020-12-10 02:21:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
getMinecraftHead(player, size).then(head => {
|
2020-12-10 02:21:42 +00:00
|
|
|
this._playerImage!.src = head.src;
|
2020-12-22 13:15:59 +00:00
|
|
|
}).catch(() => {});
|
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._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
|
|
|
|
2021-01-24 22:14:39 +00:00
|
|
|
this._playerHealth.appendChild(this._playerHealthBar);
|
|
|
|
this._playerArmor.appendChild(this._playerArmorBar);
|
|
|
|
this._playerInfo.appendChild(this._playerHealth);
|
|
|
|
this._playerInfo.appendChild(this._playerArmor);
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2021-01-05 21:06:10 +00:00
|
|
|
this._container.style.marginTop = `-${offset}px`;
|
|
|
|
this._container.style.marginLeft = `-${offset}px`;
|
|
|
|
|
2020-11-24 01:52:31 +00:00
|
|
|
return this._container;
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
2020-12-01 23:20:38 +00:00
|
|
|
if(!this._container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-24 01:27:25 +00:00
|
|
|
if(this._player!.displayName !== this._currentName) {
|
|
|
|
this._playerName!.innerHTML = this._currentName = this._player!.displayName;
|
2021-01-05 23:48:56 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|