mirror of
https://github.com/motajs/template.git
synced 2026-11-18 10:14:30 +08:00
feat: 录像记录 & CoreState 存储接口
This commit is contained in:
1 parent
5470f7a90e
commit
cee843981f
16 files changed
+156
-51
No files matched your search
@@ -24,7 +24,8 @@ import {
|
||||
IMapStore,
|
||||
MapStore,
|
||||
IReplaySystem,
|
||||
ReplaySystem
|
||||
ReplaySystem,
|
||||
ReplayCommandCode
|
||||
} from '@user/data-common';
|
||||
import {
|
||||
EnemyManager,
|
||||
@@ -83,7 +84,6 @@ import { isNil } from 'lodash-es';
|
||||
import { DefaultHeroMoveTopImpl } from './hero';
|
||||
import { createEventRegistrations } from './event/registrations';
|
||||
import {
|
||||
ReplayCommandCode,
|
||||
ReplayEquipCommand,
|
||||
ReplayMoveCommand,
|
||||
ReplayTeleportCommand,
|
||||
@@ -101,6 +101,7 @@ export class CoreState implements ICoreState {
|
||||
readonly mapStore: IMapStore;
|
||||
readonly eventStore: IGameEventStore;
|
||||
readonly directionMapper: IDirectionMapper;
|
||||
readonly replaySystem: IReplaySystem;
|
||||
|
||||
// Layer 1 数据层,所有可存档内容都在这,一般用于数据存储
|
||||
readonly maps: IMapState;
|
||||
@@ -112,7 +113,6 @@ export class CoreState implements ICoreState {
|
||||
readonly enemyContext: IEnemyContext<IEnemyAttr, IHeroAttr>;
|
||||
readonly eventSystem: IGameEventSystem;
|
||||
readonly pathfinding: IPathfindingSystem;
|
||||
readonly replaySystem: IReplaySystem;
|
||||
|
||||
// Layer 3 用户层,也就是最顶层的内容,一般仅用于初始化以及仅供渲染端调用的顶层模块
|
||||
readonly loadProgress: ILoadProgressTotal;
|
||||
@@ -505,6 +505,37 @@ export class CoreState implements ICoreState {
|
||||
}
|
||||
}
|
||||
|
||||
saveState(compression: SaveCompression): ReadonlyMap<string, unknown> {
|
||||
const result = new Map<string, unknown>();
|
||||
for (const [key, value] of this.saveables) {
|
||||
const content = value.saveState(compression);
|
||||
result.set(key, content);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
loadState(
|
||||
state: ReadonlyMap<string, unknown>,
|
||||
compression: SaveCompression
|
||||
): void {
|
||||
for (const [key, value] of this.saveables) {
|
||||
// 使用 has 判断是否在映射中,而非值的非空判断,因为空值也有可能是存档的一部分
|
||||
if (!state.has(key)) {
|
||||
logger.warn(177, key);
|
||||
continue;
|
||||
}
|
||||
const data = state.get(key);
|
||||
value.loadState(data, compression);
|
||||
}
|
||||
const loaded = new Set<string>(state.keys());
|
||||
const total = new Set(this.saveables.keys());
|
||||
const remain = total.difference(loaded);
|
||||
if (remain.size > 0) {
|
||||
const ids = [...remain].join(' | ');
|
||||
logger.warn(178, ids);
|
||||
}
|
||||
}
|
||||
|
||||
//#endregion
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ import {
|
||||
beginReplaySafetyCollection,
|
||||
endReplaySafetyCollection,
|
||||
logReplaySafetyDetail,
|
||||
shouldReplay
|
||||
shouldReplay,
|
||||
ReplayCommandCode
|
||||
} from '@user/data-common';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
@@ -20,7 +21,18 @@ import {
|
||||
ReplayUnequipCommand,
|
||||
ReplayUseItemCommand
|
||||
} from './commands';
|
||||
import { ReplayCommandCode, REPLAY_COMMAND_ORDER } from './types';
|
||||
|
||||
/** 供测试读取的稳定指令码顺序 */
|
||||
export const REPLAY_COMMAND_ORDER: readonly ReplayCommandCode[] = [
|
||||
ReplayCommandCode.Up,
|
||||
ReplayCommandCode.Right,
|
||||
ReplayCommandCode.Down,
|
||||
ReplayCommandCode.Left,
|
||||
ReplayCommandCode.Teleport,
|
||||
ReplayCommandCode.UseItem,
|
||||
ReplayCommandCode.Equip,
|
||||
ReplayCommandCode.Unequip
|
||||
];
|
||||
|
||||
function step(
|
||||
command: number,
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
export * from './commands';
|
||||
export * from './types';
|
||||
@@ -1,31 +0,0 @@
|
||||
/** 顶层拥有的稳定录像指令码,数值属于录像格式的一部分 */
|
||||
export const enum ReplayCommandCode {
|
||||
/** 向上移动一步 */
|
||||
Up = 0,
|
||||
/** 向右移动一步 */
|
||||
Right = 1,
|
||||
/** 向下移动一步 */
|
||||
Down = 2,
|
||||
/** 向左移动一步 */
|
||||
Left = 3,
|
||||
/** 自动寻路至目标点 */
|
||||
Teleport = 4,
|
||||
/** 使用道具 */
|
||||
UseItem = 5,
|
||||
/** 装备物品 */
|
||||
Equip = 6,
|
||||
/** 卸下装备 */
|
||||
Unequip = 7
|
||||
}
|
||||
|
||||
/** 供测试读取的稳定指令码顺序 */
|
||||
export const REPLAY_COMMAND_ORDER: readonly ReplayCommandCode[] = [
|
||||
ReplayCommandCode.Up,
|
||||
ReplayCommandCode.Right,
|
||||
ReplayCommandCode.Down,
|
||||
ReplayCommandCode.Left,
|
||||
ReplayCommandCode.Teleport,
|
||||
ReplayCommandCode.UseItem,
|
||||
ReplayCommandCode.Equip,
|
||||
ReplayCommandCode.Unequip
|
||||
];
|
||||
@@ -12,7 +12,8 @@ export interface ISaveableExecutor<T> {
|
||||
afterLoad(data: T, state: IStateBase): void;
|
||||
}
|
||||
|
||||
export interface ICoreState extends IStateSystem {
|
||||
export interface ICoreState
|
||||
extends IStateSystem, ISaveableContent<ReadonlyMap<string, unknown>> {
|
||||
/** 加载进度对象 */
|
||||
readonly loadProgress: ILoadProgressTotal;
|
||||
/** 数据端加载对象 */
|
||||
|
||||
Reference in new issue
Block a user