summaryrefslogtreecommitdiff
path: root/src/engine/entities/Entity.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/entities/Entity.ts')
-rw-r--r--src/engine/entities/Entity.ts12
1 files changed, 12 insertions, 0 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 {