Optimize object container
This commit is contained in:
parent
b5a2ae4d0e
commit
854e5a55a1
3 changed files with 37 additions and 7 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<T extends Command>(
|
||||
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<T extends Command>(commandType: new () => T): boolean {
|
||||
return this.commandExecutors.hasOwnProperty(commandType.name);
|
||||
public reactsToCommand(commandType: string): boolean {
|
||||
return this.commandExecutors.hasOwnProperty(commandType);
|
||||
}
|
||||
|
||||
public sendCommand(command: Command) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue