refactor: expose addInterruption

This commit is contained in:
hypercross 2026-04-04 15:40:18 +08:00
parent 0dbb4fc2ba
commit db83e89a46
2 changed files with 10 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import { GameHostScene } from 'boardgame-phaser';
import { spawnEffect, type Spawner } from 'boardgame-phaser'; import { spawnEffect, type Spawner } from 'boardgame-phaser';
import type { ReadonlySignal } from '@preact/signals-core'; import type { ReadonlySignal } from '@preact/signals-core';
import {commands} from "@/game/boop"; import {commands} from "@/game/boop";
import {MutableSignal} from "boardgame-core";
const BOARD_SIZE = 6; const BOARD_SIZE = 6;
const CELL_SIZE = 80; const CELL_SIZE = 80;
@ -38,7 +39,7 @@ export class GameScene extends GameHostScene<BoopState> {
this.drawGrid(); this.drawGrid();
this.createSupplyUI(); this.createSupplyUI();
this.disposables.add(spawnEffect(new BoopPartSpawner(this, this.gameHost.state))); this.disposables.add(spawnEffect(new BoopPartSpawner(this, this.gameHost.state as MutableSignal<BoopState>)));
this.watch(() => { this.watch(() => {
const winner = this.state.winner; const winner = this.state.winner;
@ -393,10 +394,10 @@ export class GameScene extends GameHostScene<BoopState> {
} }
class BoopPartSpawner implements Spawner<BoopPart, Phaser.GameObjects.Container> { class BoopPartSpawner implements Spawner<BoopPart, Phaser.GameObjects.Container> {
constructor(public readonly scene: GameScene, public readonly state: ReadonlySignal<BoopState>) {} constructor(public readonly scene: GameHostScene<BoopState>) {}
*getData() { *getData() {
for (const part of Object.values(this.state.value.pieces)) { for (const part of Object.values(this.scene.state.pieces)) {
yield part; yield part;
} }
} }
@ -452,6 +453,7 @@ class BoopPartSpawner implements Spawner<BoopPart, Phaser.GameObjects.Container>
duration: 200, duration: 200,
ease: 'Back.easeOut', ease: 'Back.easeOut',
}); });
this.state.addInterruption(new Promise(resolve => setTimeout(resolve, 200)));
return container; return container;
} }

View File

@ -16,6 +16,10 @@ export abstract class GameHostScene<TState extends Record<string, unknown>>
protected disposables = new DisposableBag(); protected disposables = new DisposableBag();
protected gameHost!: GameHost<TState>; protected gameHost!: GameHost<TState>;
addInterruption(promise: Promise<void>){
this.gameHost?.addInterruption(promise);
}
init(data: GameHostSceneOptions<TState>): void { init(data: GameHostSceneOptions<TState>): void {
this.gameHost = data.gameHost; this.gameHost = data.gameHost;
} }
@ -29,7 +33,7 @@ export abstract class GameHostScene<TState extends Record<string, unknown>>
} }
/** 获取当前状态的只读快照 */ /** 获取当前状态的只读快照 */
protected get state(): TState { public get state(): TState {
return this.gameHost.state.value; return this.gameHost.state.value;
} }