From 1ec5a8d088f599d094f387abc6014f228607b605 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sat, 2 Mar 2024 01:07:55 -0700 Subject: add interactable component --- src/engine/entities/Entity.ts | 12 ++++++++++++ src/engine/entities/FunctionBox.ts | 27 +++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) (limited to 'src/engine/entities') 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; public name: string; + protected hooks: Map; + 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(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!"); } } -- cgit v1.2.3-70-g09d2