refactor(02-04): restore authorized path contract

- Match path/types.ts to the user-authored baseline plus nullable returns
- Keep graph contracts implementation-owned and preserve the mover bridge
This commit is contained in:
unanmed 2026-09-10 09:35:21 +08:00
parent 3809b40b41
commit a2aedb3969
4 changed files with 91 additions and 99 deletions

View File

@ -7,12 +7,8 @@ import {
} from '@user/data-base';
import { isNil } from 'lodash-es';
import { PathfindingGraphBuilder } from './graph';
import {
IPathfinder,
IPathfindingStep,
IPathGraph,
PathCostFunction
} from './types';
import { IPathfinder, IPathfindingStep, PathCostFunction } from './types';
import { IPathGraph } from './graph';
interface IDistanceHeapEntry {
/** 条目的键值,堆中键值最小的条目最先取出 */

View File

@ -15,13 +15,79 @@ import {
IPassPredicate
} from '@user/data-base';
import { isNil } from 'lodash-es';
import {
IPathGraph,
IPathGraphEdge,
IPathGraphNode,
IPathfindingGraphBuilder,
PathCostFunction
} from './types';
import { PathCostFunction } from './types';
export interface IPathGraphEdge {
/** 本条边对应的移动方向 */
readonly dir: FaceDirection;
/** 边指向的节点索引,值为 y * width + x */
readonly to: number;
}
export interface IPathGraphNode {
/** 节点索引,值为 y * width + x */
readonly index: number;
/** 节点横坐标 */
readonly x: number;
/** 节点纵坐标 */
readonly y: number;
/** 节点对应的位置信息 */
readonly block: ILayerLocation;
/** 进入该节点的损失,构建图时由损失函数计算 */
readonly cost: number;
/** 该节点是否仅可作为路径终点,不可作为中间节点 */
readonly terminal: boolean;
/** 该节点的全部出边 */
readonly edges: readonly IPathGraphEdge[];
}
export interface IPathGraph {
/** 图宽度 */
readonly width: number;
/** 图高度 */
readonly height: number;
/** 图内全部节点,键为节点索引 */
readonly nodes: ReadonlyMap<number, IPathGraphNode>;
}
export interface IPathfindingGraphBuilder {
/**
* id
* @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;
/**
* 沿 BFS
*
* @param start BFS
*/
build(start: ITileLocator): IPathGraph;
}
/**
*

View File

@ -17,6 +17,8 @@ export class PathfindingSystem implements IPathfindingSystem {
/** 绑定的移动器对象 */
private mover: IObjectMover<IObjectMovable> | null = null;
/** 绑定的移动对象,用于保留用户契约的对象绑定入口 */
private movable: IObjectMovable | null = null;
/** 注入的瞬移回退策略,未注入时必定回退为逐步寻路 */
private policy: PathFallbackPolicy | null = null;
/** 最近一次寻路移动的控制器包装 */
@ -26,8 +28,13 @@ export class PathfindingSystem implements IPathfindingSystem {
this.finder = new PathfindingFinder(state);
}
useMovable(movable: IObjectMovable | null): void {
this.movable = movable;
}
useMover(mover: IObjectMover<IObjectMovable> | null): void {
this.mover = mover;
this.movable = mover ? mover.tile : null;
}
useFallbackPolicy(policy: PathFallbackPolicy | null): void {
@ -36,11 +43,13 @@ export class PathfindingSystem implements IPathfindingSystem {
getPath(target: ITileLocator): IPathfindingStep[] {
const mover = this.mover;
if (isNil(mover)) {
const movable = this.movable;
if (isNil(mover) && isNil(movable)) {
logger.warn(173);
return [];
}
const tile = mover.tile;
const tile = mover ? mover.tile : movable;
if (!tile) return [];
return this.finder.find({ x: tile.x, y: tile.y }, target);
}

View File

@ -9,12 +9,9 @@ import {
import {
FaceDirection,
IMoverController,
IObjectMovable,
IObjectMover
IObjectMovable
} from '@user/data-common';
//#region 寻路系统
export interface IPathfindingStep {
/** 移动方向 */
readonly dir: FaceDirection;
@ -88,9 +85,9 @@ export interface IPathfindingSystem extends IDataBaseExtended {
/**
* `IObjectMovable`
* @param mover
* @param movable
*/
useMover(mover: IObjectMover<IObjectMovable> | null): void;
useMovable(movable: IObjectMovable | null): void;
/**
* 退退
@ -107,12 +104,14 @@ export interface IPathfindingSystem extends IDataBaseExtended {
/**
*
* @param target
* @returns `null`
*/
moveTo(target: ITileLocator): IPathfindingController | null;
/**
* 退退退
* @param target
* @returns `null`
*/
teleportTo(target: ITileLocator): IPathfindingController | null;
@ -121,81 +120,3 @@ export interface IPathfindingSystem extends IDataBaseExtended {
*/
interrupt(): Promise<void>;
}
//#endregion
//#region 路径图
export interface IPathGraphEdge {
/** 本条边对应的移动方向 */
readonly dir: FaceDirection;
/** 边指向的节点索引,值为 y * width + x */
readonly to: number;
}
export interface IPathGraphNode {
/** 节点索引,值为 y * width + x */
readonly index: number;
/** 节点横坐标 */
readonly x: number;
/** 节点纵坐标 */
readonly y: number;
/** 节点对应的位置信息 */
readonly block: ILayerLocation;
/** 进入该节点的损失,构建图时由损失函数计算 */
readonly cost: number;
/** 该节点是否仅可作为路径终点,不可作为中间节点 */
readonly terminal: boolean;
/** 该节点的全部出边 */
readonly edges: readonly IPathGraphEdge[];
}
export interface IPathGraph {
/** 图宽度 */
readonly width: number;
/** 图高度 */
readonly height: number;
/** 图内全部节点,键为节点索引 */
readonly nodes: ReadonlyMap<number, IPathGraphNode>;
}
export interface IPathfindingGraphBuilder {
/**
* id
* @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;
/**
* 沿 BFS
*
* @param start BFS
*/
build(start: ITileLocator): IPathGraph;
}
//#endregion