import { HeroEquipment } from './equipment'; import { HeroItems } from './items'; import { HeroLocation } from './location'; import { IHeroAttribute, IHeroEquipment, IHeroItems, IHeroLocation, IHeroModifier, IHeroState, IHeroStateSave, IReadonlyHeroAttribute, IHeroChangeFloorInfo, IHeroStateHooks } from './types'; import { FaceDirection, IDataCommon, IFacedTileLocator, SaveCompression } from '@user/data-common'; import { Hookable, HookController, IFaceHandler, IHookController } from '@motajs/common'; import { HeroAttribute } from './attribute'; export class HeroState extends Hookable implements IHeroState { location!: IHeroLocation; attribute!: IHeroAttribute; readonly items: IHeroItems; readonly equip: IHeroEquipment; constructor(readonly state: IDataCommon) { super(); this.items = new HeroItems(state); this.equip = new HeroEquipment(this.items.equipment, this.attribute); } protected createController( hook: Partial ): IHookController { return new HookController(this, hook); } createLocation( location: IFacedTileLocator, faceHandler: IFaceHandler ): void { if (this.location) return; this.location = new HeroLocation(this.state, location, faceHandler); } createAttribute(attribute: THero): void { if (this.attribute) return; this.attribute = new HeroAttribute(attribute); } getLocation(): IFacedTileLocator { return { x: this.location.x, y: this.location.y, direction: this.location.mover.faceDirection }; } //#region 属性相关 getModifiableAttribute(): IHeroAttribute { return this.attribute; } getAttribute(): IReadonlyHeroAttribute { return this.attribute; } getIsolatedAttribute(): IHeroAttribute { return this.attribute.getModifiableClone(); } registerModifier( type: string, cons: () => IHeroModifier ): void { this.attribute.registerModifier(type, cons); } createModifier(type: string): IHeroModifier | null { return this.attribute.createModifier(type); } createAndInsertModifier( type: string, name: K ): IHeroModifier | null { return this.attribute.createAndInsertModifier(type, name); } //#endregion async changeFloor(info: IHeroChangeFloorInfo): Promise { await Promise.all( this.forEachHook(hook => hook.onBeforeChangeFloor?.(info)) ); const mover = this.location.mover; mover.setFaceDir(info.face); mover.setMoveDir(info.face); mover.setPos(info.x, info.y); this.location.setFloor(info.target); await Promise.all( this.forEachHook(hook => hook.onAfterChangeFloor?.(info)) ); } saveState(compression: SaveCompression): IHeroStateSave { return { attribute: this.attribute.saveState(compression), location: this.location.saveState(compression), items: this.items.saveState(compression), equip: this.equip.saveState(compression) }; } loadState( state: IHeroStateSave, compression: SaveCompression ): void { // 属性原地读档:不替换实例,使装备与战斗侧持有的引用跨读档始终有效 this.attribute.loadState(state.attribute, compression); this.location.loadState(state.location, compression); this.items.loadState(state.items, compression); this.equip.loadState(state.equip, compression); } }