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