feat(pathfinding): 支持从不可行走位置开始寻路

改进寻路系统,允许玩家从不可行走的当前位置开始寻路到可行走区域。
当玩家位于不可行走位置时,系统会自动寻找最近的可行走位置作为起点,
并临时将起点设置为可行走状态以启动A*算法。

主要变更:
- AStarPathfinding: 临时修改起点可行走状态以支持算法启动
- PlayerController: 检测玩家当前位置并自动传送到最近可行走点
- TiledMapPathfinder: 在寻路前验证起点并寻找替代位置
This commit is contained in:
richarjiang
2025-10-20 09:23:04 +08:00
parent 8f4200a7a3
commit 972334f786
3 changed files with 46 additions and 4 deletions

View File

@@ -238,8 +238,23 @@ export class PlayerController extends Component {
clampedPos.set(closestWalkable);
}
// 检查玩家当前位置是否可行走
let startPos = this.player.position;
if (!this.pathfinder.isWorldPositionWalkable(startPos)) {
console.log('玩家当前位置不可行走,寻找最近的可行走位置作为起点');
const closestPlayerWalkable = this.pathfinder.getClosestWalkablePosition(startPos);
if (!closestPlayerWalkable) {
console.warn('找不到玩家附近的可行走位置');
return;
}
startPos = closestPlayerWalkable;
console.log(`将玩家移动到最近的可行走位置: (${startPos.x.toFixed(2)}, ${startPos.y.toFixed(2)})`);
// 直接将玩家传送到最近的可行走位置
this.player.setPosition(startPos);
}
// 使用寻路算法计算路径
const startPos = this.player.position;
this.currentPath = this.pathfinder.findPath(startPos, clampedPos);
if (this.currentPath.length === 0) {