template/packages-user/data-state/src/event/event.ts
unanmed f0fd2f54a1 refactor(event): class-based built-ins, shared utils, single registration
- rewrite every built-in as EventXxx implements BuiltInFunction<P, E>
  (setBlock/moveBlock/deleteBlock/moveHero/moveHeroStep/touchFront/
  insertEvents/insertEvent), matching the EventSetBlock reference style
- move shared helpers to event/utils.ts (getPossibleMap, getPossibleLayer,
  getEventExecutor, enter/exitEventInsert); utils is not exported via index
- move per-class helpers into private methods (EventTouchFront and
  EventInsertEvents collectInvocations)
- use IObjectMover.push for move sequences instead of manual step calls
- registrations.ts: one createEventRegistrations() writing the eight entries
  in order, categories separated by single-line comments
- remove EventBuiltinName; classes own short string names
- update event tests; core.ts uses createEventRegistrations
2026-09-13 19:17:32 +08:00

77 lines
2.2 KiB
TypeScript

import { BuiltInFunction } from '@motajs/anon-tokyo';
import {
IBlockEventEnv,
IBlockEventParam,
IGameEventInvocation
} from '@user/data-system';
import { IGameEventStore } from '@user/data-common';
import { IInsertEventEventParam, IInsertEventsEventParam } from './types';
import { enterEventInsert, exitEventInsert, getEventExecutor } from './utils';
export class EventInsertEvents implements BuiltInFunction<
IInsertEventsEventParam,
IBlockEventEnv
> {
name: string = 'insertEvents';
async func(param: IInsertEventsEventParam, env: IBlockEventEnv) {
if (param.ids.length === 0) return;
const executor = getEventExecutor(env);
if (!executor) return;
const store = env.state.eventStore;
if (!store) return;
if (!enterEventInsert(env)) return;
try {
const invocations = this.collectInvocations(param.ids, env, store);
if (invocations.length === 0) return;
await executor.execute<void>(invocations, { custom: {} });
} finally {
exitEventInsert(env);
}
}
/**
* 过滤存在的事件 id 并构造临时事件调用
*/
private collectInvocations(
ids: readonly string[],
env: IBlockEventEnv,
store: IGameEventStore
): IGameEventInvocation[] {
const invocations: IGameEventInvocation[] = [];
for (const id of ids) {
if (
!id ||
!store.getEvent<IBlockEventParam, IBlockEventEnv, void>(id)
) {
continue;
}
invocations.push({ id, env });
}
return invocations;
}
}
export class EventInsertEvent implements BuiltInFunction<
IInsertEventEventParam,
IBlockEventEnv
> {
name: string = 'insertEvent';
async func(param: IInsertEventEventParam, env: IBlockEventEnv) {
if (param.length === 0) return;
const executor = getEventExecutor(env);
if (!executor) return;
if (!enterEventInsert(env)) return;
try {
await executor.interpreter.exec(param, { custom: {} }, env);
} finally {
exitEventInsert(env);
}
}
}