refactor: misc
This commit is contained in:
parent
72e159e52b
commit
76687de672
|
|
@ -1,5 +1,4 @@
|
||||||
import Phaser from 'phaser';
|
import type { BoopState } from '@/game/boop';
|
||||||
import type { BoopState, PlayerType, PieceType } from '@/game/boop';
|
|
||||||
import { GameHostScene } from 'boardgame-phaser';
|
import { GameHostScene } from 'boardgame-phaser';
|
||||||
import { commands } from '@/game/boop';
|
import { commands } from '@/game/boop';
|
||||||
import { BoardRenderer } from './BoardRenderer';
|
import { BoardRenderer } from './BoardRenderer';
|
||||||
|
|
@ -36,7 +35,7 @@ export class GameScene extends GameHostScene<BoopState> {
|
||||||
});
|
});
|
||||||
|
|
||||||
// 监听状态变化
|
// 监听状态变化
|
||||||
this.watch(() => {
|
this.addEffect(() => {
|
||||||
const winner = this.state.winner;
|
const winner = this.state.winner;
|
||||||
if (winner) {
|
if (winner) {
|
||||||
this.winnerOverlay.show(winner);
|
this.winnerOverlay.show(winner);
|
||||||
|
|
@ -45,7 +44,7 @@ export class GameScene extends GameHostScene<BoopState> {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.watch(() => {
|
this.addEffect(() => {
|
||||||
const currentPlayer = this.state.currentPlayer;
|
const currentPlayer = this.state.currentPlayer;
|
||||||
this.boardRenderer.updateTurnText(currentPlayer, this.state);
|
this.boardRenderer.updateTurnText(currentPlayer, this.state);
|
||||||
this.supplyUI.update(this.state);
|
this.supplyUI.update(this.state);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import Phaser from 'phaser';
|
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 type { GameHost } from 'boardgame-core';
|
||||||
import { DisposableBag, type IDisposable } from '../utils';
|
import { DisposableBag, type IDisposable } from '../utils';
|
||||||
|
|
||||||
|
|
@ -14,7 +14,10 @@ export abstract class GameHostScene<TState extends Record<string, unknown>>
|
||||||
implements IDisposable
|
implements IDisposable
|
||||||
{
|
{
|
||||||
protected disposables = new DisposableBag();
|
protected disposables = new DisposableBag();
|
||||||
protected gameHost!: GameHost<TState>;
|
private _gameHost!: GameHost<TState>;
|
||||||
|
public get gameHost(): GameHost<TState> {
|
||||||
|
return this._gameHost;
|
||||||
|
}
|
||||||
|
|
||||||
addInterruption(promise: Promise<void>){
|
addInterruption(promise: Promise<void>){
|
||||||
this.gameHost?.addInterruption(promise);
|
this.gameHost?.addInterruption(promise);
|
||||||
|
|
@ -27,7 +30,7 @@ export abstract class GameHostScene<TState extends Record<string, unknown>>
|
||||||
}
|
}
|
||||||
|
|
||||||
init(data: GameHostSceneOptions<TState>): void {
|
init(data: GameHostSceneOptions<TState>): void {
|
||||||
this.gameHost = data.gameHost;
|
this._gameHost = data.gameHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
create(): void {
|
create(): void {
|
||||||
|
|
@ -43,8 +46,11 @@ export abstract class GameHostScene<TState extends Record<string, unknown>>
|
||||||
return this.gameHost.state.value;
|
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));
|
this.disposables.add(effect(fn));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,3 @@
|
||||||
import { h, Fragment } from 'preact';
|
|
||||||
import { effect } from '@preact/signals-core';
|
|
||||||
|
|
||||||
type DisposeFn = () => void;
|
|
||||||
|
|
||||||
export interface GameUIOptions {
|
export interface GameUIOptions {
|
||||||
container: HTMLElement;
|
container: HTMLElement;
|
||||||
root: any;
|
root: any;
|
||||||
|
|
@ -11,7 +6,6 @@ export interface GameUIOptions {
|
||||||
export class GameUI {
|
export class GameUI {
|
||||||
private container: HTMLElement;
|
private container: HTMLElement;
|
||||||
private root: any;
|
private root: any;
|
||||||
private effects: DisposeFn[] = [];
|
|
||||||
|
|
||||||
constructor(options: GameUIOptions) {
|
constructor(options: GameUIOptions) {
|
||||||
this.container = options.container;
|
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 {
|
unmount(): void {
|
||||||
import('preact').then(({ render }) => {
|
import('preact').then(({ render }) => {
|
||||||
render(null, this.container);
|
render(null, this.container);
|
||||||
});
|
});
|
||||||
for (const e of this.effects) {
|
|
||||||
e();
|
|
||||||
}
|
|
||||||
this.effects = [];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue