import { Point, Rect, Size } from "./engine/datatypes.ts"; import { DrawPile } from "./drawpile.ts"; import { withCamera } from "./layout.ts"; import { getSkillsModal } from "./skillsmodal.ts"; import { addButton } from "./button.ts"; import { getPlayerProgress } from "./playerprogress.ts"; import { getStateManager } from "./statemanager.ts"; import { getCheckModal } from "./checkmodal.ts"; type Button = { label: string; cbClick: () => void; enabled: boolean; endorse: boolean; }; export class Hotbar { #drawpile: DrawPile; constructor() { this.#drawpile = new DrawPile(); } get #cellSize(): Size { return new Size(96, 32); } get size(): Size { let { w: cellW, h: cellH } = this.#cellSize; let w = this.#computeButtons().length * cellW; return new Size(w, cellH); } #computeButtons(): Button[] { let buttons: Button[] = []; buttons.push({ label: "Skills", cbClick: () => { getSkillsModal().setShown(true); }, enabled: getPlayerProgress().getAvailableSkills().length > 0, endorse: getPlayerProgress().anyAffordableSkillsAtMinimum(), }); /* buttons.push({ label: "Thralls" }) */ buttons.push({ label: "Sleep", cbClick: () => { this.#offerSleep(); }, enabled: true, endorse: getPlayerProgress().getBlood() < 100, }); /* buttons.push({ label:"Cheat", cbClick: () => { new LadderPickup().onClick(); }, enabled: true, endorse: false, }) buttons.push({ label:"Dig for bad maps", cbClick: () => { let i = 0; try { for(; i < 10000; i++) { generateMap(); } } catch(e) { console.log(`Map gen failed after ${i} tries.`); } console.log("Ten thousand maps generated successfully."); }, enabled: true, endorse: true, }) */ return buttons; } #offerSleep() { let bloodAmount = getPlayerProgress().getBlood(); let sleepText = "You're exhausted."; if (bloodAmount > 100) { sleepText = "You've got some energy left -- are you sure you want to sleep?"; } else if (bloodAmount > 2000) { sleepText = "Are you sure you want to sleep? You have so much energy."; } getCheckModal().show( { label: sleepText, options: [ { isChoice: true, countsAsSuccess: true, unlockable: "Sleep", success: null, }, { isChoice: true, countsAsSuccess: false, unlockable: "Refrain", success: null, }, ], }, () => { getStateManager().advance(); }, ); } update() { withCamera("Hotbar", () => this.#update()); } #update() { this.#drawpile.clear(); let buttons = this.#computeButtons(); let cellSize = this.#cellSize; let x = 0; for (let b of buttons.values()) { addButton( this.#drawpile, b.label, new Rect(new Point(x, 0), cellSize), b.enabled, b.cbClick, { endorse: b.endorse }, ); x += cellSize.w; } this.#drawpile.executeOnClick(); } draw() { // D.fillRect(new Point(-4, -4), this.size.add(new Size(8, 8)), BG_INSET); withCamera("Hotbar", () => this.#drawpile.draw()); } } let active = new Hotbar(); export function getHotbar(): Hotbar { return active; }