Refactor simulation state management using dedicated controller
This commit is contained in:
@@ -3957,96 +3957,29 @@ export function App({ store, initialStatusMessage }: AppProps) {
|
|||||||
}, [editorState.projectDocument.time]);
|
}, [editorState.projectDocument.time]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setEditorSimulationClockOverride((currentClock) => {
|
return editorSimulationController.subscribeUiSnapshot(
|
||||||
if (currentClock === null) {
|
setEditorSimulationSnapshot
|
||||||
return null;
|
);
|
||||||
}
|
}, [editorSimulationController]);
|
||||||
|
|
||||||
const reconfiguredClock = reconfigureRuntimeClockState(
|
useEffect(() => {
|
||||||
currentClock,
|
editorSimulationController.updateInputs({
|
||||||
editorState.projectDocument.time
|
document: editorState.document,
|
||||||
);
|
loadedModelAssets
|
||||||
|
|
||||||
return areRuntimeClockStatesEqual(currentClock, reconfiguredClock)
|
|
||||||
? currentClock
|
|
||||||
: reconfiguredClock;
|
|
||||||
});
|
});
|
||||||
}, [editorState.projectDocument.time.dayLengthMinutes]);
|
}, [editorSimulationController, editorState.document, loadedModelAssets]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (editorState.toolMode !== "play") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setEditorSimulationPlaying(false);
|
|
||||||
}, [editorState.toolMode]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (editorState.toolMode === "play" || !editorSimulationPlaying) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let animationFrame = 0;
|
|
||||||
let previousFrameTime: number | null = null;
|
|
||||||
|
|
||||||
const tick = (timestamp: number) => {
|
|
||||||
if (previousFrameTime !== null) {
|
|
||||||
const dtSeconds = Math.min(
|
|
||||||
0.25,
|
|
||||||
(timestamp - previousFrameTime) / 1000
|
|
||||||
);
|
|
||||||
setEditorSimulationClockOverride((currentClock) =>
|
|
||||||
advanceRuntimeClockState(
|
|
||||||
currentClock ??
|
|
||||||
createRuntimeClockState(editorState.projectDocument.time),
|
|
||||||
dtSeconds
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
previousFrameTime = timestamp;
|
|
||||||
animationFrame = window.requestAnimationFrame(tick);
|
|
||||||
};
|
|
||||||
|
|
||||||
animationFrame = window.requestAnimationFrame(tick);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
if (animationFrame !== 0) {
|
|
||||||
window.cancelAnimationFrame(animationFrame);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}, [
|
|
||||||
editorSimulationPlaying,
|
|
||||||
editorState.projectDocument.time,
|
|
||||||
editorState.toolMode
|
|
||||||
]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (editorState.toolMode === "play") {
|
if (editorState.toolMode === "play") {
|
||||||
return;
|
editorSimulationController.pause();
|
||||||
}
|
}
|
||||||
|
}, [editorSimulationController, editorState.toolMode]);
|
||||||
|
|
||||||
try {
|
useEffect(() => {
|
||||||
const nextEditorSimulationScene = applyResolvedControlStateToRuntimeScene(
|
return () => {
|
||||||
buildRuntimeSceneFromDocument(editorState.document, {
|
editorSimulationController.dispose();
|
||||||
loadedModelAssets,
|
};
|
||||||
runtimeClock: editorSimulationClock
|
}, [editorSimulationController]);
|
||||||
})
|
|
||||||
);
|
|
||||||
setEditorSimulationScene(nextEditorSimulationScene);
|
|
||||||
setEditorSimulationMessage(null);
|
|
||||||
} catch (error) {
|
|
||||||
setEditorSimulationScene(null);
|
|
||||||
setEditorSimulationMessage(getErrorMessage(error));
|
|
||||||
}
|
|
||||||
}, [
|
|
||||||
editorSimulationClock.dayCount,
|
|
||||||
editorSimulationClock.dayLengthMinutes,
|
|
||||||
editorSimulationClock.timeOfDayHours,
|
|
||||||
editorState.document,
|
|
||||||
editorState.toolMode,
|
|
||||||
loadedModelAssets
|
|
||||||
]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (selectedScheduleRoutineId === null) {
|
if (selectedScheduleRoutineId === null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user