summaryrefslogtreecommitdiff
path: root/src/engine/entities
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/entities')
-rw-r--r--src/engine/entities/EntityNames.ts1
-rw-r--r--src/engine/entities/FunctionApplication.ts16
-rw-r--r--src/engine/entities/FunctionBox.ts11
-rw-r--r--src/engine/entities/Sign.ts49
-rw-r--r--src/engine/entities/index.ts1
5 files changed, 70 insertions, 8 deletions
diff --git a/src/engine/entities/EntityNames.ts b/src/engine/entities/EntityNames.ts
index b4fbbb2..17b5519 100644
--- a/src/engine/entities/EntityNames.ts
+++ b/src/engine/entities/EntityNames.ts
@@ -10,4 +10,5 @@ export namespace EntityNames {
export const Particles = "Particles";
export const Portal = "Portal";
export const Grass = "Grass";
+ export const Sign = "Sign";
}
diff --git a/src/engine/entities/FunctionApplication.ts b/src/engine/entities/FunctionApplication.ts
index d907eca..175534c 100644
--- a/src/engine/entities/FunctionApplication.ts
+++ b/src/engine/entities/FunctionApplication.ts
@@ -34,6 +34,10 @@ import {
interpret,
} from "../../interpreter";
+const APPLICATION_RESULTS: Record<string, (gridPosition: Coord2D) => Entity> = {
+ _KEY: (gridPosition: Coord2D) => new Key(gridPosition),
+};
+
export class FunctionApplication extends Entity {
private static spriteSpec = SPRITE_SPECS.get(Sprites.BUBBLE) as SpriteSpec;
@@ -43,7 +47,9 @@ export class FunctionApplication extends Entity {
super(EntityNames.FunctionApplication);
this.symbolTable = new SymbolTable();
- this.symbolTable.add("key");
+ Object.keys(APPLICATION_RESULTS).forEach((key) => {
+ this.symbolTable.add(key);
+ });
const dimension = {
width: FunctionApplication.spriteSpec.width,
@@ -109,9 +115,10 @@ export class FunctionApplication extends Entity {
ComponentNames.LambdaTerm
);
const newCode = applicationTerm.code.replace("_INPUT", functionTerm.code);
+
let result: DebrujinifiedLambdaTerm | null = null;
try {
- result = interpret(newCode, this.symbolTable);
+ result = interpret(newCode, this.symbolTable, true);
} catch (e) {
console.error(e);
fail();
@@ -131,8 +138,9 @@ export class FunctionApplication extends Entity {
applicationResultingEntity = new FunctionBox(grid.gridPosition, code);
} else if ("name" in result) {
const { name } = result;
- if (name === "key") {
- applicationResultingEntity = new Key(grid.gridPosition);
+ const entityFactory = APPLICATION_RESULTS[name];
+ if (entityFactory) {
+ game.addEntity(entityFactory(nextPosition));
}
} else {
fail();
diff --git a/src/engine/entities/FunctionBox.ts b/src/engine/entities/FunctionBox.ts
index dac2174..7b70567 100644
--- a/src/engine/entities/FunctionBox.ts
+++ b/src/engine/entities/FunctionBox.ts
@@ -67,7 +67,10 @@ export class FunctionBox extends Entity {
}
}
-export const makeLambdaTermHighlightComponent = (entity: Entity) => {
+export const makeLambdaTermHighlightComponent = (
+ entity: Entity,
+ text?: string
+) => {
const onUnhighlight = () => {
closeModal();
entity.removeComponent(ComponentNames.Interactable);
@@ -87,9 +90,9 @@ export const makeLambdaTermHighlightComponent = (entity: Entity) => {
return;
}
- const code = entity.getComponent<LambdaTerm>(
- ComponentNames.LambdaTerm
- )!.code;
+ const code =
+ text ??
+ entity.getComponent<LambdaTerm>(ComponentNames.LambdaTerm)!.code;
openModal(
`<div style="text-align:center"><p>${code}</p> <br> <button id="close">Close</button></div>`
);
diff --git a/src/engine/entities/Sign.ts b/src/engine/entities/Sign.ts
new file mode 100644
index 0000000..a11fba6
--- /dev/null
+++ b/src/engine/entities/Sign.ts
@@ -0,0 +1,49 @@
+import { Entity, EntityNames, makeLambdaTermHighlightComponent } from ".";
+import { BoundingBox, Colliding, Grid, Sprite } from "../components";
+import { IMAGES, SPRITE_SPECS, SpriteSpec, Sprites } from "../config";
+import { Coord2D } from "../interfaces";
+
+export class Sign extends Entity {
+ private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
+ Sprites.SIGN
+ ) as SpriteSpec;
+
+ private text: string;
+
+ constructor(text: string, gridPosition: Coord2D) {
+ super(EntityNames.Sign);
+ this.text = text;
+
+ const dimension = {
+ width: Sign.spriteSpec.width,
+ height: Sign.spriteSpec.height,
+ };
+
+ this.addComponent(
+ new Sprite(
+ IMAGES.get(Sign.spriteSpec.sheet)!,
+ { x: 0, y: 0 },
+ dimension,
+ Sign.spriteSpec.msPerFrame,
+ Sign.spriteSpec.frames
+ )
+ );
+
+ this.addComponent(
+ new BoundingBox(
+ {
+ x: 0,
+ y: 0,
+ },
+ dimension,
+ 0
+ )
+ );
+
+ this.addComponent(new Grid(gridPosition));
+
+ this.addComponent(new Colliding());
+
+ this.addComponent(makeLambdaTermHighlightComponent(this, this.text));
+ }
+}
diff --git a/src/engine/entities/index.ts b/src/engine/entities/index.ts
index 260db5b..284a2bd 100644
--- a/src/engine/entities/index.ts
+++ b/src/engine/entities/index.ts
@@ -11,3 +11,4 @@ export * from "./FunctionApplication";
export * from "./Particles";
export * from "./Portal";
export * from "./Grass";
+export * from "./Sign";