fledgling/src/hotbar.ts
2025-02-02 21:09:10 -08:00

80 lines
1.6 KiB
TypeScript

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 {getSleepModal} from "./sleepmodal.ts";
type Button = {
label: string,
cbClick: () => void,
}
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)
}
})
/*
buttons.push({
label: "Thralls"
})
*/
buttons.push({
label: "Sleep",
cbClick: () => {
getSleepModal().setShown(true)
}
})
return buttons;
}
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), true, b.cbClick);
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;
}