diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-07-19 20:38:24 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-07-19 20:38:24 -0700 |
commit | 0fd9fb097552686f2257c1aa689d797e80057bd1 (patch) | |
tree | b8d0367bf7b62c049af60ace301ce1cffc08d821 /client/lib/JumpStorm.ts | |
download | jumpstorm-0fd9fb097552686f2257c1aa689d797e80057bd1.tar.gz jumpstorm-0fd9fb097552686f2257c1aa689d797e80057bd1.zip |
initial commit
Diffstat (limited to 'client/lib/JumpStorm.ts')
-rw-r--r-- | client/lib/JumpStorm.ts | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/client/lib/JumpStorm.ts b/client/lib/JumpStorm.ts new file mode 100644 index 0000000..c76d9bc --- /dev/null +++ b/client/lib/JumpStorm.ts @@ -0,0 +1,54 @@ +import { Floor, Player } from "./entities"; +import { Game } from "./Game"; +import { + WallBounds, + FacingDirection, + Render, + Physics, + Input, + Collision, +} from "./systems"; + +export class JumpStorm { + private game: Game; + + constructor(ctx: CanvasRenderingContext2D) { + this.game = new Game(); + + [ + this.createInputSystem(), + new FacingDirection(), + new Physics(), + new Collision(), + new WallBounds(ctx.canvas.width), + new Render(ctx), + ].forEach((system) => this.game.addSystem(system)); + + [new Floor(160), new Player()].forEach((entity) => + this.game.addEntity(entity) + ); + } + + public play() { + this.game.start(); + + const loop = (timestamp: number) => { + this.game.doGameLoop(timestamp); + requestAnimationFrame(loop); // tail call recursion! /s + }; + requestAnimationFrame(loop); + } + + private createInputSystem(): Input { + const inputSystem = new Input(); + + window.addEventListener("keydown", (e) => { + if (!e.repeat) { + inputSystem.keyPressed(e.key); + } + }); + window.addEventListener("keyup", (e) => inputSystem.keyReleased(e.key)); + + return inputSystem; + } +} |