From 4b9349b3f8bee21eb086cfd6e7668532a50e6048 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sat, 2 Mar 2024 04:02:20 -0700 Subject: add text on lambda factory --- src/engine/entities/LambdaFactory.ts | 109 +++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/engine/entities/LambdaFactory.ts (limited to 'src/engine/entities/LambdaFactory.ts') diff --git a/src/engine/entities/LambdaFactory.ts b/src/engine/entities/LambdaFactory.ts new file mode 100644 index 0000000..1483b9d --- /dev/null +++ b/src/engine/entities/LambdaFactory.ts @@ -0,0 +1,109 @@ +import { IMAGES, SPRITE_SPECS, SpriteSpec, Sprites } from "../config"; +import { Entity, EntityNames } from "."; +import { + BoundingBox, + Colliding, + ComponentNames, + Grid, + Highlight, + Interactable, + LambdaSpawn, + Sprite, + Text, +} from "../components"; +import { Coord2D, Direction } from "../interfaces"; +import { openModal, closeModal } from "../utils"; + +export class LambdaFactory extends Entity { + private static spriteSpec: SpriteSpec = SPRITE_SPECS.get( + Sprites.LAMBDA_FACTORY, + ) as SpriteSpec; + + private code: string; + private spawns: number; + + constructor(gridPosition: Coord2D, code: string, spawns: number) { + super(EntityNames.LambdaFactory); + + this.code = code; + this.spawns = spawns; + + this.addComponent( + new BoundingBox( + { + x: 0, + y: 0, + }, + { + width: LambdaFactory.spriteSpec.width, + height: LambdaFactory.spriteSpec.height, + }, + 0, + ), + ); + + this.addComponent(new Text(spawns.toString())); + + this.addComponent(new Colliding()); + + this.addComponent(new LambdaSpawn(this.spawns, this.code)); + + this.addComponent(new Grid(gridPosition)); + + this.addComponent( + new Sprite( + IMAGES.get(LambdaFactory.spriteSpec.sheet)!, + { x: 0, y: 0 }, + { + width: LambdaFactory.spriteSpec.width, + height: LambdaFactory.spriteSpec.height, + }, + LambdaFactory.spriteSpec.msPerFrame, + LambdaFactory.spriteSpec.frames, + ), + ); + + this.addComponent( + new Highlight( + (direction) => this.onHighlight(direction), + () => this.onUnhighlight(), + ), + ); + } + + private onUnhighlight() { + closeModal(); + this.removeComponent(ComponentNames.Interactable); + } + + private onHighlight(direction: Direction) { + if (direction === Direction.LEFT || direction === Direction.RIGHT) { + const interaction = () => { + const spawner = this.getComponent( + ComponentNames.LambdaSpawn, + ); + spawner.spawn(direction); + + const text = this.getComponent(ComponentNames.Text); + text.text = spawner.spawnsLeft.toString(); + this.addComponent(text); + }; + + this.addComponent(new Interactable(interaction)); + return; + } + + let modalOpen = false; + const interaction = () => { + if (modalOpen) { + modalOpen = false; + closeModal(); + return; + } + modalOpen = true; + openModal(this.code); + }; + + this.addComponent(new Interactable(interaction)); + } +} -- cgit v1.2.3-70-g09d2