Files
core_ng/@sammo/game_logic/src/LazyEntityUpdater.ts
T

37 lines
1.2 KiB
TypeScript

import type { ValidEntity, EntityType } from "./Entity/index.js";
import type { IResourceController } from "./IResourceController.js";
export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
protected abstract readonly _indirectNames: ReadonlyArray<keyof Entity>;
protected abstract readonly _entityType: EntityType<Entity>;
protected abstract readonly _raw: Entity;
protected abstract readonly rsc: IResourceController;
public get raw(): Readonly<Entity> {
return this._raw;
}
public get entityType(): EntityType<Entity> {
return this._entityType;
}
protected forceUpdate(callback: (raw: Entity) => void) {
callback(this._raw);
this.rsc.reserveUpdate(this);
}
public update(callback: (raw: Entity) => void) {
const oldRaw = { ...this._raw };
callback(this._raw);
//for구문이 빠른가, Proxy setter로 제어하는게 빠른가
for (const indirectName of this._indirectNames) {
if (oldRaw[indirectName] !== this._raw[indirectName]) {
throw new Error(`${String(indirectName)} change by update() is not allowed`);
}
}
this.rsc.reserveUpdate(this);
}
}