import * as Enum from "./Enums.js";
import {BuyMenu} from "./hud/BuyMenu.js";
import {ScoreBoard} from "./hud/ScoreBoard.js";
import {KillFeed} from "./hud/KillFeed.js";
import {Radar} from "./hud/Radar.js";
export class HUD {
#game
#setting
#buyMenu = null;
#scoreBoard = null;
#killFeed = null;
#radar = null;
#showAble = {
showScore: false,
showBuyMenu: false
}
#elements = {
score: null,
scoreDetail: null,
buyMenu: null,
canBuyIcon: null,
canPlantIcon: null,
equippedItem: null,
slotModel: null,
shotModel: null,
dropModel: null,
inventory: null,
money: null,
health: null,
armor: null,
ammo: null,
messageTop: null,
messageBottom: null,
scoreMyTeam: null,
scoreOpponentTeam: null,
aliveMyTeam: null,
aliveOpponentTeam: null,
time: null,
killFeed: null,
}
#shotAnimationInterval = null;
#countDownIntervalId = null;
#scoreBoardData = null;
injectDependency(game, setting) {
this.#game = game
this.#setting = setting
}
pause(msg, timeMs) {
this.#startCountDown(timeMs)
this.displayTopMessage(msg)
}
toggleBuyMenu() {
this.#showAble.showBuyMenu = !this.#showAble.showBuyMenu
}
toggleScore(enabled) {
this.#showAble.showScore = enabled
}
bombPlanted(detonationTimeSec) {
this.#resetCountDown()
this.#elements.time.innerText = '⚠️ 💣'
this.displayBottomMessage(`⚠️ Alert
The bomb has been planted.
${detonationTimeSec} seconds to detonation.`)
setTimeout(() => this.clearBottomMessage(), 3000)
}
requestFullScoreBoardUpdate(scoreBoardData) {
this.#scoreBoardData = scoreBoardData
}
updateMyTeamPlayerMoney(playerData, money) {
const moneyElement = this.#scoreBoard.getPlayerStatRowElement(playerData).querySelector('[data-money]')
moneyElement.innerText = `${money}`
}
showKill(playerCulprit, playerDead, wasHeadshot, playerMe, killedItemId) {
this.#killFeed.showKill(playerCulprit, playerDead, wasHeadshot, playerMe, killedItemId)
}
roundStart(roundTimeMs) {
this.#startCountDown(roundTimeMs)
}
#resetCountDown() {
clearInterval(this.#countDownIntervalId)
}
#startCountDown(timeMs) {
this.#resetCountDown()
let roundTimeSec = Math.floor(timeMs / 1000)
const timeElement = this.#elements.time
let roundTimeInterval = setInterval(function () {
roundTimeSec--
let roundTimeMinute = Math.floor(roundTimeSec / 60)
timeElement.innerText = `${roundTimeMinute.toString().padStart(2, '0')}:${(roundTimeSec % 60).toString().padStart(2, '0')}`
if (roundTimeSec === 0) {
clearInterval(roundTimeInterval)
}
}, 1000)
this.#countDownIntervalId = roundTimeInterval
}
startWarmup(timeMs) {
this.displayTopMessage('Waiting for all players to connect')
this.#startCountDown(timeMs)
}
displayTopMessage(msg) {
this.#elements.messageTop.innerText = msg
}
displayBottomMessage(msg) {
this.#elements.messageBottom.innerHTML = msg
}
clearAlerts() {
this.clearTopMessage()
this.clearBottomMessage()
this.#elements.killFeed.innerHTML = ''
}
clearTopMessage() {
this.#elements.messageTop.innerText = ''
}
clearBottomMessage() {
this.#elements.messageBottom.innerHTML = ''
}
equip(slotId, availableSlots) {
this.#elements.slotModel.src = `./resources/slot_${slotId}.png`
this.#elements.inventory.querySelectorAll('[data-slot]').forEach(function (node) {
node.classList.remove('highlight', 'hidden')
if (availableSlots[node.dataset.slot] === undefined) {
node.classList.add('hidden')
}
})
this.#elements.inventory.querySelector(`[data-slot="${slotId}"]`).classList.add('highlight')
}
showShot(item) {
clearTimeout(this.#shotAnimationInterval)
this.#elements.shotModel.classList.remove('hidden');
this.#shotAnimationInterval = setTimeout(() => this.#elements.shotModel.classList.add('hidden'), 30)
}
showDropAnimation(item) {
clearTimeout(this.#shotAnimationInterval)
this.#elements.dropModel.classList.remove('hidden');
this.#shotAnimationInterval = setTimeout(() => this.#elements.dropModel.classList.add('hidden'), 100)
}
updateHud(player) {
if (this.#radar) {
this.#radar.update(this.#game.getMyTeamPlayers(), this.#game.playerSpectate.getId(), this.#game.getPlayerSpectateRotation()[0])
}
if (this.#scoreBoardData !== null) {
this.#scoreBoard.update(this.#scoreBoardData)
this.#scoreBoardData = null
}
this.#elements.score.classList.toggle('hidden', !this.#showAble.showScore);
this.#elements.canBuyIcon.classList.toggle('hidden', !player.canBuy);
this.#elements.canPlantIcon.classList.toggle('hidden', !player.canPlant);
if (player.canBuy && this.#showAble.showBuyMenu) {
this.#game.requestPointerUnLock()
this.#buyMenu.refresh(player, this.#game.playerMe.getTeamName())
this.#elements.buyMenu.classList.remove('hidden');
} else if (!this.#elements.buyMenu.classList.contains('hidden')) {
this.#game.requestPointerLock()
this.#elements.buyMenu.innerHTML = ''
this.#elements.buyMenu.classList.add('hidden');
}
this.#elements.money.innerText = player.money
this.#elements.health.innerText = player.health
this.#elements.armor.innerText = player.armor
if (player.ammo === null) {
this.#elements.ammo.innerText = `${player.item.name}`
} else {
this.#elements.ammo.innerText = `${player.item.name} - ${player.ammo} / ${player.ammoReserve}`
}
let myTeamIndex = this.#game.playerMe.getTeamIndex()
let otherTeamIndex = this.#game.playerMe.getOtherTeamIndex()
this.#elements.scoreMyTeam.innerHTML = this.#game.score.score[myTeamIndex]
this.#elements.scoreOpponentTeam.innerHTML = this.#game.score.score[otherTeamIndex]
this.#elements.aliveMyTeam.innerHTML = this.#game.alivePlayers[myTeamIndex]
this.#elements.aliveOpponentTeam.innerHTML = this.#game.alivePlayers[otherTeamIndex]
}
createHud(elementHud, map) {
if (this.#elements.score) {
throw new Error("HUD already created")
}
elementHud.innerHTML = `
Knife
Primary
Secondary
Bomb