update game engine
This commit is contained in:
parent
7a2af832f4
commit
cea3fa32ed
|
|
@ -3,8 +3,7 @@
|
|||
|
||||
export const game = true
|
||||
|
||||
import type { InputState } from "@/shared/types"
|
||||
import type { GameContext } from "@/shared/game"
|
||||
import type { GameContext, InputState } from "@/shared/game"
|
||||
import { rng } from "@/shared/utils.ts"
|
||||
|
||||
const WIDTH = 960
|
||||
|
|
|
|||
|
|
@ -1,14 +1,37 @@
|
|||
export type InputState = { key: string, shift: boolean, ctrl: boolean, meta: boolean, pressed: Set<string> }
|
||||
|
||||
export class GameContext {
|
||||
constructor(public ctx: CanvasRenderingContext2D) { }
|
||||
|
||||
get width() { return this.ctx.canvas.width }
|
||||
get height() { return this.ctx.canvas.height }
|
||||
width = 960
|
||||
height = 540
|
||||
|
||||
clear() {
|
||||
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") {
|
||||
const c = this.ctx
|
||||
c.save()
|
||||
|
|
|
|||
|
|
@ -15,5 +15,3 @@ export type CommandResult = {
|
|||
status: "ok" | "error"
|
||||
output: CommandOutput
|
||||
}
|
||||
|
||||
export type InputState = { key: string, shift: boolean, ctrl: boolean, meta: boolean, pressed: Set<string> }
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user