我正处于 React+Redux 游戏的早期开发阶段,并遵循了 Redux 最佳实践:纯 reducer、展示/容器组件分离、getState()
仅在 Reducer 中使用(而不是在动作创建者中)等。该应用程序似乎正在工作预期但是当我尝试使用 Time Travel 反转操作时,即使 state 属性map[][]
及其计算的连接组件属性按预期更改,结果也不会正确反映在 UI 上(特别是地图上的玩家位置不会遵循国家规定)。当我检查状态变化时,我可以看到所有必要的变化都在不同状态之间正确发生。这是我的减速器:
const gridReducer = (state, action) => {
if (typeof state === 'undefined'){
let dungeon = new Dungeon();
dungeon.generate();
return {
boardWidth: Math.floor(((70/100) * window.innerWidth) / 20),
boardHeight: Math.floor(((70/100) * window.innerHeight) / 20),
map: dungeon.map,
position: dungeon.playerPosition
}
}
switch (action.type) {
case 'GRID_RESIZE':
return {...state,
boardWidth: action.newBoardWidth,
boardHeight: action.newBoardHeight
}
//This is where I have the issue, map correctly changes both when interacting with the game and when reversing using time travel however the UI fails to update (only in reverse)!
case 'MOVE':
let dungeonObj = new Dungeon(state.map.slice(), {...state.position});
if (dungeonObj.movePlayer(action.direction)) {
return {...state,
position: dungeonObj.playerPosition,
map: dungeonObj.map
}
} else return state;
default:
return state;
}
}
如果您想看一下,这是完整的代码!该应用程序目前仅支持通过按箭头键在地牢中移动玩家,并且视图应该始终基于玩家的位置居中(使用时间旅行时玩家无法向后移动) http://s.codepen。 io/sabahang/调试/GjrPNQ
PS:Dungeon.generate 确实使用了 Math.Random 但我只在initialState
调度操作中使用这个函数我只是通过将当前状态发送到 Dungeon 构造函数并使用它的其他方法来制作生成地图的浅表副本(例如.movePlayer)