From 2d412eedb557092aaa69ca937e2f07a616b7fad9 Mon Sep 17 00:00:00 2001 From: hypercross Date: Mon, 20 Apr 2026 15:11:45 +0800 Subject: [PATCH] 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. --- packages/framework/src/ui/PhaserBridge.tsx | 36 ++++++++++++---------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/framework/src/ui/PhaserBridge.tsx b/packages/framework/src/ui/PhaserBridge.tsx index 9a643ad..150d75a 100644 --- a/packages/framework/src/ui/PhaserBridge.tsx +++ b/packages/framework/src/ui/PhaserBridge.tsx @@ -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 { - sceneKey: string; - scene: ReactiveScene; + sceneKey?: string; + scene: ReactiveScene | { new (): ReactiveScene }; data?: TData; children?: any; } @@ -214,16 +214,18 @@ export function PhaserScene(props: PhaserSceneProps) { 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 () => {