refactor: 移动顶层实施对象

This commit is contained in:
unanmed 2026-06-29 23:58:52 +08:00
parent 327bdc840b
commit 3d72954bb2
7 changed files with 157 additions and 186 deletions

View File

@ -1,4 +1,4 @@
import { ITileLocator } from '@motajs/common'; import { ITileLocator, logger } from '@motajs/common';
import { import {
FaceDirection, FaceDirection,
IFaceHandler, IFaceHandler,
@ -10,13 +10,11 @@ import {
} from '@user/data-common'; } from '@user/data-common';
import { import {
HeroMoveCode, HeroMoveCode,
IHeroHitAction,
IHeroHitHandler,
IHeroLocation, IHeroLocation,
IHeroMover, IHeroMover,
IHeroMoverConfig, IHeroMoverConfig,
IPassCheckerHandler, IHeroMoveTopHandler,
ITerrainPassChecker IHeroMoveTopImpl
} from './types'; } from './types';
import { isNil } from 'lodash-es'; import { isNil } from 'lodash-es';
@ -26,17 +24,17 @@ export class HeroMover<T extends IHeroLocation>
{ {
readonly state: IDataCommon; readonly state: IDataCommon;
/** 本次移动是否不记录进路线系统 */ /** 是否不记录进路线系统 */
private noRoute: boolean = false; private noRoute: boolean = false;
/** 本次移动是否忽略地形碰撞检测 */ /** 是否忽略地形碰撞检测 */
private ignoreTerrain: boolean = false; private ignoreTerrain: boolean = false;
/** 本次移动是否在特定时机触发自动存档 */ /** 是否在特定时机触发自动存档 */
private autoSave: boolean = false; private autoSave: boolean = false;
/** 是否允许到达地图外 */
private allowOutBound: boolean = false;
/** 地形通行判定器 */ /** 勇士移动顶层实现对象 */
private terrainChecker: ITerrainPassChecker | null = null; private topImpl: IHeroMoveTopImpl | null = null;
/** 撞击行为对象 */
private hitAction: IHeroHitAction | null = null;
constructor( constructor(
readonly tile: T, readonly tile: T,
@ -46,7 +44,7 @@ export class HeroMover<T extends IHeroLocation>
this.state = tile.state; this.state = tile.state;
} }
config(config: Readonly<IHeroMoverConfig>): this { config(config: Partial<IHeroMoverConfig>): this {
if (!isNil(config.noRoute)) { if (!isNil(config.noRoute)) {
this.noRoute = config.noRoute; this.noRoute = config.noRoute;
} }
@ -56,6 +54,9 @@ export class HeroMover<T extends IHeroLocation>
if (!isNil(config.autoSave)) { if (!isNil(config.autoSave)) {
this.autoSave = config.autoSave; this.autoSave = config.autoSave;
} }
if (!isNil(config.allowOutBound)) {
this.allowOutBound = config.allowOutBound;
}
return this; return this;
} }
@ -63,95 +64,104 @@ export class HeroMover<T extends IHeroLocation>
return { return {
noRoute: this.noRoute, noRoute: this.noRoute,
ignoreTerrain: this.ignoreTerrain, ignoreTerrain: this.ignoreTerrain,
autoSave: this.autoSave autoSave: this.autoSave,
allowOutBound: this.allowOutBound
}; };
} }
useTerrainChecker(checker: ITerrainPassChecker | null): void { useTopImplementation(impl: IHeroMoveTopImpl | null): void {
this.terrainChecker = checker; this.topImpl = impl;
}
useHitAction(action: IHeroHitAction | null): void {
this.hitAction = action;
} }
/** /**
* *
* @param curr * @param curr
* @param dir * @param dir
* @param floorId id * @param floorId id
*/ */
private createPassHandler( private createHandler(
curr: ITileLocator, curr: ITileLocator,
dir: FaceDirection, step: Readonly<ObjectMoveStep>,
floorId: string | undefined floorId: string | undefined
): IPassCheckerHandler { ): IHeroMoveTopHandler {
const { x, y } = this.faceHandler.movement(dir); switch (step.type) {
const nx = curr.x + x; case ObjectMoveType.Dir:
const ny = curr.y + y; case ObjectMoveType.DirFace:
return { case ObjectMoveType.Special: {
currLoc: curr, const { x, y } = this.faceHandler.movement(this.moveDirection);
nextLoc: { x: nx, y: ny }, const nx = curr.x + x;
direction: dir, const ny = curr.y + y;
floorId, return {
face: this.faceHandler, currLoc: { x: curr.x, y: curr.y },
state: this.state nextLoc: { x: nx, y: ny },
}; direction: this.moveDirection,
floorId,
face: this.faceHandler,
state: this.state
};
}
case ObjectMoveType.Jump:
case ObjectMoveType.Teleport: {
const nx = step.rel ? curr.x + step.x : step.x;
const ny = step.rel ? curr.y + step.y : step.y;
return {
currLoc: { x: curr.x, y: curr.y },
nextLoc: { x: nx, y: ny },
direction: this.moveDirection,
floorId,
face: this.faceHandler,
state: this.state
};
}
case ObjectMoveType.AnimDir:
case ObjectMoveType.Face:
case ObjectMoveType.Speed: {
return {
currLoc: { x: curr.x, y: curr.y },
nextLoc: { x: curr.x, y: curr.y },
direction: this.moveDirection,
floorId,
face: this.faceHandler,
state: this.state
};
}
}
} }
/** protected async onMoveStart(): Promise<void> {
* if (!this.topImpl) {
* @param curr logger.warn(144);
* @param dir }
* @param floorId id
*/
private createHitHandler(
curr: ITileLocator,
dir: FaceDirection,
floorId: string | undefined
): IHeroHitHandler {
const { x, y } = this.faceHandler.movement(dir);
const nx = curr.x + x;
const ny = curr.y + y;
return {
currLoc: curr,
nextLoc: { x: nx, y: ny },
direction: dir,
floorId,
state: this.state
};
} }
protected async onMoveStart(): Promise<void> {}
protected async onMoveEnd(): Promise<void> {} protected async onMoveEnd(): Promise<void> {}
protected async onStepStart( protected async onStepStart(
step: Readonly<ObjectMoveStep>, step: Readonly<ObjectMoveStep>,
tile: IHeroLocation tile: IHeroLocation
): Promise<number> { ): Promise<number> {
if (!this.terrainChecker) return HeroMoveCode.CannotMove; if (!this.topImpl) return HeroMoveCode.Stop;
const type = step.type; const type = step.type;
const handler = this.createHandler(tile, step, tile.floorId);
// 边界判断
const { x: nx, y: ny } = handler.nextLoc;
const inBound = this.topImpl.inBound(nx, ny, handler.floorId);
if (!this.allowOutBound && !inBound) return HeroMoveCode.CannotMove;
// 移动步判断
if ( if (
type === ObjectMoveType.Dir || type === ObjectMoveType.Dir ||
type === ObjectMoveType.DirFace || type === ObjectMoveType.DirFace ||
type === ObjectMoveType.Special type === ObjectMoveType.Special
) { ) {
const dir = this.moveDirection;
const handler = this.createPassHandler(tile, dir, tile.floorId);
const { x: nx, y: ny } = handler.nextLoc;
const inBound = this.terrainChecker.inBound(nx, ny, tile.floorId);
// 目标点不在地图内
if (!inBound) return HeroMoveCode.CannotMove;
if (this.ignoreTerrain) { if (this.ignoreTerrain) {
return HeroMoveCode.Step; return HeroMoveCode.Step;
} else { } else {
const canPass = this.terrainChecker.canPass(handler); const canPass = this.topImpl.canPass(handler);
if (canPass) { if (canPass) {
const hit = this.terrainChecker.shouldHit(handler); const hit = this.topImpl.shouldHit(handler);
if (hit) return HeroMoveCode.Hit; if (hit) return HeroMoveCode.Hit;
else return HeroMoveCode.Step; else return HeroMoveCode.Step;
} else { } else {
@ -160,14 +170,12 @@ export class HeroMover<T extends IHeroLocation>
} }
} }
// 跳跃和瞬移仅需要进行边界判断 // 跳跃和瞬移
if (type === ObjectMoveType.Jump || type === ObjectMoveType.Teleport) { if (type === ObjectMoveType.Jump || type === ObjectMoveType.Teleport) {
const nx = step.rel ? tile.x + step.x : step.x;
const ny = step.rel ? tile.y + step.y : step.y;
if (this.ignoreTerrain) { if (this.ignoreTerrain) {
return HeroMoveCode.Step; return HeroMoveCode.Step;
} else { } else {
if (this.terrainChecker.inBound(nx, ny, tile.floorId)) { if (inBound) {
return HeroMoveCode.Step; return HeroMoveCode.Step;
} else { } else {
return HeroMoveCode.Stop; return HeroMoveCode.Stop;
@ -175,6 +183,7 @@ export class HeroMover<T extends IHeroLocation>
} }
} }
// 其他的一律可以直接执行
return HeroMoveCode.Step; return HeroMoveCode.Step;
} }
@ -184,7 +193,9 @@ export class HeroMover<T extends IHeroLocation>
tile: IHeroLocation, tile: IHeroLocation,
controller: Readonly<IMoverController> controller: Readonly<IMoverController>
): Promise<ITileLocator> { ): Promise<ITileLocator> {
const type = step.type; if (!this.topImpl) return { x: tile.x, y: tile.y };
const handler = this.createHandler(tile, step, tile.floorId);
// 不能移动或停止 // 不能移动或停止
if (code === HeroMoveCode.CannotMove || code === HeroMoveCode.Stop) { if (code === HeroMoveCode.CannotMove || code === HeroMoveCode.Stop) {
@ -195,12 +206,7 @@ export class HeroMover<T extends IHeroLocation>
// 撞击时也使用当前位置,同时处理撞击行为 // 撞击时也使用当前位置,同时处理撞击行为
if (code === HeroMoveCode.Hit) { if (code === HeroMoveCode.Hit) {
// 执行撞击行为 await this.topImpl.hit(handler);
if (this.hitAction) {
const dir = this.moveDirection;
const handler = this.createHitHandler(tile, dir, tile.floorId);
await this.hitAction.hit(handler);
}
// 这里同样不能 await // 这里同样不能 await
controller.stop(); controller.stop();
return { x: tile.x, y: tile.y }; return { x: tile.x, y: tile.y };
@ -208,30 +214,7 @@ export class HeroMover<T extends IHeroLocation>
// 正常移动 // 正常移动
if (code === HeroMoveCode.Step) { if (code === HeroMoveCode.Step) {
if ( return handler.nextLoc;
type === ObjectMoveType.Teleport ||
type === ObjectMoveType.Jump
) {
// 瞬移行为
if (step.rel) {
return { x: tile.x + step.x, y: tile.y + step.y };
} else {
return { x: step.x, y: step.y };
}
} else if (
type === ObjectMoveType.Dir ||
type === ObjectMoveType.DirFace ||
type === ObjectMoveType.Special
) {
// 移动行为
const { x, y } = this.faceHandler.movement(this.moveDirection);
const nx = tile.x + x;
const ny = tile.y + y;
return { x: nx, y: ny };
} else {
// 其他行为
return { x: tile.x, y: tile.y };
}
} }
return { x: tile.x, y: tile.y }; return { x: tile.x, y: tile.y };

View File

@ -230,7 +230,7 @@ export const enum HeroMoveCode {
CannotMove CannotMove
} }
export interface IHeroMoveHandlerBase extends IDataCommonExtended { export interface IHeroMoveTopHandler extends IDataCommonExtended {
/** 当前位置 */ /** 当前位置 */
readonly currLoc: ITileLocator; readonly currLoc: ITileLocator;
/** 要移动至的位置 */ /** 要移动至的位置 */
@ -239,16 +239,11 @@ export interface IHeroMoveHandlerBase extends IDataCommonExtended {
readonly direction: FaceDirection; readonly direction: FaceDirection;
/** 当前楼层 id */ /** 当前楼层 id */
readonly floorId: string | undefined; readonly floorId: string | undefined;
}
export interface IPassCheckerHandler extends IHeroMoveHandlerBase {
/** 朝向管理对象 */ /** 朝向管理对象 */
readonly face: IFaceHandler<FaceDirection>; readonly face: IFaceHandler<FaceDirection>;
} }
export interface IHeroHitHandler extends IHeroMoveHandlerBase {} export interface IHeroMoveTopImpl {
export interface ITerrainPassChecker {
/** /**
* *
* @param x * @param x
@ -261,30 +256,30 @@ export interface ITerrainPassChecker {
* *
* @param handler * @param handler
*/ */
canPass(handler: IPassCheckerHandler): boolean; canPass(handler: IHeroMoveTopHandler): boolean;
/** /**
* *
* @param handler * @param handler
*/ */
shouldHit(handler: IPassCheckerHandler): boolean; shouldHit(handler: IHeroMoveTopHandler): boolean;
}
export interface IHeroHitAction {
/** /**
* *
* @param handler * @param handler
*/ */
hit(handler: IHeroHitHandler): Promise<void>; hit(handler: IHeroMoveTopHandler): Promise<void>;
} }
export interface IHeroMoverConfig { export interface IHeroMoverConfig {
/** 本次移动是否不记录进路线系统 */ /** 是否不记录进路线系统 */
noRoute?: boolean; noRoute: boolean;
/** 本次移动是否忽略地形碰撞检测 */ /** 是否忽略地形碰撞检测 */
ignoreTerrain?: boolean; ignoreTerrain: boolean;
/** 本次移动是否在特定时机触发自动存档 */ /** 是否在特定时机触发自动存档 */
autoSave?: boolean; autoSave: boolean;
/** 是否允许到达地图外 */
allowOutBound: boolean;
} }
export interface IHeroMover<T extends IHeroLocation> export interface IHeroMover<T extends IHeroLocation>
@ -293,7 +288,7 @@ export interface IHeroMover<T extends IHeroLocation>
* *
* @param config * @param config
*/ */
config(config: Readonly<IHeroMoverConfig>): this; config(config: Partial<IHeroMoverConfig>): this;
/** /**
* *
@ -301,16 +296,10 @@ export interface IHeroMover<T extends IHeroLocation>
getConfig(): Readonly<IHeroMoverConfig>; getConfig(): Readonly<IHeroMoverConfig>;
/** /**
* `null` *
* @param checker * @param impl
*/ */
useTerrainChecker(checker: ITerrainPassChecker | null): void; useTopImplementation(impl: IHeroMoveTopImpl | null): void;
/**
* `null`
* @param action
*/
useHitAction(action: IHeroHitAction | null): void;
} }
//#endregion //#endregion

View File

@ -66,8 +66,7 @@ import { ILoadProgressTotal, LoadProgressTotal } from '@motajs/loader';
import { isNil } from 'lodash-es'; import { isNil } from 'lodash-es';
import { logger } from '@motajs/common'; import { logger } from '@motajs/common';
import { ISaveSystem, SaveSystem } from './save'; import { ISaveSystem, SaveSystem } from './save';
import { DefaultPassChecker } from './hero'; import { DefaultHeroMoveTopImpl } from './hero';
import { DefaultHitAction } from './hero/hitAction';
export class CoreState implements ICoreState { export class CoreState implements ICoreState {
// Layer 0 公共层,最底层的接口,不会依赖任何其他内容,一般是工具性接口及不需要存档的数据 // Layer 0 公共层,最底层的接口,不会依赖任何其他内容,一般是工具性接口及不需要存档的数据
@ -211,14 +210,11 @@ export class CoreState implements ICoreState {
}); });
// 勇士顶层初始化 // 勇士顶层初始化
const passChecker = new DefaultPassChecker(this.maps); const heroMoveTopImpl = new DefaultHeroMoveTopImpl(
const hitAction = new DefaultHitAction( this,
this.maps, this.triggerCollector
this.triggerCollector,
this
); );
this.hero.location.mover.useTerrainChecker(passChecker); this.hero.location.mover.useTopImplementation(heroMoveTopImpl);
this.hero.location.mover.useHitAction(hitAction);
//#endregion //#endregion
} }

View File

@ -1,37 +0,0 @@
import {
IHeroHitAction,
IHeroHitHandler,
IMapStore,
IStateBase
} from '@user/data-base';
import { ITriggerCollector, ITriggerHandler } from '@user/data-system';
import { isNil } from 'lodash-es';
export class DefaultHitAction implements IHeroHitAction {
constructor(
readonly maps: IMapStore,
readonly collector: ITriggerCollector,
readonly state: IStateBase
) {}
async hit(handler: IHeroHitHandler): Promise<void> {
if (isNil(handler.floorId)) return;
const map = this.maps.getLayerState(handler.floorId);
if (!map) return;
const event = map.eventLayer;
if (!event) return;
const { x, y } = handler.nextLoc;
const triggers = this.collector.collect(x, y, event);
const triggerHandler: ITriggerHandler = {
state: this.state,
layer: map,
mapLayer: event,
locator: handler.nextLoc
};
return triggers.trigger(triggerHandler);
}
}

View File

@ -1,2 +1,2 @@
export * from './passChecker'; export * from './moverImpl';
export * from './types'; export * from './types';

View File

@ -1,13 +1,25 @@
import { FaceDirection, PassBit } from '@user/data-common';
import { import {
IHeroMoveTopHandler,
IHeroMoveTopImpl,
IMapStore, IMapStore,
IPassCheckerHandler, IStateBase
ITerrainPassChecker
} from '@user/data-base'; } from '@user/data-base';
import { FaceDirection, PassBit } from '@user/data-common';
import { ITriggerCollector, ITriggerHandler } from '@user/data-system';
import { isNil } from 'lodash-es'; import { isNil } from 'lodash-es';
export class DefaultPassChecker implements ITerrainPassChecker { export class DefaultHeroMoveTopImpl implements IHeroMoveTopImpl {
constructor(readonly maps: IMapStore) {} /** 地图存储对象 */
private readonly maps: IMapStore;
constructor(
private readonly state: IStateBase,
private readonly collector: ITriggerCollector
) {
this.maps = state.maps;
}
//#region 通行性判断
/** /**
* *
@ -36,7 +48,7 @@ export class DefaultPassChecker implements ITerrainPassChecker {
return x >= 0 && y >= 0 && x < width && y < height; return x >= 0 && y >= 0 && x < width && y < height;
} }
canPass(handler: IPassCheckerHandler): boolean { canPass(handler: IHeroMoveTopHandler): boolean {
const { currLoc, nextLoc, direction, floorId, face } = handler; const { currLoc, nextLoc, direction, floorId, face } = handler;
if (isNil(floorId)) return false; if (isNil(floorId)) return false;
@ -96,7 +108,7 @@ export class DefaultPassChecker implements ITerrainPassChecker {
return true; return true;
} }
shouldHit(handler: IPassCheckerHandler): boolean { shouldHit(handler: IHeroMoveTopHandler): boolean {
const { nextLoc, floorId } = handler; const { nextLoc, floorId } = handler;
if (isNil(floorId)) return false; if (isNil(floorId)) return false;
const layerState = this.maps.getLayerState(floorId); const layerState = this.maps.getLayerState(floorId);
@ -110,4 +122,31 @@ export class DefaultPassChecker implements ITerrainPassChecker {
if (!next || !next.raw) return false; if (!next || !next.raw) return false;
return !next.raw.eventPass; return !next.raw.eventPass;
} }
//#endregion
//#region 触发器行为
async hit(handler: IHeroMoveTopHandler): Promise<void> {
if (isNil(handler.floorId)) return;
const map = this.maps.getLayerState(handler.floorId);
if (!map) return;
const event = map.eventLayer;
if (!event) return;
const { x, y } = handler.nextLoc;
const triggers = this.collector.collect(x, y, event);
const triggerHandler: ITriggerHandler = {
state: this.state,
layer: map,
mapLayer: event,
locator: handler.nextLoc
};
return triggers.trigger(triggerHandler);
}
//#endregion
} }

View File

@ -200,6 +200,7 @@
"140": "Different priority of combat script is expected, but got a same one.", "140": "Different priority of combat script is expected, but got a same one.",
"141": "Some issue may occur in binded damage system on combat flow, please check the console.", "141": "Some issue may occur in binded damage system on combat flow, please check the console.",
"142": "Expected a specific tile number after convert id '$1' to number, but got null.", "142": "Expected a specific tile number after convert id '$1' to number, but got null.",
"143": "Cannot create dynamic tile for $1 since its raw data is not found." "143": "Cannot create dynamic tile for $1 since its raw data is not found.",
"144": "A hero move top implementation object binding is required for hero moving."
} }
} }