summaryrefslogtreecommitdiff
path: root/engine/Game.ts
diff options
context:
space:
mode:
Diffstat (limited to 'engine/Game.ts')
-rw-r--r--engine/Game.ts25
1 files changed, 19 insertions, 6 deletions
diff --git a/engine/Game.ts b/engine/Game.ts
index 3682fbd..07d06e8 100644
--- a/engine/Game.ts
+++ b/engine/Game.ts
@@ -12,6 +12,7 @@ export class Game {
public componentEntities: Map<string, Set<number>>;
constructor() {
+ this.lastTimeStamp = performance.now();
this.running = false;
this.systemOrder = [];
this.systems = new Map();
@@ -28,7 +29,7 @@ export class Game {
this.entities.set(entity.id, entity);
}
- public getEntity(id: number): Entity {
+ public getEntity(id: number): Entity | undefined {
return this.entities.get(id);
}
@@ -36,6 +37,18 @@ export class Game {
this.entities.delete(id);
}
+ public forEachEntityWithComponent(
+ componentName: string,
+ callback: (entity: Entity) => void,
+ ) {
+ this.componentEntities.get(componentName)?.forEach((entityId) => {
+ const entity = this.getEntity(entityId);
+ if (!entity) return;
+
+ callback(entity);
+ });
+ }
+
public addSystem(system: System) {
if (!this.systemOrder.includes(system.name)) {
this.systemOrder.push(system.name);
@@ -43,7 +56,7 @@ export class Game {
this.systems.set(system.name, system);
}
- public getSystem(name: string): System {
+ public getSystem(name: string): System | undefined {
return this.systems.get(name);
}
@@ -62,16 +75,16 @@ export class Game {
if (!this.componentEntities.has(component.name)) {
this.componentEntities.set(
component.name,
- new Set<number>([entity.id])
+ new Set<number>([entity.id]),
);
return;
}
- this.componentEntities.get(component.name).add(entity.id);
- })
+ this.componentEntities.get(component.name)?.add(entity.id);
+ }),
);
this.systemOrder.forEach((systemName) => {
- this.systems.get(systemName).update(dt, this);
+ this.systems.get(systemName)?.update(dt, this);
});
};
}