2023-07-19 23:38:24 -04:00
|
|
|
import type { Component } from "../components";
|
|
|
|
|
|
|
|
export abstract class Entity {
|
2023-08-15 20:30:19 -04:00
|
|
|
public readonly id: string;
|
2023-07-19 23:38:24 -04:00
|
|
|
public readonly components: Map<string, Component>;
|
|
|
|
|
|
|
|
constructor() {
|
2023-08-15 20:30:19 -04:00
|
|
|
this.id = crypto.randomUUID();
|
2023-07-19 23:38:24 -04:00
|
|
|
this.components = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
public addComponent(component: Component) {
|
|
|
|
this.components.set(component.name, component);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getComponent<T extends Component>(name: string): T {
|
|
|
|
if (!this.hasComponent(name)) {
|
|
|
|
throw new Error("Entity does not have component " + name);
|
|
|
|
}
|
|
|
|
return this.components.get(name) as T;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getComponents(): Component[] {
|
|
|
|
return Array.from(this.components.values());
|
|
|
|
}
|
|
|
|
|
|
|
|
public hasComponent(name: string): boolean {
|
|
|
|
return this.components.has(name);
|
|
|
|
}
|
|
|
|
}
|