diff options
Diffstat (limited to 'src/engine/entities')
-rw-r--r-- | src/engine/entities/EntityNames.ts | 1 | ||||
-rw-r--r-- | src/engine/entities/FunctionBox.ts | 2 | ||||
-rw-r--r-- | src/engine/entities/LambdaFactory.ts | 109 | ||||
-rw-r--r-- | src/engine/entities/index.ts | 1 |
4 files changed, 112 insertions, 1 deletions
diff --git a/src/engine/entities/EntityNames.ts b/src/engine/entities/EntityNames.ts index ffcc937..3ad31d0 100644 --- a/src/engine/entities/EntityNames.ts +++ b/src/engine/entities/EntityNames.ts @@ -2,4 +2,5 @@ export namespace EntityNames { export const Player = "Player"; export const FunctionBox = "FunctionBox"; export const Wall = "Wall"; + export const LambdaFactory = "LambdaFactory"; } diff --git a/src/engine/entities/FunctionBox.ts b/src/engine/entities/FunctionBox.ts index e5d031a..b7015f2 100644 --- a/src/engine/entities/FunctionBox.ts +++ b/src/engine/entities/FunctionBox.ts @@ -57,7 +57,7 @@ export class FunctionBox extends Entity { this.addComponent( new Highlight( - () => this.onHighlight(), + (_direction) => this.onHighlight(), () => this.onUnhighlight(), ), ); 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<LambdaSpawn>( + ComponentNames.LambdaSpawn, + ); + spawner.spawn(direction); + + const text = this.getComponent<Text>(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)); + } +} diff --git a/src/engine/entities/index.ts b/src/engine/entities/index.ts index e63b272..a049350 100644 --- a/src/engine/entities/index.ts +++ b/src/engine/entities/index.ts @@ -3,3 +3,4 @@ export * from "./EntityNames"; export * from "./Player"; export * from "./FunctionBox"; export * from "./Wall"; +export * from "./LambdaFactory"; |