refactor: wait to launch scene
This commit is contained in:
parent
fbf3f5e636
commit
70334fa9e3
|
|
@ -38,16 +38,8 @@ export interface PhaserGameProps {
|
||||||
children?: any;
|
children?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 存储待注册的场景配置 */
|
|
||||||
interface SceneRegistration<TData extends Record<string, unknown> = {}> {
|
|
||||||
sceneKey: string;
|
|
||||||
scene: ReactiveScene<TData>;
|
|
||||||
initData?: TData;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function PhaserGame(props: PhaserGameProps) {
|
export function PhaserGame(props: PhaserGameProps) {
|
||||||
const gameSignal = useSignal<PhaserGameContext>({ game: undefined!, sceneController: undefined! });
|
const gameSignal = useSignal<PhaserGameContext>({ game: undefined!, sceneController: undefined! });
|
||||||
const scenesRef = useRef<Map<string, SceneRegistration>>(new Map());
|
|
||||||
const initialSceneLaunched = useRef(false);
|
const initialSceneLaunched = useRef(false);
|
||||||
|
|
||||||
useSignalEffect(() => {
|
useSignalEffect(() => {
|
||||||
|
|
@ -72,8 +64,15 @@ export function PhaserGame(props: PhaserGameProps) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 等待场景注册完成(最多等待 100ms)
|
||||||
|
let retries = 0;
|
||||||
|
while (!phaserGame.scene.getScene(sceneKey) && retries < 10) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 10));
|
||||||
|
retries++;
|
||||||
|
}
|
||||||
|
|
||||||
// 验证场景是否已注册
|
// 验证场景是否已注册
|
||||||
if (!phaserGame.scene.getScene(sceneKey) && !scenesRef.current.has(sceneKey)) {
|
if (!phaserGame.scene.getScene(sceneKey)) {
|
||||||
console.error(`SceneController: 场景 "${sceneKey}" 未注册`);
|
console.error(`SceneController: 场景 "${sceneKey}" 未注册`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -90,20 +89,11 @@ export function PhaserGame(props: PhaserGameProps) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 确保场景已注册后再启动
|
// 确保场景已注册后再启动
|
||||||
|
// (场景应该已经在 PhaserScene 组件中注册)
|
||||||
if (!phaserGame.scene.getScene(sceneKey)) {
|
if (!phaserGame.scene.getScene(sceneKey)) {
|
||||||
const registration = scenesRef.current.get(sceneKey);
|
console.error(`SceneController: 场景 "${sceneKey}" 在切换时仍未注册`);
|
||||||
if (registration) {
|
isTransitioning.value = false;
|
||||||
phaserGame.scene.add(
|
return;
|
||||||
sceneKey,
|
|
||||||
registration.scene,
|
|
||||||
false,
|
|
||||||
{
|
|
||||||
...registration.initData,
|
|
||||||
phaserGame: gameSignal,
|
|
||||||
sceneController,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启动新场景
|
// 启动新场景
|
||||||
|
|
@ -122,7 +112,6 @@ export function PhaserGame(props: PhaserGameProps) {
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
gameSignal.value = { game: undefined!, sceneController: undefined! };
|
gameSignal.value = { game: undefined!, sceneController: undefined! };
|
||||||
scenesRef.current.clear();
|
|
||||||
initialSceneLaunched.current = false;
|
initialSceneLaunched.current = false;
|
||||||
phaserGame.destroy(true);
|
phaserGame.destroy(true);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue