update game engine

This commit is contained in:
Chris Wanstrath 2025-09-28 18:26:51 -07:00
parent 7a2af832f4
commit cea3fa32ed
3 changed files with 26 additions and 6 deletions

View File

@ -3,8 +3,7 @@
export const game = true export const game = true
import type { InputState } from "@/shared/types" import type { GameContext, InputState } from "@/shared/game"
import type { GameContext } from "@/shared/game"
import { rng } from "@/shared/utils.ts" import { rng } from "@/shared/utils.ts"
const WIDTH = 960 const WIDTH = 960

View File

@ -1,14 +1,37 @@
export type InputState = { key: string, shift: boolean, ctrl: boolean, meta: boolean, pressed: Set<string> }
export class GameContext { export class GameContext {
constructor(public ctx: CanvasRenderingContext2D) { } constructor(public ctx: CanvasRenderingContext2D) { }
get width() { return this.ctx.canvas.width } width = 960
get height() { return this.ctx.canvas.height } height = 540
clear() { clear() {
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height) this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height)
} }
text(msg: string, x: number, y: number, color = "black", size = 16, font = "monospace") {
const c = this.ctx
c.save()
c.fillStyle = color
c.font = `${size}px ${font}`
c.textBaseline = "top"
c.fillText(msg, x, y)
c.restore()
}
centerText(msg: string, y: number, color = "black", size = 16, font = "monospace") {
const c = this.ctx
c.save()
c.fillStyle = color
c.font = `${size}px ${font}`
c.textBaseline = "middle"
const metrics = c.measureText(msg)
const x = (this.width - metrics.width) / 2
c.fillText(msg, x, y)
c.restore()
}
circ(x: number, y: number, r: number, color = "black") { circ(x: number, y: number, r: number, color = "black") {
const c = this.ctx const c = this.ctx
c.save() c.save()

View File

@ -15,5 +15,3 @@ export type CommandResult = {
status: "ok" | "error" status: "ok" | "error"
output: CommandOutput output: CommandOutput
} }
export type InputState = { key: string, shift: boolean, ctrl: boolean, meta: boolean, pressed: Set<string> }