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