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.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/engine/entities/Entity.ts b/src/engine/entities/Entity.ts
new file mode 100644
index 0000000..18ee5d0
--- /dev/null
+++ b/src/engine/entities/Entity.ts
@@ -0,0 +1,34 @@
+import { type Component } from "../components";
+
+const randomId = () => (Math.random() * 1_000_000_000).toString();
+
+export abstract class Entity {
+ public id: string;
+ public components: Map<string, Component>;
+ public name: string;
+
+ constructor(name: string, id: string = randomId()) {
+ 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);
+ }
+}