refactor: misc

This commit is contained in:
hypercross 2026-04-04 16:34:17 +08:00
parent 72e159e52b
commit 76687de672
3 changed files with 13 additions and 24 deletions

View File

@ -1,5 +1,4 @@
import Phaser from 'phaser';
import type { BoopState, PlayerType, PieceType } from '@/game/boop';
import type { BoopState } from '@/game/boop';
import { GameHostScene } from 'boardgame-phaser';
import { commands } from '@/game/boop';
import { BoardRenderer } from './BoardRenderer';
@ -36,7 +35,7 @@ export class GameScene extends GameHostScene<BoopState> {
});
// 监听状态变化
this.watch(() => {
this.addEffect(() => {
const winner = this.state.winner;
if (winner) {
this.winnerOverlay.show(winner);
@ -45,7 +44,7 @@ export class GameScene extends GameHostScene<BoopState> {
}
});
this.watch(() => {
this.addEffect(() => {
const currentPlayer = this.state.currentPlayer;
this.boardRenderer.updateTurnText(currentPlayer, this.state);
this.supplyUI.update(this.state);

View File

@ -1,5 +1,5 @@
import Phaser from 'phaser';
import { effect, type ReadonlySignal } from '@preact/signals-core';
import { effect } from '@preact/signals-core';
import type { GameHost } from 'boardgame-core';
import { DisposableBag, type IDisposable } from '../utils';
@ -14,7 +14,10 @@ export abstract class GameHostScene<TState extends Record<string, unknown>>
implements IDisposable
{
protected disposables = new DisposableBag();
protected gameHost!: GameHost<TState>;
private _gameHost!: GameHost<TState>;
public get gameHost(): GameHost<TState> {
return this._gameHost;
}
addInterruption(promise: Promise<void>){
this.gameHost?.addInterruption(promise);
@ -27,7 +30,7 @@ export abstract class GameHostScene<TState extends Record<string, unknown>>
}
init(data: GameHostSceneOptions<TState>): void {
this.gameHost = data.gameHost;
this._gameHost = data.gameHost;
}
create(): void {
@ -43,8 +46,11 @@ export abstract class GameHostScene<TState extends Record<string, unknown>>
return this.gameHost.state.value;
}
public addDisposable(disposable: IDisposable){
this.disposables.add(disposable);
}
/** 注册响应式监听(场景关闭时自动清理) */
protected watch(fn: () => CleanupFn): void {
public addEffect(fn: () => CleanupFn): void {
this.disposables.add(effect(fn));
}
}

View File

@ -1,8 +1,3 @@
import { h, Fragment } from 'preact';
import { effect } from '@preact/signals-core';
type DisposeFn = () => void;
export interface GameUIOptions {
container: HTMLElement;
root: any;
@ -11,7 +6,6 @@ export interface GameUIOptions {
export class GameUI {
private container: HTMLElement;
private root: any;
private effects: DisposeFn[] = [];
constructor(options: GameUIOptions) {
this.container = options.container;
@ -24,19 +18,9 @@ export class GameUI {
});
}
watch(fn: () => void): DisposeFn {
const e = effect(fn);
this.effects.push(e);
return e;
}
unmount(): void {
import('preact').then(({ render }) => {
render(null, this.container);
});
for (const e of this.effects) {
e();
}
this.effects = [];
}
}