diff options
Diffstat (limited to 'src/engine/entities')
-rw-r--r-- | src/engine/entities/EntityNames.ts | 1 | ||||
-rw-r--r-- | src/engine/entities/FunctionApplication.ts | 7 | ||||
-rw-r--r-- | src/engine/entities/FunctionBox.ts | 40 | ||||
-rw-r--r-- | src/engine/entities/LambdaFactory.ts | 24 |
4 files changed, 43 insertions, 29 deletions
diff --git a/src/engine/entities/EntityNames.ts b/src/engine/entities/EntityNames.ts index 7fc69c6..3f6d26f 100644 --- a/src/engine/entities/EntityNames.ts +++ b/src/engine/entities/EntityNames.ts @@ -6,4 +6,5 @@ export namespace EntityNames { export const Key = "Key"; export const LockedDoor = "LockedDoor"; export const Curry = "Curry"; + export const FunctionApplication = "FunctionApplication"; } diff --git a/src/engine/entities/FunctionApplication.ts b/src/engine/entities/FunctionApplication.ts new file mode 100644 index 0000000..31e3490 --- /dev/null +++ b/src/engine/entities/FunctionApplication.ts @@ -0,0 +1,7 @@ +import { Entity, EntityNames } from "."; + +export class FunctionApplication extends Entity { + constructor() { + super(EntityNames.FunctionApplication); + } +} diff --git a/src/engine/entities/FunctionBox.ts b/src/engine/entities/FunctionBox.ts index e51eb2b..92f1908 100644 --- a/src/engine/entities/FunctionBox.ts +++ b/src/engine/entities/FunctionBox.ts @@ -12,6 +12,7 @@ import { Grid, Highlight, Interactable, + LambdaTerm, Pushable, Sprite, } from "../components"; @@ -23,13 +24,9 @@ export class FunctionBox extends Entity { Sprites.FUNCTION_BOX, ) as SpriteSpec; - private code: string; - constructor(gridPosition: Coord2D, code: string) { super(EntityNames.FunctionBox); - this.code = code; - this.addComponent( new BoundingBox( { @@ -61,20 +58,19 @@ export class FunctionBox extends Entity { ), ); - this.addComponent( - new Highlight( - (_direction) => this.onHighlight(), - () => this.onUnhighlight(), - ), - ); + this.addComponent(new LambdaTerm(code)); + + this.addComponent(makeLambdaTermHighlightComponent(this)); } +} - private onUnhighlight() { +export const makeLambdaTermHighlightComponent = (entity: Entity) => { + const onUnhighlight = () => { closeModal(); - this.removeComponent(ComponentNames.Interactable); - } + entity.removeComponent(ComponentNames.Interactable); + }; - private onHighlight() { + const onHighlight = () => { let modalOpen = false; const interaction = () => { if (modalOpen) { @@ -82,10 +78,14 @@ export class FunctionBox extends Entity { closeModal(); return; } - modalOpen = true; + + const code = entity.getComponent<LambdaTerm>( + ComponentNames.LambdaTerm, + )!.code; openModal( - `<div style="text-align:center"><p>${this.code}</p> <br> <button id="close">Close</button></div>`, + `<div style="text-align:center"><p>${code}</p> <br> <button id="close">Close</button></div>`, ); + modalOpen = true; document.getElementById("close")!.addEventListener("click", () => { closeModal(); @@ -93,6 +93,8 @@ export class FunctionBox extends Entity { }); }; - this.addComponent(new Interactable(interaction)); - } -} + entity.addComponent(new Interactable(interaction)); + }; + + return new Highlight(onHighlight, onUnhighlight); +}; diff --git a/src/engine/entities/LambdaFactory.ts b/src/engine/entities/LambdaFactory.ts index 1c897eb..a0f5749 100644 --- a/src/engine/entities/LambdaFactory.ts +++ b/src/engine/entities/LambdaFactory.ts @@ -5,15 +5,15 @@ import { SpriteSpec, Sprites, } from "../config"; -import { Entity, EntityNames } from "."; +import { Entity, EntityNames, FunctionBox } from "."; import { BoundingBox, Colliding, ComponentNames, Grid, + GridSpawn, Highlight, Interactable, - LambdaSpawn, Sprite, Text, } from "../components"; @@ -75,11 +75,13 @@ export class LambdaFactory extends Entity { private codeEditorState: CodeEditorState | null; private spawns: number; + private code: string; constructor(gridPosition: Coord2D, code: string, spawns: number) { super(EntityNames.LambdaFactory); this.spawns = spawns; + this.code = code; this.codeEditorState = null; this.addComponent( @@ -100,7 +102,12 @@ export class LambdaFactory extends Entity { this.addComponent(new Colliding()); - this.addComponent(new LambdaSpawn(this.spawns, code)); + this.addComponent( + new GridSpawn( + this.spawns, + () => new FunctionBox({ x: 0, y: 0 }, this.code), + ), + ); this.addComponent(new Grid(gridPosition)); @@ -131,8 +138,8 @@ export class LambdaFactory extends Entity { } private spawnNewLambda(direction: Direction) { - const spawner = this.getComponent<LambdaSpawn>(ComponentNames.LambdaSpawn); - spawner.spawn(direction); + const spawner = this.getComponent<GridSpawn>(ComponentNames.GridSpawn); + spawner.spawnEntity(direction); const text = this.getComponent<Text>(ComponentNames.Text); text.text = spawner.spawnsLeft.toString(); @@ -144,9 +151,8 @@ export class LambdaFactory extends Entity { "<div class='code'><div id='code'></div><br><p id='syntax-error' class='error'></p><button id='close-modal'>Save</button></div>"; openModal(modalContent); - const { code } = this.getComponent<LambdaSpawn>(ComponentNames.LambdaSpawn); const startState = EditorState.create({ - doc: code, + doc: this.code, extensions: [ basicSetup, keymap.of(defaultKeymap), @@ -236,9 +242,7 @@ export class LambdaFactory extends Entity { return; } - const spawner = this.getComponent<LambdaSpawn>(ComponentNames.LambdaSpawn); - spawner.code = text; - this.addComponent(spawner); + this.code = text; view.destroy(); editorElement.innerHTML = ""; |