summaryrefslogtreecommitdiff
path: root/src/engine/entities
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-02 01:07:55 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-02 01:07:55 -0700
commit1ec5a8d088f599d094f387abc6014f228607b605 (patch)
tree0d0e0271b6d5db354a56337c5de9df1a933e767b /src/engine/entities
parenta333ce8845de4269d6a6f5523d2273da11fe271c (diff)
downloadthe-abstraction-engine-1ec5a8d088f599d094f387abc6014f228607b605.tar.gz
the-abstraction-engine-1ec5a8d088f599d094f387abc6014f228607b605.zip
add interactable component
Diffstat (limited to 'src/engine/entities')
-rw-r--r--src/engine/entities/Entity.ts12
-rw-r--r--src/engine/entities/FunctionBox.ts27
2 files changed, 37 insertions, 2 deletions
diff --git a/src/engine/entities/Entity.ts b/src/engine/entities/Entity.ts
index 2cc2ac3..d5a8e6e 100644
--- a/src/engine/entities/Entity.ts
+++ b/src/engine/entities/Entity.ts
@@ -7,14 +7,26 @@ export abstract class Entity {
public components: Map<string, Component>;
public name: string;
+ protected hooks: Map<string, { add: Function; remove: Function }>;
+
constructor(name: string, id: string = (Entity.Id++).toString()) {
this.name = name;
this.id = id;
this.components = new Map();
+ this.hooks = new Map();
}
public addComponent(component: Component) {
+ const hadBeforeSet = this.components.has(component.name);
this.components.set(component.name, component);
+ if (!hadBeforeSet) {
+ this.hooks.get(component.name)?.add();
+ }
+ }
+
+ public removeComponent(name: string) {
+ this.components.delete(name);
+ this.hooks.get(name)?.remove();
}
public getComponent<T extends Component>(name: string): T {
diff --git a/src/engine/entities/FunctionBox.ts b/src/engine/entities/FunctionBox.ts
index e6c41c2..393514e 100644
--- a/src/engine/entities/FunctionBox.ts
+++ b/src/engine/entities/FunctionBox.ts
@@ -1,6 +1,12 @@
import { IMAGES, SPRITE_SPECS, SpriteSpec, Sprites } from "../config";
import { Entity, EntityNames } from ".";
-import { BoundingBox, Grid, Sprite } from "../components";
+import {
+ BoundingBox,
+ ComponentNames,
+ Grid,
+ Interactable,
+ Sprite,
+} from "../components";
import { Coord2D } from "../interfaces";
export class FunctionBox extends Entity {
@@ -8,9 +14,13 @@ export class FunctionBox extends Entity {
Sprites.FUNCTION_BOX,
) as SpriteSpec;
- constructor(gridPosition: Coord2D) {
+ private code: string;
+
+ constructor(gridPosition: Coord2D, code: string) {
super(EntityNames.FunctionBox);
+ this.code = code;
+
this.addComponent(
new BoundingBox(
{
@@ -39,5 +49,18 @@ export class FunctionBox extends Entity {
FunctionBox.spriteSpec.frames,
),
);
+
+ this.hooks.set(ComponentNames.Highlight, {
+ add: () => {
+ this.addComponent(new Interactable(() => this.viewInsides()));
+ },
+ remove: () => {
+ this.removeComponent(ComponentNames.Interactable);
+ },
+ });
+ }
+
+ public viewInsides() {
+ console.log("I am a function box!");
}
}