From 7a011b29c2e197b4feca910dfa9962fc32c3a4c6 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Wed, 9 Sep 2026 16:40:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E5=AF=BB=E8=B7=AF?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=8E=A5=E5=8F=A3=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages-user/data-base/src/hero/types.ts | 18 +-- packages-user/data-base/src/map/types.ts | 29 +++++ packages-user/data-system/src/path/types.ts | 122 ++++++++++++++++++++ 3 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 packages-user/data-system/src/path/types.ts diff --git a/packages-user/data-base/src/hero/types.ts b/packages-user/data-base/src/hero/types.ts index 2fb0c3f..4b62857 100644 --- a/packages-user/data-base/src/hero/types.ts +++ b/packages-user/data-base/src/hero/types.ts @@ -13,6 +13,7 @@ import { IObjectMover, ISaveableContent } from '@user/data-common'; +import { IPassPredicate } from '../map'; //#region 勇士属性 @@ -300,6 +301,11 @@ export interface IHeroMoveTopHandler extends IDataCommonExtended { } export interface IHeroMoveTopImpl { + /** + * 获取用于检查通行性的谓词对象 + */ + predicate(): IPassPredicate; + /** * 检查目标位置是否在地图范围内 * @param x 横坐标 @@ -308,18 +314,6 @@ export interface IHeroMoveTopImpl { */ inBound(x: number, y: number, floorId: string | undefined): boolean; - /** - * 判断在指定楼层中,从指定坐标向指定方向移动一格是否可通行 - * @param handler 通行性检查对象 - */ - canPass(handler: IHeroMoveTopHandler): boolean; - - /** - * 判断在指定楼层中,从指定坐标向指定方向移动时是否应该产生撞击,撞击将会触发目标位置的撞击触发器 - * @param handler 通行性检查对象 - */ - shouldHit(handler: IHeroMoveTopHandler): boolean; - /** * 勇士正常移动到某个图块上时触发进入触发器 * @param handler 移动信息对象 diff --git a/packages-user/data-base/src/map/types.ts b/packages-user/data-base/src/map/types.ts index f659dd9..cb2efcb 100644 --- a/packages-user/data-base/src/map/types.ts +++ b/packages-user/data-base/src/map/types.ts @@ -889,3 +889,32 @@ export interface IMapState } //#endregion + +//#region 通行检测 + +export interface IPassCheckHandler extends IDataCommonExtended { + /** 当前位置 */ + readonly currLoc: ITileLocator; + /** 要移动至的位置 */ + readonly nextLoc: ITileLocator; + /** 移动方向 */ + readonly direction: FaceDirection; + /** 当前楼层 id */ + readonly floorId: string | undefined; +} + +export interface IPassPredicate { + /** + * 检查在指定楼层中,能否从某一个移动入某一格 + * @param handler 通行性检查信息对象 + */ + canPass(handler: IPassCheckHandler): boolean; + + /** + * 检查在指定楼层中,从某一个移动至某一格时是否会产生撞击行为 + * @param handler 通行性检查信息对象 + */ + shouldHit(handler: IPassCheckHandler): boolean; +} + +//#endregion diff --git a/packages-user/data-system/src/path/types.ts b/packages-user/data-system/src/path/types.ts new file mode 100644 index 0000000..dbcdbe6 --- /dev/null +++ b/packages-user/data-system/src/path/types.ts @@ -0,0 +1,122 @@ +import { ITileLocator } from '@motajs/common'; +import { + IDataBaseExtended, + ILayerLocation, + IMapLayer, + IMapState, + IPassPredicate +} from '@user/data-base'; +import { + FaceDirection, + IMoverController, + IObjectMovable +} from '@user/data-common'; + +export interface IPathfindingStep { + /** 移动方向 */ + readonly dir: FaceDirection; + /** 这一步的出发位置 */ + readonly from: Readonly; + /** 这一步移动到的位置 */ + readonly to: Readonly; +} + +export interface IPathfindingController { + /** 移动控制器对象 */ + readonly controller: Readonly; + /** 寻路路径 */ + readonly path: readonly IPathfindingStep[]; +} + +/** + * 寻路损失函数,损失值仅与指定位置的图块有关,与移动方式等无关 + * @param block 指定坐标的位置信息 + */ +export type PathCostFunction = (block: ILayerLocation) => number; + +/** + * 瞬移回退策略函数,若返回 `true`,表示当前操作不允许瞬移,必须逐步寻路 + * @param path 完整路径列表 + */ +export type PathFallbackPolicy = (path: readonly IPathfindingStep[]) => boolean; + +export interface IPathfinder extends IDataBaseExtended { + /** + * 绑定寻路所用的地图状态对象,用于获取楼层与事件层信息,未绑定时寻路告警并返回空路径 + * @param maps 地图状态对象 + */ + useMapState(maps: IMapState | null): void; + + /** + * 绑定构建有向图所用的地图图层,通常绑定事件层 + * @param layer 地图图层对象 + */ + useMapLayer(layer: IMapLayer | null): void; + + /** + * 设置自定义损失函数,默认每格损失 1 + * @param cost 损失函数 + */ + useCostFunction(cost: PathCostFunction | null): void; + + /** + * 设置通行性谓词,用于通行性判断,默认均不可通行 + * @param predicate 通行性谓词 + */ + usePassPredicate(predicate: IPassPredicate | null): void; + + /** + * 设置寻路使用的朝向组 + * @param group 朝向组 + */ + useDirGroup(group: number): void; + + /** + * 在当前状态下执行寻路操作 + * @param start 寻路起始位置 + * @param target 寻路目标位置 + */ + find(start: ITileLocator, target: ITileLocator): IPathfindingStep[]; +} + +export interface IPathfindingSystem extends IDataBaseExtended { + /** 寻路求解器 */ + readonly finder: IPathfinder; + + /** + * 绑定寻路移动对象,可绑定勇士位置或任意 `IObjectMovable`,如动态图块 + * @param movable 移动对象 + */ + useMovable(movable: IObjectMovable | null): void; + + /** + * 设置瞬移回退策略,默认必定回退为逐步寻路 + * @param policy 回退策略函数 + */ + useFallbackPolicy(policy: PathFallbackPolicy | null): void; + + /** + * 仅获取从当前位置至目标位置的最小损失路径,不产生任何移动 + * @param target 目标坐标 + */ + getPath(target: ITileLocator): IPathfindingStep[]; + + /** + * 逐步寻路至目标位置,触发途经事件 + * @param target 目标坐标 + * @returns 移动控制器。无法寻路、无路径或已有移动进行中时返回 `null` + */ + moveTo(target: ITileLocator): IPathfindingController; + + /** + * 瞬移至目标位置。瞬移前经回退策略判定,判定需要回退则自动退为逐步寻路 + * @param target 目标坐标 + * @returns 移动控制器;无法寻路、无路径或已有移动进行中时返回 `null` + */ + teleportTo(target: ITileLocator): IPathfindingController; + + /** + * 打断当前自动寻路。新的方向输入或新的寻路调用可随时打断并接管 + */ + interrupt(): Promise; +}