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; } } }