mirror of
https://github.com/motajs/template.git
synced 2026-11-12 22:14:30 +08:00
refactor(02-06): align pathfinding structure with user scope
This commit is contained in:
1 parent
97a559cdf8
commit
26f7ad6b2e
23 files changed
+556
-992
No files matched your search
@@ -7,8 +7,12 @@ import {
|
||||
} from '@user/data-base';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { PathfindingGraphBuilder } from './graph';
|
||||
import { IPathfinder, IPathfindingStep, PathCostFunction } from './types';
|
||||
import { IPathGraph } from './graph';
|
||||
import {
|
||||
IPathfinder,
|
||||
IPathfindingStep,
|
||||
IPathGraph,
|
||||
PathCostFunction
|
||||
} from './types';
|
||||
|
||||
interface IDistanceHeapEntry {
|
||||
/** 条目的键值,堆中键值最小的条目最先取出 */
|
||||
|
||||
@@ -260,6 +260,7 @@ function createFixture(
|
||||
eventStore: {},
|
||||
roleFace: new modules.RoleFaceBinder(),
|
||||
faceManager,
|
||||
directionMapper: new modules.DirectionMapper(),
|
||||
saveSystem: {}
|
||||
} as never;
|
||||
const maps = new modules.MapState(tileStore, commonState);
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import {
|
||||
DirectionMapper,
|
||||
IDirectionDescriptor,
|
||||
IDirectionMapper,
|
||||
InternalDirectionGroup,
|
||||
ITileLocator,
|
||||
logger
|
||||
@@ -15,79 +13,13 @@ import {
|
||||
IPassPredicate
|
||||
} from '@user/data-base';
|
||||
import { isNil } from 'lodash-es';
|
||||
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;
|
||||
}
|
||||
import {
|
||||
IPathGraph,
|
||||
IPathGraphEdge,
|
||||
IPathGraphNode,
|
||||
IPathfindingGraphBuilder,
|
||||
PathCostFunction
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* 将方向描述器的坐标增量解析为对应的朝向
|
||||
@@ -118,9 +50,6 @@ export class PathfindingGraphBuilder implements IPathfindingGraphBuilder {
|
||||
/** 邻域方向组别,默认四正交方向 */
|
||||
private group: number = InternalDirectionGroup.Dir4;
|
||||
|
||||
/** 方向组解析对象 */
|
||||
private readonly mapper: IDirectionMapper = new DirectionMapper();
|
||||
|
||||
useMapState(maps: IMapState | null): void {
|
||||
this.maps = maps;
|
||||
}
|
||||
@@ -181,7 +110,9 @@ export class PathfindingGraphBuilder implements IPathfindingGraphBuilder {
|
||||
const height = layer.height;
|
||||
const floorId = this.resolveFloorId();
|
||||
const state = layer.state;
|
||||
const dirs: IDirectionDescriptor[] = [...this.mapper.map(this.group)];
|
||||
const dirs: IDirectionDescriptor[] = [
|
||||
...layer.state.directionMapper.map(this.group)
|
||||
];
|
||||
|
||||
const terminals: Set<number> = new Set();
|
||||
const adjacency: Map<number, IPathGraphEdge[]> = new Map();
|
||||
|
||||
@@ -40,6 +40,7 @@ interface TestModules {
|
||||
ObjectMover: typeof import('@user/data-common').ObjectMover;
|
||||
FaceManager: typeof import('@user/data-common').FaceManager;
|
||||
Dir8FaceHandler: typeof import('@user/data-common').Dir8FaceHandler;
|
||||
DirectionMapper: typeof import('@motajs/common').DirectionMapper;
|
||||
RoleFaceBinder: typeof import('@user/data-common').RoleFaceBinder;
|
||||
}
|
||||
|
||||
@@ -51,6 +52,7 @@ beforeAll(async () => {
|
||||
const systemModule = await import('./system');
|
||||
const baseModule = await import('@user/data-base');
|
||||
const commonModule = await import('@user/data-common');
|
||||
const motaModule = await import('@motajs/common');
|
||||
modules = {
|
||||
PathfindingSystem: systemModule.PathfindingSystem,
|
||||
MapState: baseModule.MapState,
|
||||
@@ -58,7 +60,8 @@ beforeAll(async () => {
|
||||
ObjectMover: commonModule.ObjectMover,
|
||||
FaceManager: commonModule.FaceManager,
|
||||
Dir8FaceHandler: commonModule.Dir8FaceHandler,
|
||||
RoleFaceBinder: commonModule.RoleFaceBinder
|
||||
RoleFaceBinder: commonModule.RoleFaceBinder,
|
||||
DirectionMapper: motaModule.DirectionMapper
|
||||
};
|
||||
});
|
||||
|
||||
@@ -289,6 +292,7 @@ function createPerformanceSystem(
|
||||
eventStore: {},
|
||||
roleFace: new modules.RoleFaceBinder(),
|
||||
faceManager,
|
||||
directionMapper: new modules.DirectionMapper(),
|
||||
saveSystem: {}
|
||||
} as never;
|
||||
const maps = new modules.MapState(tileStore, commonState);
|
||||
|
||||
@@ -43,6 +43,7 @@ interface TestModules {
|
||||
ObjectMover: typeof import('@user/data-common').ObjectMover;
|
||||
FaceManager: typeof import('@user/data-common').FaceManager;
|
||||
Dir8FaceHandler: typeof import('@user/data-common').Dir8FaceHandler;
|
||||
DirectionMapper: typeof import('@motajs/common').DirectionMapper;
|
||||
RoleFaceBinder: typeof import('@user/data-common').RoleFaceBinder;
|
||||
logger: typeof import('@motajs/common').logger;
|
||||
}
|
||||
@@ -63,6 +64,7 @@ beforeAll(async () => {
|
||||
ObjectMover: commonModule.ObjectMover,
|
||||
FaceManager: commonModule.FaceManager,
|
||||
Dir8FaceHandler: commonModule.Dir8FaceHandler,
|
||||
DirectionMapper: motaModule.DirectionMapper,
|
||||
RoleFaceBinder: commonModule.RoleFaceBinder,
|
||||
logger: motaModule.logger
|
||||
};
|
||||
@@ -319,6 +321,7 @@ function createSystem(rows: number[], width: number): SystemFixture {
|
||||
eventStore: {},
|
||||
roleFace: new modules.RoleFaceBinder(),
|
||||
faceManager,
|
||||
directionMapper: new modules.DirectionMapper(),
|
||||
saveSystem: {}
|
||||
} as never;
|
||||
const maps = new modules.MapState(tileStore, commonState);
|
||||
|
||||
@@ -12,6 +12,78 @@ import {
|
||||
IObjectMovable
|
||||
} from '@user/data-common';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export interface IPathfindingStep {
|
||||
/** 移动方向 */
|
||||
readonly dir: FaceDirection;
|
||||
|
||||
Reference in new issue
Block a user