refactor: 地图存档的加载适配

This commit is contained in:
AncTe committed 2026-04-26 15:31:27 +08:00
1 parent 70a58ef4dc
commit c43bfa2ab0
10 files changed
+130 -53

No files matched your search

+101 -26
View File
@@ -16,16 +16,14 @@ import {
MotaDataLoader,
loading,
IRoleFaceBinder,
ILayerState,
LayerState,
RoleFaceBinder,
FaceDirection,
ISaveableContent,
IStateSaveData,
SaveCompression,
IReadonlyEnemy
IReadonlyEnemy,
IMapStore,
MapStore
} from '@user/data-base';
import { IEnemyAttr } from './enemy';
import {
CommonAuraConverter,
EnemyLegacyBridge,
@@ -34,16 +32,25 @@ import {
MainEnemyFinalEffect,
MainMapDamageConverter,
MainMapDamageReducer,
registerSpecials
registerSpecials,
MainEnemyComparer,
IEnemyAttr
} from './enemy';
import { HERO_DEFAULT_ATTRIBUTE, TILE_HEIGHT, TILE_WIDTH } from './shared';
import {
BG2_ZINDEX,
BG_ZINDEX,
EVENT_ZINDEX,
FG2_ZINDEX,
FG_ZINDEX,
HERO_DEFAULT_ATTRIBUTE,
TILE_HEIGHT,
TILE_WIDTH
} from './shared';
import { IHeroAttr } from './hero';
import { ILoadProgressTotal, LoadProgressTotal } from '@motajs/loader';
import { isNil } from 'lodash-es';
import { logger } from '@motajs/common';
import { ISaveSystem } from './save';
import { SaveSystem } from './save/system';
import { MainEnemyComparer } from './enemy/comparer';
import { ISaveSystem, SaveSystem } from './save';
export class CoreState implements ICoreState {
// 全局内容
@@ -52,7 +59,7 @@ export class CoreState implements ICoreState {
readonly numberIdMap: Map<number, string>;
// 可存档内容
readonly layer: ILayerState;
readonly maps: IMapStore;
readonly hero: IHeroState<IHeroAttr>;
readonly enemyManager: IEnemyManager<IEnemyAttr>;
readonly flags: IFlagSystem;
@@ -74,7 +81,7 @@ export class CoreState implements ICoreState {
> = new Map();
constructor() {
this.layer = new LayerState();
this.maps = new MapStore();
this.roleFace = new RoleFaceBinder();
this.idNumberMap = new Map();
this.numberIdMap = new Map();
@@ -149,9 +156,16 @@ export class CoreState implements ICoreState {
// 加载先使用兼容层实现
loading.once('loaded', () => {
this.initEnemyManager(enemys_fcae963b_31c9_42b4_b48c_bb48d09f3f80);
this.initMapStore(
core.floorIds,
core.floors as Record<FloorIds, ResolvedFloor>
);
});
this.addSaveableContent('flags', this.flags);
this.addSaveableContent('@system/hero', this.hero);
this.addSaveableContent('@system/flags', this.flags);
this.addSaveableContent('@system/maps', this.maps);
this.addSaveableContent('@system/enemy', this.enemyManager);
//#endregion
}
@@ -193,12 +207,86 @@ export class CoreState implements ICoreState {
manager.compareWith(reference);
}
private initMapStore(
floors: FloorIds[],
data: Record<FloorIds, ResolvedFloor>
) {
const reference = new Map<string, Map<number, Uint32Array>>();
for (const id of floors) {
const floor = data[id];
const state = this.maps.createLayerState(id);
const bg = state.addLayer(floor.width, floor.height);
const bg2 = state.addLayer(floor.width, floor.height);
const event = state.addLayer(floor.width, floor.height);
const fg = state.addLayer(floor.width, floor.height);
const fg2 = state.addLayer(floor.width, floor.height);
bg.setZIndex(BG_ZINDEX);
bg2.setZIndex(BG2_ZINDEX);
event.setZIndex(EVENT_ZINDEX);
fg.setZIndex(FG_ZINDEX);
fg2.setZIndex(FG2_ZINDEX);
state.setLayerAlias(bg, 'bg');
state.setLayerAlias(bg2, 'bg2');
state.setLayerAlias(event, 'event');
state.setLayerAlias(fg, 'fg');
state.setLayerAlias(fg2, 'fg2');
state.setActiveStatus(false);
const size = floor.width * floor.height;
const ref = new Map<number, Uint32Array>();
if (floor.bgmap && floor.bgmap.length > 0) {
const arr = new Uint32Array(floor.bgmap.flat());
bg.setMapRef(arr);
ref.set(BG_ZINDEX, new Uint32Array(arr));
} else {
ref.set(BG_ZINDEX, new Uint32Array(size));
}
if (floor.bg2map && floor.bg2map.length > 0) {
const arr = new Uint32Array(floor.bg2map.flat());
bg2.setMapRef(arr);
ref.set(BG2_ZINDEX, new Uint32Array(arr));
} else {
ref.set(BG2_ZINDEX, new Uint32Array(size));
}
if (floor.map && floor.map.length > 0) {
const arr = new Uint32Array(floor.map.flat());
event.setMapRef(arr);
ref.set(EVENT_ZINDEX, new Uint32Array(arr));
} else {
ref.set(EVENT_ZINDEX, new Uint32Array(size));
}
if (floor.fgmap && floor.fgmap.length > 0) {
const arr = new Uint32Array(floor.fgmap.flat());
fg.setMapRef(arr);
ref.set(FG_ZINDEX, new Uint32Array(arr));
} else {
ref.set(FG_ZINDEX, new Uint32Array(size));
}
if (floor.fg2map && floor.fg2map.length > 0) {
const arr = new Uint32Array(floor.fg2map.flat());
fg2.setMapRef(arr);
ref.set(FG2_ZINDEX, new Uint32Array(arr));
} else {
ref.set(FG2_ZINDEX, new Uint32Array(size));
}
reference.set(id, ref);
}
this.maps.compareWith(reference);
}
addSaveableContent(id: string, content: ISaveableContent<unknown>): void {
if (this.saveables.has(id)) {
logger.warn(112, id);
return;
}
this.saveables.set(id, content);
this.addedSaveables.add(content);
}
getSaveableContent<T>(id: string): ISaveableContent<T> | null {
@@ -222,17 +310,4 @@ export class CoreState implements ICoreState {
this.executors.set(content, executor);
}
}
saveState(): IStateSaveData {
return structuredClone({
followers: this.hero.mover.followers
});
}
loadState(data: IStateSaveData): void {
this.hero.mover.removeAllFollowers();
data.followers.forEach(v => {
this.hero.mover.addFollower(v.num, v.identifier);
});
}
}
@@ -1,5 +1,6 @@
export * from './aura';
export * from './calculator';
export * from './comparer';
export * from './damage';
export * from './final';
export * from './legacy';
-18
View File
@@ -1,27 +1,9 @@
import { FaceDirection, loading } from '@user/data-base';
import { isNil } from 'lodash-es';
import { ICoreState } from './types';
import { TILE_HEIGHT, TILE_WIDTH } from './shared';
import { state } from './ins';
function createCoreState(state: ICoreState) {
//#region 地图部分
const width = TILE_WIDTH;
const height = TILE_HEIGHT;
const bg = state.layer.addLayer(width, height);
const bg2 = state.layer.addLayer(width, height);
const event = state.layer.addLayer(width, height);
const fg = state.layer.addLayer(width, height);
const fg2 = state.layer.addLayer(width, height);
state.layer.setLayerAlias(bg, 'bg');
state.layer.setLayerAlias(bg2, 'bg2');
state.layer.setLayerAlias(event, 'event');
state.layer.setLayerAlias(fg, 'fg');
state.layer.setLayerAlias(fg2, 'fg2');
//#endregion
//#region 图块部分
const data = Object.entries(core.maps.blocksInfo);
@@ -1 +1,2 @@
export * from './system';
export * from './types';
+17
View File
@@ -1,10 +1,27 @@
import { IHeroAttr } from './hero';
//#region 地图相关
/** 每个地图的默认宽度 */
export const TILE_WIDTH = 13;
/** 每个地图的默认高度 */
export const TILE_HEIGHT = 13;
// 图层纵深,这些纵深与渲染系统的纵深没有关系,仅在地图图层之间生效
/** 背景层纵深 */
export const BG_ZINDEX = 0;
/** 背景层2纵深 */
export const BG2_ZINDEX = 10;
/** 事件层纵深 */
export const EVENT_ZINDEX = 20;
/** 前景层纵深 */
export const FG_ZINDEX = 30;
/** 前景层2纵深 */
export const FG2_ZINDEX = 40;
//#endregion
//#region 勇士相关
/** 默认的勇士图片 */
+2 -1
View File
@@ -30,7 +30,8 @@ export interface ICoreState extends IStateBase<IEnemyAttr, IHeroAttr> {
readonly saveSystem: ISaveSystem;
/**
* 将某个存档执行器绑定至指定的可存档对象,一个可存档对象只能绑定一个执行器,但一个执行器可以绑定多个可存档对象
* 将某个存档执行器绑定至指定的可存档对象,一个可存档对象只能绑定一个执行器,
* 但一个执行器可以绑定多个可存档对象,主要用来在读档后进行一些全局性的操作
* @param content 可存档对象或其注册 id
* @param executor 可存档对象对应的执行器
*/