diff options
Diffstat (limited to 'src/engine/entities')
-rw-r--r-- | src/engine/entities/Entity.ts | 12 | ||||
-rw-r--r-- | src/engine/entities/FunctionBox.ts | 27 |
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!"); } } |