update: various improvements
This commit is contained in:
@@ -35,7 +35,7 @@ export interface BindRegionOptions<TPart extends Part> {
|
||||
export function bindRegion<TState, TMeta>(
|
||||
state: MutableSignal<TState>,
|
||||
partsGetter: (state: TState) => Record<string, Part<TMeta>>,
|
||||
region: Region,
|
||||
regionGetter: (state: TState) => Region,
|
||||
options: BindRegionOptions<Part<TMeta>>,
|
||||
container: Phaser.GameObjects.Container,
|
||||
): { cleanup: () => void; objects: Map<string, Phaser.GameObjects.GameObject> } {
|
||||
@@ -44,9 +44,11 @@ export function bindRegion<TState, TMeta>(
|
||||
const offset = options.offset ?? { x: 0, y: 0 };
|
||||
|
||||
const dispose = effect(function(this: { dispose: () => void }) {
|
||||
const parts = partsGetter(state.value);
|
||||
const currentState = state.value;
|
||||
const parts = partsGetter(currentState);
|
||||
const region = regionGetter(currentState);
|
||||
const currentIds = new Set(region.childIds);
|
||||
|
||||
|
||||
// 移除不在 region 中的对象
|
||||
for (const [id, obj] of objects) {
|
||||
if (!currentIds.has(id)) {
|
||||
|
||||
@@ -4,16 +4,22 @@ import type { IGameContext, PromptEvent } from 'boardgame-core';
|
||||
export interface InputMapperOptions<TState extends Record<string, unknown>> {
|
||||
scene: Phaser.Scene;
|
||||
commands: IGameContext<TState>['commands'];
|
||||
activePrompt: { current: PromptEvent | null };
|
||||
onSubmitPrompt: (input: string) => string | null;
|
||||
}
|
||||
|
||||
export class InputMapper<TState extends Record<string, unknown>> {
|
||||
private scene: Phaser.Scene;
|
||||
private commands: IGameContext<TState>['commands'];
|
||||
private activePrompt: { current: PromptEvent | null };
|
||||
private onSubmitPrompt: (input: string) => string | null;
|
||||
private pointerDownCallback: ((pointer: Phaser.Input.Pointer) => void) | null = null;
|
||||
|
||||
constructor(options: InputMapperOptions<TState>) {
|
||||
this.scene = options.scene;
|
||||
this.commands = options.commands;
|
||||
this.activePrompt = options.activePrompt;
|
||||
this.onSubmitPrompt = options.onSubmitPrompt;
|
||||
}
|
||||
|
||||
mapGridClick(
|
||||
@@ -35,7 +41,12 @@ export class InputMapper<TState extends Record<string, unknown>> {
|
||||
|
||||
const cmd = onCellClick(col, row);
|
||||
if (cmd) {
|
||||
this.commands.run(cmd);
|
||||
// 如果有活跃的 prompt,通过 submit 提交
|
||||
if (this.activePrompt.current) {
|
||||
this.onSubmitPrompt(cmd);
|
||||
} else {
|
||||
this.commands.run(cmd);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -148,8 +159,10 @@ export class PromptHandler<TState extends Record<string, unknown>> {
|
||||
export function createInputMapper<TState extends Record<string, unknown>>(
|
||||
scene: Phaser.Scene,
|
||||
commands: IGameContext<TState>['commands'],
|
||||
activePrompt: { current: PromptEvent | null },
|
||||
onSubmitPrompt: (input: string) => string | null,
|
||||
): InputMapper<TState> {
|
||||
return new InputMapper({ scene, commands });
|
||||
return new InputMapper({ scene, commands, activePrompt, onSubmitPrompt });
|
||||
}
|
||||
|
||||
export function createPromptHandler<TState extends Record<string, unknown>>(
|
||||
|
||||
Reference in New Issue
Block a user