refactor: scene control
This commit is contained in:
parent
c639218b53
commit
c9acb7f228
|
|
@ -9,6 +9,8 @@ import { FadeScene as FadeSceneClass, FADE_SCENE_KEY } from '../scenes/FadeScene
|
||||||
export interface SceneController {
|
export interface SceneController {
|
||||||
/** 启动场景(带淡入淡出过渡) */
|
/** 启动场景(带淡入淡出过渡) */
|
||||||
launch(sceneKey: string): Promise<void>;
|
launch(sceneKey: string): Promise<void>;
|
||||||
|
/** 重新启动当前场景(带淡入淡出过渡) */
|
||||||
|
restart(): Promise<void>;
|
||||||
/** 当前活跃场景 key */
|
/** 当前活跃场景 key */
|
||||||
currentScene: ReadonlySignal<string | null>;
|
currentScene: ReadonlySignal<string | null>;
|
||||||
/** 是否正在过渡 */
|
/** 是否正在过渡 */
|
||||||
|
|
@ -105,6 +107,39 @@ export function PhaserGame(props: PhaserGameProps) {
|
||||||
await fade.fadeIn(300);
|
await fade.fadeIn(300);
|
||||||
isTransitioning.value = false;
|
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,
|
currentScene,
|
||||||
isTransitioning,
|
isTransitioning,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -167,20 +167,20 @@ export class GridViewerScene extends ReactiveScene {
|
||||||
const { width, height } = this.scale;
|
const { width, height } = this.scale;
|
||||||
|
|
||||||
// Back button
|
// Back button
|
||||||
this.createButton('返回菜单', 100, 40, () => {
|
this.createButton('返回菜单', 100, 40, async () => {
|
||||||
this.scene.start('IndexScene');
|
await this.sceneController.launch('IndexScene');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Clear button
|
// Clear button
|
||||||
this.createButton('清空', width - 260, 40, () => {
|
this.createButton('清空', width - 260, 40, async () => {
|
||||||
this.inventory = createGridInventory(this.GRID_WIDTH, this.GRID_HEIGHT);
|
this.inventory = createGridInventory(this.GRID_WIDTH, this.GRID_HEIGHT);
|
||||||
this.scene.restart();
|
await this.sceneController.restart();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Random fill button
|
// Random fill button
|
||||||
this.createButton('随机填充', width - 130, 40, () => {
|
this.createButton('随机填充', width - 130, 40, async () => {
|
||||||
this.randomFill();
|
this.randomFill();
|
||||||
this.scene.restart();
|
await this.sceneController.restart();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,8 +63,8 @@ export class IndexScene extends ReactiveScene {
|
||||||
text.setScale(1);
|
text.setScale(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
bg.on('pointerdown', () => {
|
bg.on('pointerdown', async () => {
|
||||||
this.scene.start(targetScene);
|
await this.sceneController.launch(targetScene);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,8 +85,8 @@ export class MapViewerScene extends ReactiveScene {
|
||||||
this.backButtonBg.setFillStyle(0x444466);
|
this.backButtonBg.setFillStyle(0x444466);
|
||||||
this.backButtonText.setScale(1);
|
this.backButtonText.setScale(1);
|
||||||
});
|
});
|
||||||
this.backButtonBg.on('pointerdown', () => {
|
this.backButtonBg.on('pointerdown', async () => {
|
||||||
this.scene.start('IndexScene');
|
await this.sceneController.launch('IndexScene');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Regenerate button
|
// Regenerate button
|
||||||
|
|
|
||||||
|
|
@ -126,8 +126,8 @@ export class ShapeViewerScene extends ReactiveScene {
|
||||||
const { width, height } = this.scale;
|
const { width, height } = this.scale;
|
||||||
|
|
||||||
// Back button
|
// Back button
|
||||||
this.createButton('返回菜单', 100, height - 40, () => {
|
this.createButton('返回菜单', 100, height - 40, async () => {
|
||||||
this.scene.start('IndexScene');
|
await this.sceneController.launch('IndexScene');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Info text
|
// Info text
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue