diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-02 06:00:47 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-03-02 06:07:42 -0700 |
commit | 4233aca561b5650924f3cc4232cfd294d706c863 (patch) | |
tree | af67bae5565cf1b7d5400d9678d1fcc038584438 | |
parent | cbb88091bdf69cc8752ef1cc3662dc0b99e3ead6 (diff) | |
download | the-abstraction-engine-4233aca561b5650924f3cc4232cfd294d706c863.tar.gz the-abstraction-engine-4233aca561b5650924f3cc4232cfd294d706c863.zip |
refocus canvas on lambda factory clsoe
-rw-r--r-- | src/components/GameCanvas.tsx | 11 | ||||
-rw-r--r-- | src/engine/TheAbstractionEngine.ts | 8 | ||||
-rw-r--r-- | src/engine/config/constants.ts | 5 | ||||
-rw-r--r-- | src/engine/entities/LambdaFactory.ts | 43 | ||||
-rw-r--r-- | src/engine/systems/Collision.ts | 2 |
5 files changed, 59 insertions, 10 deletions
diff --git a/src/components/GameCanvas.tsx b/src/components/GameCanvas.tsx index 0ea7180..09351e3 100644 --- a/src/components/GameCanvas.tsx +++ b/src/components/GameCanvas.tsx @@ -1,5 +1,6 @@ import { useState, useEffect, useRef } from "react"; import { TheAbstractionEngine, Game } from "../engine"; +import { Miscellaneous } from "../engine/config"; export interface GameCanvasProps { width: number; @@ -21,6 +22,8 @@ export const GameCanvas = ({ width, height }: GameCanvasProps) => { theAbstractionEngine.init().then(() => { theAbstractionEngine.play(); setGame(theAbstractionEngine); + + canvas.focus(); }); return () => theAbstractionEngine.stop(); @@ -30,7 +33,13 @@ export const GameCanvas = ({ width, height }: GameCanvasProps) => { return ( <div className="centered-game"> - <canvas ref={canvasRef} width={width} height={height}></canvas> + <canvas + id={Miscellaneous.CANVAS_ID} + tabIndex={1} + ref={canvasRef} + width={width} + height={height} + ></canvas> </div> ); }; diff --git a/src/engine/TheAbstractionEngine.ts b/src/engine/TheAbstractionEngine.ts index e84093f..9ea5b90 100644 --- a/src/engine/TheAbstractionEngine.ts +++ b/src/engine/TheAbstractionEngine.ts @@ -94,19 +94,19 @@ export class TheAbstractionEngine { } private addWindowEventListenersToInputSystem(input: Input) { - window.addEventListener("keydown", (e) => { + this.ctx.canvas.addEventListener("keydown", (e) => { if (!e.repeat) { input.keyPressed(e.key.toLowerCase()); } }); - window.addEventListener("keyup", (e) => + this.ctx.canvas.addEventListener("keyup", (e) => input.keyReleased(e.key.toLowerCase()), ); - window.addEventListener("blur", () => input.clearKeys()); + this.ctx.canvas.addEventListener("blur", () => input.clearKeys()); - window.addEventListener("mousemove", (e) => { + this.ctx.canvas.addEventListener("mousemove", (e) => { const canvas = this.ctx.canvas; const rect = canvas.getBoundingClientRect(); diff --git a/src/engine/config/constants.ts b/src/engine/config/constants.ts index 288513a..c6b592e 100644 --- a/src/engine/config/constants.ts +++ b/src/engine/config/constants.ts @@ -10,15 +10,19 @@ export enum Action { export namespace KeyConstants { export const KeyActions: Record<string, Action> = { a: Action.MOVE_LEFT, + h: Action.MOVE_LEFT, arrowleft: Action.MOVE_LEFT, d: Action.MOVE_RIGHT, + l: Action.MOVE_RIGHT, arrowright: Action.MOVE_RIGHT, w: Action.MOVE_UP, + k: Action.MOVE_UP, arrowup: Action.MOVE_UP, s: Action.MOVE_DOWN, + j: Action.MOVE_DOWN, arrowdown: Action.MOVE_DOWN, " ": Action.INTERACT, @@ -56,4 +60,5 @@ export namespace Miscellaneous { export const GRID_CELL_HEIGHT = Math.floor(HEIGHT / GRID_ROWS); export const MODAL_ID = "modal"; + export const CANVAS_ID = "canvas"; } diff --git a/src/engine/entities/LambdaFactory.ts b/src/engine/entities/LambdaFactory.ts index 1483b9d..4861c6b 100644 --- a/src/engine/entities/LambdaFactory.ts +++ b/src/engine/entities/LambdaFactory.ts @@ -1,4 +1,10 @@ -import { IMAGES, SPRITE_SPECS, SpriteSpec, Sprites } from "../config"; +import { + IMAGES, + Miscellaneous, + SPRITE_SPECS, + SpriteSpec, + Sprites, +} from "../config"; import { Entity, EntityNames } from "."; import { BoundingBox, @@ -94,16 +100,45 @@ export class LambdaFactory extends Entity { } let modalOpen = false; + const close = () => { + modalOpen = false; + closeModal(); + + const spawner = this.getComponent<LambdaSpawn>( + ComponentNames.LambdaSpawn, + ); + spawner.code = ( + document.getElementById("code") as HTMLTextAreaElement + ).value; + this.addComponent(spawner); + + document.getElementById(Miscellaneous.CANVAS_ID)!.focus(); + return; + }; + const interaction = () => { if (modalOpen) { - modalOpen = false; - closeModal(); + close(); return; } + modalOpen = true; - openModal(this.code); + openModal(this.codeEditor(this.code)); + + document.getElementById("close-modal")!.addEventListener("click", close); }; this.addComponent(new Interactable(interaction)); } + + private codeEditor(code: string) { + return ` + <div> + <textarea id="code" autofocus="autofocus" rows="10" cols="50"> + ${code} + </textarea> + <button id="close-modal">Close</button> + </div> + `; + } } diff --git a/src/engine/systems/Collision.ts b/src/engine/systems/Collision.ts index 7b1b963..1912fb6 100644 --- a/src/engine/systems/Collision.ts +++ b/src/engine/systems/Collision.ts @@ -1,7 +1,7 @@ import { System, SystemNames } from "."; import { Game } from ".."; import { Entity, EntityNames } from "../entities"; -import { BoundingBox, Colliding, ComponentNames, Grid } from "../components"; +import { BoundingBox, ComponentNames, Grid } from "../components"; const collisionMap: Record<string, Set<string>> = { [EntityNames.Key]: new Set([EntityNames.LockedDoor]), |