From 854e5a55a1f7d24524e1fa2157b32fa1630fd547 Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Mon, 27 Jul 2020 18:50:57 +0200 Subject: [PATCH] Optimize object container --- frontend/src/index.ts | 10 ++++--- frontend/src/scripts/objects/game-object.ts | 7 ++--- .../src/scripts/objects/object-container.ts | 27 ++++++++++++++++++- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/frontend/src/index.ts b/frontend/src/index.ts index 93255b2..bebec70 100644 --- a/frontend/src/index.ts +++ b/frontend/src/index.ts @@ -1,9 +1,13 @@ -import './styles/main.scss'; +import { glMatrix } from 'gl-matrix'; import { Game } from './scripts/game'; import { applyArrayPlugins } from './scripts/helper/array'; -import { glMatrix } from 'gl-matrix'; +import './styles/main.scss'; glMatrix.setMatrixArrayType(Array); applyArrayPlugins(); -new Game(); +try { + new Game(); +} catch (e) { + alert(e); +} diff --git a/frontend/src/scripts/objects/game-object.ts b/frontend/src/scripts/objects/game-object.ts index a186716..dcefd56 100644 --- a/frontend/src/scripts/objects/game-object.ts +++ b/frontend/src/scripts/objects/game-object.ts @@ -18,9 +18,10 @@ export abstract class GameObject extends Typed implements CommandReceiver { } private commandExecutors: { - [commandName: string]: (e: Command) => void; + [commandType: string]: (e: Command) => void; } = {}; + // can only be called inside the constructor protected addCommandExecutor( commandType: new () => T, handler: (command: T) => void @@ -28,8 +29,8 @@ export abstract class GameObject extends Typed implements CommandReceiver { this.commandExecutors[commandType.name] = handler; } - public reactsToCommand(commandType: new () => T): boolean { - return this.commandExecutors.hasOwnProperty(commandType.name); + public reactsToCommand(commandType: string): boolean { + return this.commandExecutors.hasOwnProperty(commandType); } public sendCommand(command: Command) { diff --git a/frontend/src/scripts/objects/object-container.ts b/frontend/src/scripts/objects/object-container.ts index 332c127..ac44edf 100644 --- a/frontend/src/scripts/objects/object-container.ts +++ b/frontend/src/scripts/objects/object-container.ts @@ -5,9 +5,16 @@ import { GameObject } from './game-object'; export class ObjectContainer implements CommandReceiver { private objects: Map = new Map(); + private objectsGroupedByAbilities: Map> = 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); } }