template/packages-user/data-base/src/map/staticTile.ts
2026-09-30 00:00:20 +08:00

65 lines
1.7 KiB
TypeScript

import { ITileRawData, shouldReplay } from '@user/data-common';
import {
IDynamicTile,
IMapLayer,
IStaticBlockSave,
IStaticTile
} from './types';
import { MapTileBase } from './tile';
export class StaticTile
extends MapTileBase<IStaticBlockSave>
implements IStaticTile
{
constructor(x: number, y: number, layer: IMapLayer) {
super(x, y, layer);
this.restoreDefaultEvents();
}
num(): number {
return this.layer.getBlock(this.locator.x, this.locator.y);
}
raw(): ITileRawData | null {
return this.state.tileStore.getData(this.num());
}
@shouldReplay('Setting static tile num should be replayed.')
set(num: number): void {
this.layer.setBlock(num, this.locator.x, this.locator.y);
this.restoreDefaultEvents();
}
@shouldReplay('Transfering static tile to dynamic should be replayed.')
toDynamic(): IDynamicTile {
return this.layer.transferToDynamic(this.locator.x, this.locator.y)!;
}
shouldSave(): boolean {
const eventView = this.tileEvent();
return eventView.dirty();
}
saveState(): Readonly<IStaticBlockSave> {
const eventView = this.tileEvent();
if (eventView.dirty()) {
return {
events: new Map(eventView.get())
};
} else {
return {};
}
}
loadState(save: Readonly<IStaticBlockSave>): void {
this.restoreDefaultEvents();
if (save.events) {
const eventView = this.tileEvent();
eventView.clear();
for (const [priority, id] of save.events) {
eventView.set(priority, id);
}
}
}
}