refactor: scene control

This commit is contained in:
hypercross 2026-04-14 11:10:14 +08:00
parent c639218b53
commit c9acb7f228
5 changed files with 47 additions and 12 deletions

View File

@ -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,
};

View File

@ -167,20 +167,20 @@ export class GridViewerScene extends ReactiveScene {
const { width, height } = this.scale;
// Back button
this.createButton('返回菜单', 100, 40, () => {
this.scene.start('IndexScene');
this.createButton('返回菜单', 100, 40, async () => {
await this.sceneController.launch('IndexScene');
});
// Clear button
this.createButton('清空', width - 260, 40, () => {
this.createButton('清空', width - 260, 40, async () => {
this.inventory = createGridInventory(this.GRID_WIDTH, this.GRID_HEIGHT);
this.scene.restart();
await this.sceneController.restart();
});
// Random fill button
this.createButton('随机填充', width - 130, 40, () => {
this.createButton('随机填充', width - 130, 40, async () => {
this.randomFill();
this.scene.restart();
await this.sceneController.restart();
});
}

View File

@ -63,8 +63,8 @@ export class IndexScene extends ReactiveScene {
text.setScale(1);
});
bg.on('pointerdown', () => {
this.scene.start(targetScene);
bg.on('pointerdown', async () => {
await this.sceneController.launch(targetScene);
});
}
}

View File

@ -85,8 +85,8 @@ export class MapViewerScene extends ReactiveScene {
this.backButtonBg.setFillStyle(0x444466);
this.backButtonText.setScale(1);
});
this.backButtonBg.on('pointerdown', () => {
this.scene.start('IndexScene');
this.backButtonBg.on('pointerdown', async () => {
await this.sceneController.launch('IndexScene');
});
// Regenerate button

View File

@ -126,8 +126,8 @@ export class ShapeViewerScene extends ReactiveScene {
const { width, height } = this.scale;
// Back button
this.createButton('返回菜单', 100, height - 40, () => {
this.scene.start('IndexScene');
this.createButton('返回菜单', 100, height - 40, async () => {
await this.sceneController.launch('IndexScene');
});
// Info text