35 lines
840 B
TypeScript
35 lines
840 B
TypeScript
import { type Component } from "../components";
|
|
|
|
export abstract class Entity {
|
|
static Id = 0;
|
|
|
|
public id: string;
|
|
public components: Map<string, Component>;
|
|
public name: string;
|
|
|
|
constructor(name: string, id: string = (Entity.Id++).toString()) {
|
|
this.name = name;
|
|
this.id = id;
|
|
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);
|
|
}
|
|
}
|