2026-04-04 13:08:28 +08:00
|
|
|
|
import Phaser from 'phaser';
|
2026-04-12 17:29:02 +08:00
|
|
|
|
import { signal, useSignal, useSignalEffect } from '@preact/signals';
|
2026-04-04 13:08:28 +08:00
|
|
|
|
import { createContext, h } from 'preact';
|
2026-04-12 17:52:44 +08:00
|
|
|
|
import { useContext, useEffect, useRef } from 'preact/hooks';
|
|
|
|
|
|
import { ReadonlySignal } from "@preact/signals-core";
|
2026-04-12 17:29:02 +08:00
|
|
|
|
import type { ReactiveScene } from '../scenes';
|
2026-04-12 16:52:53 +08:00
|
|
|
|
import { FadeScene as FadeSceneClass, FADE_SCENE_KEY } from '../scenes/FadeScene';
|
|
|
|
|
|
|
|
|
|
|
|
export interface SceneController {
|
|
|
|
|
|
/** 启动场景(带淡入淡出过渡) */
|
2026-04-12 17:29:02 +08:00
|
|
|
|
launch(sceneKey: string): Promise<void>;
|
2026-04-12 16:52:53 +08:00
|
|
|
|
/** 当前活跃场景 key */
|
|
|
|
|
|
currentScene: ReadonlySignal<string | null>;
|
|
|
|
|
|
/** 是否正在过渡 */
|
|
|
|
|
|
isTransitioning: ReadonlySignal<boolean>;
|
|
|
|
|
|
}
|
2026-04-04 12:14:26 +08:00
|
|
|
|
|
2026-04-12 16:26:52 +08:00
|
|
|
|
export interface PhaserGameContext {
|
|
|
|
|
|
game: Phaser.Game;
|
2026-04-12 16:52:53 +08:00
|
|
|
|
sceneController: SceneController;
|
2026-04-12 16:26:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const phaserContext = createContext<ReadonlySignal<PhaserGameContext> | null>(null);
|
2026-04-04 12:14:26 +08:00
|
|
|
|
|
|
|
|
|
|
export const defaultPhaserConfig: Phaser.Types.Core.GameConfig = {
|
|
|
|
|
|
type: Phaser.AUTO,
|
|
|
|
|
|
width: 560,
|
|
|
|
|
|
height: 560,
|
|
|
|
|
|
parent: 'phaser-container',
|
|
|
|
|
|
backgroundColor: '#f9fafb',
|
|
|
|
|
|
scene: [],
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-04-04 13:08:28 +08:00
|
|
|
|
export interface PhaserGameProps {
|
|
|
|
|
|
config?: Partial<Phaser.Types.Core.GameConfig>;
|
2026-04-12 17:29:02 +08:00
|
|
|
|
/** 初始启动的场景 key */
|
|
|
|
|
|
initialScene?: string;
|
2026-04-04 13:08:28 +08:00
|
|
|
|
children?: any;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function PhaserGame(props: PhaserGameProps) {
|
2026-04-12 16:52:53 +08:00
|
|
|
|
const gameSignal = useSignal<PhaserGameContext>({ game: undefined!, sceneController: undefined! });
|
2026-04-12 17:52:44 +08:00
|
|
|
|
const initialSceneLaunched = useRef(false);
|
2026-04-04 13:08:28 +08:00
|
|
|
|
|
2026-04-04 12:14:26 +08:00
|
|
|
|
useSignalEffect(() => {
|
2026-04-04 14:47:03 +08:00
|
|
|
|
const config: Phaser.Types.Core.GameConfig = {
|
|
|
|
|
|
...defaultPhaserConfig,
|
|
|
|
|
|
...props.config,
|
|
|
|
|
|
};
|
|
|
|
|
|
const phaserGame = new Phaser.Game(config);
|
2026-04-12 16:52:53 +08:00
|
|
|
|
|
2026-04-12 18:11:23 +08:00
|
|
|
|
// 添加 FadeScene 并启动它来初始化 overlay
|
2026-04-12 16:52:53 +08:00
|
|
|
|
const fadeScene = new FadeSceneClass();
|
2026-04-12 18:11:23 +08:00
|
|
|
|
phaserGame.scene.add(FADE_SCENE_KEY, fadeScene, true); // 改为 true 以触发 create
|
2026-04-12 16:52:53 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建 SceneController
|
|
|
|
|
|
const currentScene = signal<string | null>(null);
|
|
|
|
|
|
const isTransitioning = signal(false);
|
|
|
|
|
|
|
|
|
|
|
|
const sceneController: SceneController = {
|
2026-04-12 17:29:02 +08:00
|
|
|
|
async launch(sceneKey: string) {
|
2026-04-12 16:52:53 +08:00
|
|
|
|
if (isTransitioning.value) {
|
|
|
|
|
|
console.warn('SceneController: 正在进行场景切换');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 18:00:08 +08:00
|
|
|
|
// 等待场景注册完成(最多等待 100ms)
|
|
|
|
|
|
let retries = 0;
|
|
|
|
|
|
while (!phaserGame.scene.getScene(sceneKey) && retries < 10) {
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
|
|
|
|
retries++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 17:52:44 +08:00
|
|
|
|
// 验证场景是否已注册
|
2026-04-12 18:00:08 +08:00
|
|
|
|
if (!phaserGame.scene.getScene(sceneKey)) {
|
2026-04-12 17:52:44 +08:00
|
|
|
|
console.error(`SceneController: 场景 "${sceneKey}" 未注册`);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 16:52:53 +08:00
|
|
|
|
isTransitioning.value = true;
|
|
|
|
|
|
const fade = phaserGame.scene.getScene(FADE_SCENE_KEY) as FadeSceneClass;
|
|
|
|
|
|
|
|
|
|
|
|
// 淡出到黑色
|
|
|
|
|
|
await fade.fadeOut(300);
|
|
|
|
|
|
|
|
|
|
|
|
// 停止当前场景
|
|
|
|
|
|
if (currentScene.value) {
|
|
|
|
|
|
phaserGame.scene.stop(currentScene.value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 17:52:44 +08:00
|
|
|
|
// 确保场景已注册后再启动
|
2026-04-12 18:00:08 +08:00
|
|
|
|
// (场景应该已经在 PhaserScene 组件中注册)
|
2026-04-12 17:52:44 +08:00
|
|
|
|
if (!phaserGame.scene.getScene(sceneKey)) {
|
2026-04-12 18:00:08 +08:00
|
|
|
|
console.error(`SceneController: 场景 "${sceneKey}" 在切换时仍未注册`);
|
|
|
|
|
|
isTransitioning.value = false;
|
|
|
|
|
|
return;
|
2026-04-12 17:52:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 16:52:53 +08:00
|
|
|
|
// 启动新场景
|
2026-04-12 17:29:02 +08:00
|
|
|
|
phaserGame.scene.start(sceneKey);
|
2026-04-12 16:52:53 +08:00
|
|
|
|
currentScene.value = sceneKey;
|
|
|
|
|
|
|
|
|
|
|
|
// 淡入
|
|
|
|
|
|
await fade.fadeIn(300);
|
|
|
|
|
|
isTransitioning.value = false;
|
|
|
|
|
|
},
|
|
|
|
|
|
currentScene,
|
|
|
|
|
|
isTransitioning,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
gameSignal.value = { game: phaserGame, sceneController };
|
2026-04-04 13:08:28 +08:00
|
|
|
|
|
2026-04-04 12:14:26 +08:00
|
|
|
|
return () => {
|
2026-04-12 16:52:53 +08:00
|
|
|
|
gameSignal.value = { game: undefined!, sceneController: undefined! };
|
2026-04-12 17:52:44 +08:00
|
|
|
|
initialSceneLaunched.current = false;
|
2026-04-04 12:14:26 +08:00
|
|
|
|
phaserGame.destroy(true);
|
2026-04-04 13:08:28 +08:00
|
|
|
|
};
|
2026-04-04 12:14:26 +08:00
|
|
|
|
});
|
2026-04-04 13:08:28 +08:00
|
|
|
|
|
2026-04-12 17:52:44 +08:00
|
|
|
|
// 启动初始场景(仅一次)
|
|
|
|
|
|
useEffect(() => {
|
2026-04-12 17:29:02 +08:00
|
|
|
|
const ctx = gameSignal.value;
|
2026-04-12 17:52:44 +08:00
|
|
|
|
if (!initialSceneLaunched.current && props.initialScene && ctx?.sceneController) {
|
|
|
|
|
|
initialSceneLaunched.current = true;
|
|
|
|
|
|
// 使用 microtask 确保所有子组件的场景注册已完成
|
|
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
|
|
ctx.sceneController.launch(props.initialScene!);
|
|
|
|
|
|
});
|
2026-04-12 17:29:02 +08:00
|
|
|
|
}
|
2026-04-12 17:52:44 +08:00
|
|
|
|
}, [gameSignal.value, props.initialScene]);
|
2026-04-12 17:29:02 +08:00
|
|
|
|
|
2026-04-04 13:08:28 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<div id="phaser-container" className="w-full h-full">
|
|
|
|
|
|
<phaserContext.Provider value={gameSignal}>
|
|
|
|
|
|
{props.children}
|
|
|
|
|
|
</phaserContext.Provider>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
2026-04-04 12:14:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 16:26:52 +08:00
|
|
|
|
export interface PhaserSceneProps<TData extends Record<string, unknown> = {}> {
|
2026-04-04 13:08:28 +08:00
|
|
|
|
sceneKey: string;
|
2026-04-12 16:26:52 +08:00
|
|
|
|
scene: ReactiveScene<TData>;
|
|
|
|
|
|
data?: TData;
|
2026-04-04 16:19:18 +08:00
|
|
|
|
children?: any;
|
2026-04-04 13:08:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 16:31:10 +08:00
|
|
|
|
export const phaserSceneContext = createContext<ReadonlySignal<ReactiveScene> | null>(null);
|
2026-04-12 17:52:44 +08:00
|
|
|
|
|
2026-04-12 16:26:52 +08:00
|
|
|
|
export function PhaserScene<TData extends Record<string, unknown> = {}>(props: PhaserSceneProps<TData>) {
|
|
|
|
|
|
const phaserGameSignal = useContext(phaserContext);
|
2026-04-12 16:52:53 +08:00
|
|
|
|
const sceneSignal = useSignal<ReactiveScene<TData>>();
|
2026-04-12 17:52:44 +08:00
|
|
|
|
const registered = useRef(false);
|
2026-04-04 13:08:28 +08:00
|
|
|
|
|
2026-04-04 12:14:26 +08:00
|
|
|
|
useSignalEffect(() => {
|
2026-04-12 16:26:52 +08:00
|
|
|
|
if (!phaserGameSignal) return;
|
|
|
|
|
|
const ctx = phaserGameSignal.value;
|
|
|
|
|
|
if (!ctx?.game) return;
|
|
|
|
|
|
|
|
|
|
|
|
const game = ctx.game;
|
2026-04-04 13:08:28 +08:00
|
|
|
|
|
2026-04-12 17:52:44 +08:00
|
|
|
|
// 注册场景到 Phaser(但不启动)
|
2026-04-12 16:52:53 +08:00
|
|
|
|
if (!game.scene.getScene(props.sceneKey)) {
|
2026-04-12 17:52:44 +08:00
|
|
|
|
const initData = {
|
|
|
|
|
|
...props.data,
|
|
|
|
|
|
phaserGame: phaserGameSignal,
|
|
|
|
|
|
sceneController: ctx.sceneController,
|
|
|
|
|
|
};
|
2026-04-12 16:52:53 +08:00
|
|
|
|
game.scene.add(props.sceneKey, props.scene, false, initData);
|
|
|
|
|
|
}
|
2026-04-12 17:52:44 +08:00
|
|
|
|
|
2026-04-12 16:52:53 +08:00
|
|
|
|
sceneSignal.value = props.scene;
|
2026-04-12 17:52:44 +08:00
|
|
|
|
registered.current = true;
|
2026-04-12 17:29:02 +08:00
|
|
|
|
|
2026-04-04 12:14:26 +08:00
|
|
|
|
return () => {
|
2026-04-04 16:19:18 +08:00
|
|
|
|
sceneSignal.value = undefined;
|
2026-04-12 17:52:44 +08:00
|
|
|
|
registered.current = false;
|
2026-04-12 16:52:53 +08:00
|
|
|
|
// 不在这里移除场景,让 SceneController 管理生命周期
|
2026-04-04 13:08:28 +08:00
|
|
|
|
};
|
2026-04-04 12:14:26 +08:00
|
|
|
|
});
|
2026-04-04 13:08:28 +08:00
|
|
|
|
|
2026-04-12 16:31:10 +08:00
|
|
|
|
return <phaserSceneContext.Provider value={sceneSignal as ReadonlySignal<ReactiveScene>}>{props.children}</phaserSceneContext.Provider>;
|
2026-04-04 13:08:28 +08:00
|
|
|
|
}
|