mirror of
https://github.com/motajs/template.git
synced 2026-09-15 03:18:50 +08:00
feat: 完成寻路系统接口设计
This commit is contained in:
parent
67c0806f7f
commit
7a011b29c2
@ -13,6 +13,7 @@ import {
|
|||||||
IObjectMover,
|
IObjectMover,
|
||||||
ISaveableContent
|
ISaveableContent
|
||||||
} from '@user/data-common';
|
} from '@user/data-common';
|
||||||
|
import { IPassPredicate } from '../map';
|
||||||
|
|
||||||
//#region 勇士属性
|
//#region 勇士属性
|
||||||
|
|
||||||
@ -300,6 +301,11 @@ export interface IHeroMoveTopHandler extends IDataCommonExtended {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface IHeroMoveTopImpl {
|
export interface IHeroMoveTopImpl {
|
||||||
|
/**
|
||||||
|
* 获取用于检查通行性的谓词对象
|
||||||
|
*/
|
||||||
|
predicate(): IPassPredicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查目标位置是否在地图范围内
|
* 检查目标位置是否在地图范围内
|
||||||
* @param x 横坐标
|
* @param x 横坐标
|
||||||
@ -308,18 +314,6 @@ export interface IHeroMoveTopImpl {
|
|||||||
*/
|
*/
|
||||||
inBound(x: number, y: number, floorId: string | undefined): boolean;
|
inBound(x: number, y: number, floorId: string | undefined): boolean;
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断在指定楼层中,从指定坐标向指定方向移动一格是否可通行
|
|
||||||
* @param handler 通行性检查对象
|
|
||||||
*/
|
|
||||||
canPass(handler: IHeroMoveTopHandler): boolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断在指定楼层中,从指定坐标向指定方向移动时是否应该产生撞击,撞击将会触发目标位置的撞击触发器
|
|
||||||
* @param handler 通行性检查对象
|
|
||||||
*/
|
|
||||||
shouldHit(handler: IHeroMoveTopHandler): boolean;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 勇士正常移动到某个图块上时触发进入触发器
|
* 勇士正常移动到某个图块上时触发进入触发器
|
||||||
* @param handler 移动信息对象
|
* @param handler 移动信息对象
|
||||||
|
|||||||
@ -889,3 +889,32 @@ export interface IMapState
|
|||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#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
|
||||||
|
|||||||
122
packages-user/data-system/src/path/types.ts
Normal file
122
packages-user/data-system/src/path/types.ts
Normal file
@ -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<ITileLocator>;
|
||||||
|
/** 这一步移动到的位置 */
|
||||||
|
readonly to: Readonly<ITileLocator>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IPathfindingController {
|
||||||
|
/** 移动控制器对象 */
|
||||||
|
readonly controller: Readonly<IMoverController>;
|
||||||
|
/** 寻路路径 */
|
||||||
|
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<void>;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user