mirror of
https://github.com/motajs/template.git
synced 2026-11-29 09:54:30 +08:00
refactor: 按审查意见调整寻路模块与开发规范
This commit is contained in:
1 parent
489e3de106
commit
622008f67c
6 files changed
+25
-26
No files matched your search
@@ -55,14 +55,13 @@ export class PathfindingFinder implements IPathfinder {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取进入指定位置节点的损失,损失值非有限数或负数时
|
||||
* 告警并按默认损失 1 处理
|
||||
* 获取进入指定位置节点的损失,损失值为负数时抛出警告并按损失 1 处理
|
||||
* @param block 位置信息
|
||||
*/
|
||||
private getNodeCost(block: ILayerLocation): number {
|
||||
if (!this.cost) return 1;
|
||||
const value = this.cost(block);
|
||||
if (!Number.isFinite(value) || value < 0) {
|
||||
if (value < 0) {
|
||||
logger.warn(174);
|
||||
return 1;
|
||||
}
|
||||
@@ -70,8 +69,7 @@ export class PathfindingFinder implements IPathfinder {
|
||||
}
|
||||
|
||||
/**
|
||||
* 在有向图上执行最小损失搜索,
|
||||
* 终端节点仅可作为路径终点,不可作为中间节点
|
||||
* 在有向图上执行最小损失搜索,终端节点仅可作为路径终点,不可作为中间节点
|
||||
* @param graph 寻路有向图
|
||||
* @param start 寻路起始位置
|
||||
* @param target 寻路目标位置
|
||||
|
||||
@@ -91,7 +91,7 @@ export class PathfindingGraphBuilder implements IPathfindingGraphBuilder {
|
||||
const width = layer.width;
|
||||
const height = layer.height;
|
||||
const floorId = this.resolveFloorId();
|
||||
const state: IDataCommon = layer.state;
|
||||
const state = layer.state;
|
||||
const blocks: (ILayerLocation | null)[] = new Array(
|
||||
width * height
|
||||
).fill(null);
|
||||
|
||||
@@ -34,10 +34,6 @@ export class PathfindingSystem implements IPathfindingSystem {
|
||||
this.policy = policy;
|
||||
}
|
||||
|
||||
/**
|
||||
* 未绑定移动器时告警并返回空数组
|
||||
* @param target 目标坐标
|
||||
*/
|
||||
getPath(target: ITileLocator): IPathfindingStep[] {
|
||||
const mover = this.mover;
|
||||
if (isNil(mover)) {
|
||||
@@ -49,8 +45,7 @@ export class PathfindingSystem implements IPathfindingSystem {
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定移动方式启动寻路移动,返回控制器包装。
|
||||
* 已有移动进行中时返回 `null`
|
||||
* 按指定移动方式启动寻路移动,返回控制器包装。已有移动进行中时返回 `null`
|
||||
* @param path 寻路步骤序列
|
||||
* @param teleport 是否瞬移
|
||||
*/
|
||||
@@ -64,7 +59,7 @@ export class PathfindingSystem implements IPathfindingSystem {
|
||||
if (current && !current.controller.done) return null;
|
||||
|
||||
if (teleport) {
|
||||
const last = path[path.length - 1];
|
||||
const last = path.at(-1)!;
|
||||
mover.tp(last.to.x, last.to.y);
|
||||
} else {
|
||||
for (const step of path) {
|
||||
@@ -72,7 +67,6 @@ export class PathfindingSystem implements IPathfindingSystem {
|
||||
}
|
||||
}
|
||||
|
||||
// 移动器移动中时启动失败,对应已有移动进行中的契约
|
||||
const controller = mover.start();
|
||||
if (!controller) return null;
|
||||
const result: IPathfindingController = { controller, path };
|
||||
@@ -89,10 +83,11 @@ export class PathfindingSystem implements IPathfindingSystem {
|
||||
teleportTo(target: ITileLocator): IPathfindingController | null {
|
||||
const path = this.getPath(target);
|
||||
if (path.length === 0) return null;
|
||||
if (isNil(this.policy) || this.policy(path)) {
|
||||
if (!this.policy || this.policy(path)) {
|
||||
return this.startMove(path, false);
|
||||
} else {
|
||||
return this.startMove(path, true);
|
||||
}
|
||||
return this.startMove(path, true);
|
||||
}
|
||||
|
||||
async interrupt(): Promise<void> {
|
||||
|
||||
@@ -13,6 +13,8 @@ import {
|
||||
IObjectMover
|
||||
} from '@user/data-common';
|
||||
|
||||
//#region 寻路系统
|
||||
|
||||
export interface IPathfindingStep {
|
||||
/** 移动方向 */
|
||||
readonly dir: FaceDirection;
|
||||
@@ -105,14 +107,12 @@ export interface IPathfindingSystem extends IDataBaseExtended {
|
||||
/**
|
||||
* 逐步寻路至目标位置,触发途经事件
|
||||
* @param target 目标坐标
|
||||
* @returns 移动控制器。无法寻路、无路径或已有移动进行中时返回 `null`
|
||||
*/
|
||||
moveTo(target: ITileLocator): IPathfindingController | null;
|
||||
|
||||
/**
|
||||
* 瞬移至目标位置。瞬移前经回退策略判定,判定需要回退则自动退为逐步寻路
|
||||
* @param target 目标坐标
|
||||
* @returns 移动控制器;无法寻路、无路径或已有移动进行中时返回 `null`
|
||||
*/
|
||||
teleportTo(target: ITileLocator): IPathfindingController | null;
|
||||
|
||||
@@ -122,6 +122,10 @@ export interface IPathfindingSystem extends IDataBaseExtended {
|
||||
interrupt(): Promise<void>;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 路径图
|
||||
|
||||
export interface IPathGraphEdge {
|
||||
/** 本条边对应的移动方向 */
|
||||
readonly dir: FaceDirection;
|
||||
@@ -156,19 +160,19 @@ export interface IPathGraph {
|
||||
export interface IPathfindingGraphBuilder {
|
||||
/**
|
||||
* 绑定地图状态对象,用于解析图层所属楼层 id
|
||||
* @param maps 地图状态对象,传入 `null` 解绑
|
||||
* @param maps 地图状态对象
|
||||
*/
|
||||
useMapState(maps: IMapState | null): void;
|
||||
|
||||
/**
|
||||
* 绑定构建有向图所用的地图图层
|
||||
* @param layer 地图图层对象,传入 `null` 解绑
|
||||
* @param layer 地图图层对象
|
||||
*/
|
||||
useMapLayer(layer: IMapLayer | null): void;
|
||||
|
||||
/**
|
||||
* 注入判定边可行性的通行性谓词
|
||||
* @param predicate 通行性谓词,传入 `null` 解绑
|
||||
* @param predicate 通行性谓词
|
||||
*/
|
||||
usePassPredicate(predicate: IPassPredicate | null): void;
|
||||
|
||||
@@ -179,9 +183,9 @@ export interface IPathfindingGraphBuilder {
|
||||
useDirGroup(group: number): void;
|
||||
|
||||
/**
|
||||
* 构建有向图。图层未绑定时告警并返回空图,
|
||||
* 不包含任何节点与边
|
||||
* @returns 构建的有向图
|
||||
* 构建有向图。图层未绑定时告警并返回空图,不包含任何节点与边
|
||||
*/
|
||||
build(): IPathGraph;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
Reference in new issue
Block a user