@cc-component/cc-pathfinding 1.0.8 → 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,15 +57,23 @@ 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 时该单位进入"允许重叠"模式,
|
|
65
69
|
* 与其他同样开启该开关的单位可以走到同一格(互相穿过,不再互相绕行/互相等待),
|
|
66
70
|
* 与终点是否相同无关
|
|
71
|
+
* @param moveDuration 本次移动的"单格移动时长",单位秒(越小越快);
|
|
72
|
+
* 不传或传 <= 0 时使用 InitMovementController 配置的默认值
|
|
73
|
+
* @param useBottomPosition 节点对齐方式:false(默认)用格子中心 GetMapPosition,
|
|
74
|
+
* true 用格子底部 GetMapModel(...).bottom_positon
|
|
67
75
|
*/
|
|
68
|
-
export function MoveTo(playerNode: Node, target: { row: number, col: number }, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap?: boolean): 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
|
|
69
77
|
/** 取消移动 */
|
|
70
78
|
export function CancelMove(playerNode: Node, preserveCurrent?: boolean): void
|
|
71
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
|
/** 获取单位当前格子,未记录时返回默认起点。 */
|
|
@@ -58,8 +61,10 @@ export class GridMovementController {
|
|
|
58
61
|
* allowOverlap:为 true 时该单位进入"允许重叠"模式,
|
|
59
62
|
* 与其他同样开启该开关的单位可以走到同一格(互相穿过、不互相绕行、不互相等待),
|
|
60
63
|
* 与终点是否相同无关;未开启的单位之间、以及开启与未开启之间仍保持互斥。
|
|
64
|
+
* moveDuration:本次移动的"单格移动时长",单位秒;不传或传非法值时用控制器的默认值。
|
|
65
|
+
* useBottomPosition:为 true 时节点对齐到格子底部(bottom_positon),否则对齐到格子中心(GetMapPosition)。
|
|
61
66
|
*/
|
|
62
|
-
moveTo(playerNode: Node, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false): void {
|
|
67
|
+
moveTo(playerNode: Node, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number, useBottomPosition = false): void {
|
|
63
68
|
const start = this.getPosition(playerNode);
|
|
64
69
|
if (start.row === target.row && start.col === target.col) {
|
|
65
70
|
return;
|
|
@@ -68,13 +73,13 @@ export class GridMovementController {
|
|
|
68
73
|
this.cancel(playerNode, true);
|
|
69
74
|
const route = this.traceRoute(start, target, attackDistance, openColor, allowOverlap, this.movementOwners.get(playerNode));
|
|
70
75
|
if (!route || route.length === 0 || this.isPathBlocked(start, target, route, attackDistance)) {
|
|
71
|
-
this.retryPathfinding(playerNode, target, attackDistance, openColor, callBack, allowOverlap);
|
|
76
|
+
this.retryPathfinding(playerNode, target, attackDistance, openColor, callBack, allowOverlap, moveDuration, useBottomPosition);
|
|
72
77
|
return;
|
|
73
78
|
}
|
|
74
79
|
|
|
75
80
|
this.clearPathfindingRetry(playerNode);
|
|
76
81
|
route.shift();
|
|
77
|
-
this.startMovement(playerNode, route, start, target, attackDistance, openColor, callBack, allowOverlap);
|
|
82
|
+
this.startMovement(playerNode, route, start, target, attackDistance, openColor, callBack, allowOverlap, moveDuration, useBottomPosition);
|
|
78
83
|
}
|
|
79
84
|
|
|
80
85
|
/** 强制中止当前移动,可选择保留当前格子的临时占用。 */
|
|
@@ -121,7 +126,7 @@ export class GridMovementController {
|
|
|
121
126
|
* 执行逐格移动。
|
|
122
127
|
* 每次移动前会尝试保留下一格,失败说明被占用,就重新规划路径或延时重试。
|
|
123
128
|
*/
|
|
124
|
-
private startMovement(playerNode: Node, route: Cell[], start: Cell, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false): 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 {
|
|
125
130
|
const previousOwner = this.movementOwners.get(playerNode);
|
|
126
131
|
if (previousOwner) {
|
|
127
132
|
this.pathState.release(previousOwner);
|
|
@@ -130,6 +135,8 @@ export class GridMovementController {
|
|
|
130
135
|
this.movementOwners.set(playerNode, owner);
|
|
131
136
|
const shareToken = this.getShareToken(allowOverlap);
|
|
132
137
|
this.pathState.reserveCurrent(start, owner, shareToken);
|
|
138
|
+
// 单格移动时长:本次指定优先,否则用控制器默认值
|
|
139
|
+
const duration = this.resolveMoveDuration(moveDuration);
|
|
133
140
|
const version = this.bumpVersion(playerNode);
|
|
134
141
|
let previous = start;
|
|
135
142
|
let index = 0;
|
|
@@ -194,7 +201,7 @@ export class GridMovementController {
|
|
|
194
201
|
}
|
|
195
202
|
|
|
196
203
|
tween(playerNode)
|
|
197
|
-
.to(
|
|
204
|
+
.to(duration, { worldPosition: this.resolveCellPosition(next, useBottomPosition) })
|
|
198
205
|
.call(() => {
|
|
199
206
|
this.pathState.completeMove(previous, next, owner, shareToken);
|
|
200
207
|
this.positions.set(playerNode, { row: next.row, col: next.col });
|
|
@@ -231,12 +238,24 @@ export class GridMovementController {
|
|
|
231
238
|
return allowOverlap ? GridMovementController.OVERLAP_TOKEN : null;
|
|
232
239
|
}
|
|
233
240
|
|
|
241
|
+
/** 单格移动时长:未指定或非法时回退到控制器默认值(单位秒) */
|
|
242
|
+
private resolveMoveDuration(moveDuration?: number): number {
|
|
243
|
+
return (typeof moveDuration === 'number' && moveDuration > 0) ? moveDuration : this.moveDuration;
|
|
244
|
+
}
|
|
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
|
+
|
|
234
253
|
/** 路径规划失败时,做一次延迟重试,避免因为瞬时占用导致立刻失败。 */
|
|
235
|
-
private retryPathfinding(playerNode: Node, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false): void {
|
|
254
|
+
private retryPathfinding(playerNode: Node, target: Cell, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false, moveDuration?: number, useBottomPosition = false): void {
|
|
236
255
|
this.clearPathfindingRetry(playerNode);
|
|
237
256
|
const retry = () => {
|
|
238
257
|
this.clearPathfindingRetry(playerNode);
|
|
239
|
-
this.moveTo(playerNode, target, attackDistance, openColor, callBack, allowOverlap);
|
|
258
|
+
this.moveTo(playerNode, target, attackDistance, openColor, callBack, allowOverlap, moveDuration, useBottomPosition);
|
|
240
259
|
};
|
|
241
260
|
this.movementRetryCallbacks.set(playerNode, retry);
|
|
242
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 } {
|
|
@@ -46,9 +50,13 @@ export class PathfindingModule {
|
|
|
46
50
|
* allowOverlap:为 true 时该单位进入"允许重叠"模式,
|
|
47
51
|
* 与其他同样开启该开关的单位可以走到同一格(互相穿过,不再互相绕行/互相等待),
|
|
48
52
|
* 与终点是否相同无关;未开启的单位之间、以及开启与未开启之间仍保持互斥。
|
|
53
|
+
* moveDuration:本次移动的"单格移动时长",单位秒(数字越小走得越快)。
|
|
54
|
+
* 不传或传 <= 0 时使用 InitMovementController 配置的默认值。
|
|
55
|
+
* useBottomPosition:节点对齐方式。false(默认)用格子中心 GetMapPosition,
|
|
56
|
+
* true 用格子底部 GetMapModel(...).bottom_positon(角色脚底踩在格子底边)。
|
|
49
57
|
*/
|
|
50
|
-
static MoveTo(playerNode: Node, target: { row: number, col: number }, attackDistance: AttackDistance, openColor: boolean, callBack?: () => void, allowOverlap = false): void {
|
|
51
|
-
this.movementController?.moveTo(playerNode, target, attackDistance, openColor, callBack, allowOverlap);
|
|
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);
|
|
52
60
|
}
|
|
53
61
|
|
|
54
62
|
static CancelMove(playerNode: Node, preserveCurrent = false): void {
|