refactor: scene control
This commit is contained in:
@@ -9,6 +9,8 @@ import { FadeScene as FadeSceneClass, FADE_SCENE_KEY } from '../scenes/FadeScene
|
||||
export interface SceneController {
|
||||
/** 启动场景(带淡入淡出过渡) */
|
||||
launch(sceneKey: string): Promise<void>;
|
||||
/** 重新启动当前场景(带淡入淡出过渡) */
|
||||
restart(): Promise<void>;
|
||||
/** 当前活跃场景 key */
|
||||
currentScene: ReadonlySignal<string | null>;
|
||||
/** 是否正在过渡 */
|
||||
@@ -105,6 +107,39 @@ export function PhaserGame(props: PhaserGameProps) {
|
||||
await fade.fadeIn(300);
|
||||
isTransitioning.value = false;
|
||||
},
|
||||
async restart() {
|
||||
if (isTransitioning.value) {
|
||||
console.warn('SceneController: 正在进行场景切换');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!currentScene.value) {
|
||||
console.warn('SceneController: 没有当前场景,无法 restart');
|
||||
return;
|
||||
}
|
||||
|
||||
const sceneKey = currentScene.value;
|
||||
const scene = phaserGame.scene.getScene(sceneKey);
|
||||
|
||||
if (!scene) {
|
||||
console.error(`SceneController: 场景 "${sceneKey}" 不存在`);
|
||||
return;
|
||||
}
|
||||
|
||||
isTransitioning.value = true;
|
||||
const fade = phaserGame.scene.getScene(FADE_SCENE_KEY) as FadeSceneClass;
|
||||
|
||||
// 淡出到黑色
|
||||
phaserGame.scene.bringToTop(FADE_SCENE_KEY);
|
||||
await fade.fadeOut(300);
|
||||
|
||||
// 重启当前场景
|
||||
scene.scene.restart();
|
||||
|
||||
// 淡入
|
||||
await fade.fadeIn(300);
|
||||
isTransitioning.value = false;
|
||||
},
|
||||
currentScene,
|
||||
isTransitioning,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user