From a8d07a790395e14fe7aedd3ba638db466f9c0842 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Fri, 1 Mar 2024 18:56:58 -0700 Subject: boundingbox + draw player --- src/engine/TheAbstractionEngine.ts | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/engine/TheAbstractionEngine.ts (limited to 'src/engine/TheAbstractionEngine.ts') diff --git a/src/engine/TheAbstractionEngine.ts b/src/engine/TheAbstractionEngine.ts new file mode 100644 index 0000000..e720293 --- /dev/null +++ b/src/engine/TheAbstractionEngine.ts @@ -0,0 +1,42 @@ +import { Game } from "."; +import { loadAssets } from "./config"; +import { Player } from "./entities"; +import { Render } from "./systems"; + +export class TheAbstractionEngine { + private game: Game; + private ctx: CanvasRenderingContext2D; + private animationFrameId: number | null; + + constructor(game: Game, ctx: CanvasRenderingContext2D) { + this.game = game; + this.ctx = ctx; + this.animationFrameId = null; + } + + public async init() { + await loadAssets(); + + [new Render(this.ctx)].forEach((system) => this.game.addSystem(system)); + + const player = new Player(); + this.game.addEntity(player); + } + + public play() { + this.game.start(); + + const loop = (timestamp: number) => { + this.game.doGameLoop(timestamp); + this.animationFrameId = requestAnimationFrame(loop); // tail call recursion! /s + }; + this.animationFrameId = requestAnimationFrame(loop); + } + + public stop() { + if (this.animationFrameId) { + cancelAnimationFrame(this.animationFrameId); + this.animationFrameId = null; + } + } +} -- cgit v1.2.3-70-g09d2