Optimize object container

This commit is contained in:
schmelczerandras 2020-07-27 18:50:57 +02:00
parent b5a2ae4d0e
commit 854e5a55a1
3 changed files with 37 additions and 7 deletions

View file

@ -5,9 +5,16 @@ import { GameObject } from './game-object';
export class ObjectContainer implements CommandReceiver {
private objects: Map<Id, GameObject> = new Map();
private objectsGroupedByAbilities: Map<string, Array<GameObject>> = new Map();
public addObject(o: GameObject) {
this.objects.set(o.id, o);
for (let command of this.objectsGroupedByAbilities.keys()) {
if (o.reactsToCommand(command)) {
this.objectsGroupedByAbilities.get(command).push(o);
}
}
}
public removeObject(o: GameObject) {
@ -19,6 +26,24 @@ export class ObjectContainer implements CommandReceiver {
}
public sendCommand(e: Command) {
this.objects.forEach((o, _) => o.sendCommand(e));
if (!this.objectsGroupedByAbilities.has(e.type)) {
this.createGroupForCommand(e.type);
}
this.objectsGroupedByAbilities
.get(e.type)
.forEach((o, _) => o.sendCommand(e));
}
private createGroupForCommand(commandType: string) {
const objectsReactingToCommand = [];
this.objects.forEach((o, _) => {
if (o.reactsToCommand(commandType)) {
objectsReactingToCommand.push(o);
}
});
this.objectsGroupedByAbilities.set(commandType, objectsReactingToCommand);
}
}