feat: add state management with full serialization and rebirth reset

This commit is contained in:
2026-05-13 03:31:49 +00:00
parent b09683e7f4
commit c78e4bba0b
2 changed files with 276 additions and 0 deletions
+125
View File
@@ -0,0 +1,125 @@
// ==================== 游戏状态管理 ====================
export function createInitialState() {
return {
// 时间
day: 0,
year: 0,
age: 0,
reincarnation: 0,
// 玩家状态
alive: true,
identity: null,
stats: { body: 0, wisdom: 0, charm: 0, destiny: 0, business: 0, intelligence: 0 },
talents: [],
careers: {},
money: 0,
// 商店与增益
shopItems: {},
activeBuffs: {},
// 神器与世界
artifacts: {},
worldFlags: {},
npcs: {},
// 入侵事件
invasionsTriggered: { military_40: false, spiritual_45: false, political_50: false, final_60: false },
// 突破路线
breakthroughRoute: null,
// 运行状态
speed: 0,
paused: false,
logs: [],
triggeredEvents: new Set(),
// 跨轮传承
metaExp: {},
memories: [],
unlockedEntries: new Set(),
history: [],
unlockedShopPool: new Set(),
};
}
export function resetForRebirth(state) {
// 保存本轮记录
const record = {
reincarnation: state.reincarnation,
age: state.age,
day: state.day,
careers: { ...state.careers },
identity: state.identity,
stats: { ...state.stats },
money: state.money,
};
state.history.push(record);
if (state.history.length > 50) {
state.history.shift();
}
// 保留跨轮数据
const preserved = {
reincarnation: state.reincarnation + 1,
metaExp: state.metaExp,
memories: state.memories,
unlockedEntries: state.unlockedEntries,
history: state.history,
artifacts: state.artifacts,
unlockedShopPool: state.unlockedShopPool,
};
// 重置本轮数据
state.day = 0;
state.year = 0;
state.age = 0;
state.alive = true;
state.careers = {};
state.money = 0;
state.shopItems = {};
state.activeBuffs = {};
state.worldFlags = {};
state.npcs = {};
state.logs = [];
state.triggeredEvents = new Set();
state.identity = null;
state.stats = { body: 0, wisdom: 0, charm: 0, destiny: 0, business: 0, intelligence: 0 };
state.talents = [];
state.invasionsTriggered = { military_40: false, spiritual_45: false, political_50: false, final_60: false };
state.breakthroughRoute = null;
state.speed = 0;
state.paused = false;
// 恢复跨轮数据
state.reincarnation = preserved.reincarnation;
state.metaExp = preserved.metaExp;
state.memories = preserved.memories;
state.unlockedEntries = preserved.unlockedEntries;
state.history = preserved.history;
state.artifacts = preserved.artifacts;
state.unlockedShopPool = preserved.unlockedShopPool;
return state;
}
export function serializeState(state) {
return {
...state,
triggeredEvents: Array.from(state.triggeredEvents),
unlockedEntries: Array.from(state.unlockedEntries),
unlockedShopPool: Array.from(state.unlockedShopPool),
};
}
export function deserializeState(data, target) {
Object.assign(target, data, {
triggeredEvents: new Set(data.triggeredEvents || []),
unlockedEntries: new Set(data.unlockedEntries || []),
unlockedShopPool: new Set(data.unlockedShopPool || []),
});
return target;
}