@cc-component/cc-pathfinding 1.0.9 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -57,8 +57,12 @@ declare global {
|
|
|
57
57
|
//#region 移动控制
|
|
58
58
|
/** 内部创建并绑定移动控制器 */
|
|
59
59
|
export function InitMovementController(scheduler: Component, options?: { retryDelay?: number; moveDuration?: number }): void
|
|
60
|
-
/**
|
|
61
|
-
|
|
60
|
+
/**
|
|
61
|
+
* 设置单位位置
|
|
62
|
+
* @param useBottomPosition false(默认)用格子中心 GetMapPosition,
|
|
63
|
+
* true 用格子底部 GetMapModel(...).bottom_positon
|
|
64
|
+
*/
|
|
65
|
+
export function SetPosition(playerNode: Node, position: { row: number, col: number }, useBottomPosition?: boolean): void
|
|
62
66
|
/**
|
|
63
67
|
* 让单位移动到目标格子
|
|
64
68
|
* @param allowOverlap 为 true 时该单位进入"允许重叠"模式,
|
|
@@ -66,8 +70,10 @@ declare global {
|
|
|
66
70
|
* 与终点是否相同无关
|
|
67
71
|
* @param moveDuration 本次移动的"单格移动时长",单位秒(越小越快);
|
|
68
72
|
* 不传或传 <= 0 时使用 InitMovementController 配置的默认值
|
|
73
|
+
* @param useBottomPosition 节点对齐方式:false(默认)用格子中心 GetMapPosition,
|
|
74
|
+
* true 用格子底部 GetMapModel(...).bottom_positon
|
|
69
75
|
*/
|
|
70
|
-
export function MoveTo(playerNode: Node, target: { row: number, col: number }, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap?: boolean, moveDuration?: number): void
|
|
76
|
+
export function MoveTo(playerNode: Node, target: { row: number, col: number }, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap?: boolean, moveDuration?: number, useBottomPosition?: boolean): void
|
|
71
77
|
/** 取消移动 */
|
|
72
78
|
export function CancelMove(playerNode: Node, preserveCurrent?: boolean): void
|
|
73
79
|
/** 销毁移动控制器 */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Component, Node, Tween, tween } from 'cc';
|
|
1
|
+
import { Component, Node, Tween, tween, Vec3 } from 'cc';
|
|
2
2
|
import { AttackDistance } from './Common';
|
|
3
3
|
import { Cell } from './GridMap';
|
|
4
4
|
import { PathGridStateManager } from './PathGridStateManager';
|
|
@@ -38,10 +38,13 @@ export class GridMovementController {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
/**
|
|
42
|
-
|
|
41
|
+
/**
|
|
42
|
+
* 设定单位当前格子,并同步到节点世界坐标。
|
|
43
|
+
* useBottomPosition:false(默认)对齐格子中心,true 对齐格子底部。
|
|
44
|
+
*/
|
|
45
|
+
setPosition(playerNode: Node, position: Cell, useBottomPosition = false): void {
|
|
43
46
|
this.positions.set(playerNode, { row: position.row, col: position.col });
|
|
44
|
-
playerNode.worldPosition =
|
|
47
|
+
playerNode.worldPosition = this.resolveCellPosition(position, useBottomPosition);
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
/** 获取单位当前格子,未记录时返回默认起点。 */
|
|
@@ -59,8 +62,9 @@ export class GridMovementController {
|
|
|
59
62
|
* 与其他同样开启该开关的单位可以走到同一格(互相穿过、不互相绕行、不互相等待),
|
|
60
63
|
* 与终点是否相同无关;未开启的单位之间、以及开启与未开启之间仍保持互斥。
|
|
61
64
|
* moveDuration:本次移动的"单格移动时长",单位秒;不传或传非法值时用控制器的默认值。
|
|
65
|
+
* useBottomPosition:为 true 时节点对齐到格子底部(bottom_positon),否则对齐到格子中心(GetMapPosition)。
|
|
62
66
|
*/
|
|
63
|
-
moveTo(playerNode: Node, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number): void {
|
|
67
|
+
moveTo(playerNode: Node, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number, useBottomPosition = false): void {
|
|
64
68
|
const start = this.getPosition(playerNode);
|
|
65
69
|
if (start.row === target.row && start.col === target.col) {
|
|
66
70
|
return;
|
|
@@ -69,13 +73,13 @@ export class GridMovementController {
|
|
|
69
73
|
this.cancel(playerNode, true);
|
|
70
74
|
const route = this.traceRoute(start, target, attackDistance, openColor, allowOverlap, this.movementOwners.get(playerNode));
|
|
71
75
|
if (!route || route.length === 0 || this.isPathBlocked(start, target, route, attackDistance)) {
|
|
72
|
-
this.retryPathfinding(playerNode, target, attackDistance, openColor, callBack, allowOverlap, moveDuration);
|
|
76
|
+
this.retryPathfinding(playerNode, target, attackDistance, openColor, callBack, allowOverlap, moveDuration, useBottomPosition);
|
|
73
77
|
return;
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
this.clearPathfindingRetry(playerNode);
|
|
77
81
|
route.shift();
|
|
78
|
-
this.startMovement(playerNode, route, start, target, attackDistance, openColor, callBack, allowOverlap, moveDuration);
|
|
82
|
+
this.startMovement(playerNode, route, start, target, attackDistance, openColor, callBack, allowOverlap, moveDuration, useBottomPosition);
|
|
79
83
|
}
|
|
80
84
|
|
|
81
85
|
/** 强制中止当前移动,可选择保留当前格子的临时占用。 */
|
|
@@ -122,7 +126,7 @@ export class GridMovementController {
|
|
|
122
126
|
* 执行逐格移动。
|
|
123
127
|
* 每次移动前会尝试保留下一格,失败说明被占用,就重新规划路径或延时重试。
|
|
124
128
|
*/
|
|
125
|
-
private startMovement(playerNode: Node, route: Cell[], start: Cell, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number): void {
|
|
129
|
+
private startMovement(playerNode: Node, route: Cell[], start: Cell, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number, useBottomPosition = false): void {
|
|
126
130
|
const previousOwner = this.movementOwners.get(playerNode);
|
|
127
131
|
if (previousOwner) {
|
|
128
132
|
this.pathState.release(previousOwner);
|
|
@@ -197,7 +201,7 @@ export class GridMovementController {
|
|
|
197
201
|
}
|
|
198
202
|
|
|
199
203
|
tween(playerNode)
|
|
200
|
-
.to(duration, { worldPosition:
|
|
204
|
+
.to(duration, { worldPosition: this.resolveCellPosition(next, useBottomPosition) })
|
|
201
205
|
.call(() => {
|
|
202
206
|
this.pathState.completeMove(previous, next, owner, shareToken);
|
|
203
207
|
this.positions.set(playerNode, { row: next.row, col: next.col });
|
|
@@ -239,12 +243,19 @@ export class GridMovementController {
|
|
|
239
243
|
return (typeof moveDuration === 'number' && moveDuration > 0) ? moveDuration : this.moveDuration;
|
|
240
244
|
}
|
|
241
245
|
|
|
246
|
+
/** 节点落到格子上的坐标:底部对齐用 bottom_positon,否则用格子中心 */
|
|
247
|
+
private resolveCellPosition(cell: Cell, useBottomPosition: boolean): Vec3 {
|
|
248
|
+
return useBottomPosition
|
|
249
|
+
? PathfindingModule.GetMapModel(cell.row, cell.col).bottom_positon
|
|
250
|
+
: PathfindingModule.GetMapPosition(cell.row, cell.col);
|
|
251
|
+
}
|
|
252
|
+
|
|
242
253
|
/** 路径规划失败时,做一次延迟重试,避免因为瞬时占用导致立刻失败。 */
|
|
243
|
-
private retryPathfinding(playerNode: Node, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number): void {
|
|
254
|
+
private retryPathfinding(playerNode: Node, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number, useBottomPosition = false): void {
|
|
244
255
|
this.clearPathfindingRetry(playerNode);
|
|
245
256
|
const retry = () => {
|
|
246
257
|
this.clearPathfindingRetry(playerNode);
|
|
247
|
-
this.moveTo(playerNode, target, attackDistance, openColor, callBack, allowOverlap, moveDuration);
|
|
258
|
+
this.moveTo(playerNode, target, attackDistance, openColor, callBack, allowOverlap, moveDuration, useBottomPosition);
|
|
248
259
|
};
|
|
249
260
|
this.movementRetryCallbacks.set(playerNode, retry);
|
|
250
261
|
this.scheduler.scheduleOnce(retry, this.retryDelay);
|
|
@@ -33,8 +33,12 @@ export class PathfindingModule {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
/**
|
|
37
|
+
* 设置单位所在格子,并同步节点世界坐标。
|
|
38
|
+
* useBottomPosition:false(默认)用格子中心 GetMapPosition,true 用格子底部 bottom_positon。
|
|
39
|
+
*/
|
|
40
|
+
static SetPosition(playerNode: Node, position: { row: number, col: number }, useBottomPosition = false): void {
|
|
41
|
+
this.movementController?.setPosition(playerNode, position, useBottomPosition);
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
static GetPosition(playerNode: Node): { row: number, col: number } {
|
|
@@ -48,9 +52,11 @@ export class PathfindingModule {
|
|
|
48
52
|
* 与终点是否相同无关;未开启的单位之间、以及开启与未开启之间仍保持互斥。
|
|
49
53
|
* moveDuration:本次移动的"单格移动时长",单位秒(数字越小走得越快)。
|
|
50
54
|
* 不传或传 <= 0 时使用 InitMovementController 配置的默认值。
|
|
55
|
+
* useBottomPosition:节点对齐方式。false(默认)用格子中心 GetMapPosition,
|
|
56
|
+
* true 用格子底部 GetMapModel(...).bottom_positon(角色脚底踩在格子底边)。
|
|
51
57
|
*/
|
|
52
|
-
static MoveTo(playerNode: Node, target: { row: number, col: number }, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number): void {
|
|
53
|
-
this.movementController?.moveTo(playerNode, target, attackDistance, openColor, callBack, allowOverlap, moveDuration);
|
|
58
|
+
static MoveTo(playerNode: Node, target: { row: number, col: number }, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number, useBottomPosition = false): void {
|
|
59
|
+
this.movementController?.moveTo(playerNode, target, attackDistance, openColor, callBack, allowOverlap, moveDuration, useBottomPosition);
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
static CancelMove(playerNode: Node, preserveCurrent = false): void {
|