diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 19:45:33 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-01 19:45:33 -0700 |
commit | d08e0105cbc59c6cc804f04aaf1e4e625a13960c (patch) | |
tree | 57d5270e146ce2e0c5cfba30e172ab87a2210514 /src/engine/TheAbstractionEngine.ts | |
parent | a8d07a790395e14fe7aedd3ba638db466f9c0842 (diff) | |
download | the-abstraction-engine-d08e0105cbc59c6cc804f04aaf1e4e625a13960c.tar.gz the-abstraction-engine-d08e0105cbc59c6cc804f04aaf1e4e625a13960c.zip |
eyes follow cursor
Diffstat (limited to 'src/engine/TheAbstractionEngine.ts')
-rw-r--r-- | src/engine/TheAbstractionEngine.ts | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/engine/TheAbstractionEngine.ts b/src/engine/TheAbstractionEngine.ts index e720293..76ca7e9 100644 --- a/src/engine/TheAbstractionEngine.ts +++ b/src/engine/TheAbstractionEngine.ts @@ -1,7 +1,7 @@ import { Game } from "."; import { loadAssets } from "./config"; import { Player } from "./entities"; -import { Render } from "./systems"; +import { FacingDirection, Input, Render } from "./systems"; export class TheAbstractionEngine { private game: Game; @@ -17,7 +17,14 @@ export class TheAbstractionEngine { public async init() { await loadAssets(); - [new Render(this.ctx)].forEach((system) => this.game.addSystem(system)); + const inputSystem = new Input(); + this.addWindowEventListenersToInputSystem(inputSystem); + + const facingDirectionSystem = new FacingDirection(inputSystem); + + [new Render(this.ctx), inputSystem, facingDirectionSystem].forEach( + (system) => this.game.addSystem(system), + ); const player = new Player(); this.game.addEntity(player); @@ -39,4 +46,31 @@ export class TheAbstractionEngine { this.animationFrameId = null; } } + + private addWindowEventListenersToInputSystem(input: Input) { + window.addEventListener("keydown", (e) => { + if (!e.repeat) { + input.keyPressed(e.key.toLowerCase()); + } + }); + + window.addEventListener("keyup", (e) => + input.keyReleased(e.key.toLowerCase()), + ); + + window.addEventListener("blur", () => input.clearKeys()); + + window.addEventListener("mousemove", (e) => { + const canvas = this.ctx.canvas; + const rect = canvas.getBoundingClientRect(); + + const scaleX = canvas.width / rect.width; + const scaleY = canvas.height / rect.height; + + const x = (e.clientX - rect.left) * scaleX; + const y = (e.clientY - rect.top) * scaleY; + + input.setMousePosition({ x, y }); + }); + } } |