refactor(framework): improve PhaserScene flexibility

Allow `PhaserScene` to accept either a scene instance or a scene
constructor. This enables more flexible scene registration within
the `PhaserBridge`. Also updates `sceneKey` to be optional,
defaulting to the scene's internal key.
This commit is contained in:
hypercross 2026-04-20 15:11:45 +08:00
parent f32aca2543
commit 2d412eedb5
1 changed files with 19 additions and 17 deletions

View File

@ -1,15 +1,15 @@
import { signal, useSignal, useSignalEffect } from '@preact/signals'; import { signal, useSignal, useSignalEffect } from "@preact/signals";
import Phaser, { AUTO } from 'phaser'; import Phaser, { AUTO } from "phaser";
import { createContext } from 'preact'; import { createContext } from "preact";
import { useContext, useEffect, useRef } from 'preact/hooks'; import { useContext, useEffect, useRef } from "preact/hooks";
import { import {
FadeScene as FadeSceneClass, FadeScene as FadeSceneClass,
FADE_SCENE_KEY, FADE_SCENE_KEY,
} from '../scenes/FadeScene'; } from "../scenes/FadeScene";
import type { ReactiveScene } from '../scenes'; import type { ReactiveScene } from "../scenes";
import type { ReadonlySignal } from '@preact/signals-core'; import type { ReadonlySignal } from "@preact/signals-core";
export interface SceneController { export interface SceneController {
/** 启动场景(带淡入淡出过渡) */ /** 启动场景(带淡入淡出过渡) */
@ -34,8 +34,8 @@ export const defaultPhaserConfig: Phaser.Types.Core.GameConfig = {
type: AUTO, type: AUTO,
width: 560, width: 560,
height: 560, height: 560,
parent: 'phaser-container', parent: "phaser-container",
backgroundColor: '#f9fafb', backgroundColor: "#f9fafb",
scene: [], scene: [],
}; };
@ -71,7 +71,7 @@ export function PhaserGame(props: PhaserGameProps) {
const sceneController: SceneController = { const sceneController: SceneController = {
async launch(sceneKey: string) { async launch(sceneKey: string) {
if (isTransitioning.value) { if (isTransitioning.value) {
console.warn('SceneController: 正在进行场景切换'); console.warn("SceneController: 正在进行场景切换");
return; return;
} }
@ -120,12 +120,12 @@ export function PhaserGame(props: PhaserGameProps) {
}, },
async restart() { async restart() {
if (isTransitioning.value) { if (isTransitioning.value) {
console.warn('SceneController: 正在进行场景切换'); console.warn("SceneController: 正在进行场景切换");
return; return;
} }
if (!currentScene.value) { if (!currentScene.value) {
console.warn('SceneController: 没有当前场景,无法 restart'); console.warn("SceneController: 没有当前场景,无法 restart");
return; return;
} }
@ -192,8 +192,8 @@ export function PhaserGame(props: PhaserGameProps) {
} }
export interface PhaserSceneProps<TData = {}> { export interface PhaserSceneProps<TData = {}> {
sceneKey: string; sceneKey?: string;
scene: ReactiveScene<TData>; scene: ReactiveScene<TData> | { new (): ReactiveScene<TData> };
data?: TData; data?: TData;
children?: any; children?: any;
} }
@ -214,16 +214,18 @@ export function PhaserScene<TData = {}>(props: PhaserSceneProps<TData>) {
const game = ctx.game; const game = ctx.game;
// 注册场景到 Phaser但不启动 // 注册场景到 Phaser但不启动
if (!game.scene.getScene(props.sceneKey)) { const scene = "scene" in props.scene ? props.scene : new props.scene();
const sceneKey = props.sceneKey ?? scene.scene.key;
if (!game.scene.getScene(sceneKey)) {
const initData = { const initData = {
...props.data, ...props.data,
phaserGame: phaserGameSignal, phaserGame: phaserGameSignal,
sceneController: ctx.sceneController, sceneController: ctx.sceneController,
}; };
game.scene.add(props.sceneKey, props.scene, false, initData); game.scene.add(sceneKey, props.scene, false, initData);
} }
sceneSignal.value = props.scene; sceneSignal.value = scene;
registered.current = true; registered.current = true;
return () => { return () => {