summaryrefslogtreecommitdiff
path: root/src/engine/TheAbstractionEngine.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 18:56:58 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 18:56:58 -0700
commita8d07a790395e14fe7aedd3ba638db466f9c0842 (patch)
tree644f60a6bca79ceb55f24fcab7bdb3dee52c2d6e /src/engine/TheAbstractionEngine.ts
parentaa08a8943a9a2d4a0e51893eebe6900bca7a7251 (diff)
downloadthe-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.tar.gz
the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.zip
boundingbox + draw player
Diffstat (limited to 'src/engine/TheAbstractionEngine.ts')
-rw-r--r--src/engine/TheAbstractionEngine.ts42
1 files changed, 42 insertions, 0 deletions
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;
+ }
+ }
+}