@cc-component/cc-pathfinding 1.1.4 → 1.1.5
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.
|
@@ -4,7 +4,7 @@ import { AttackDistance } from "./base/Common";
|
|
|
4
4
|
import { MapBoxView } from "./base/MapBoxView";
|
|
5
5
|
import { Component } from "cc";
|
|
6
6
|
import { Node } from "cc";
|
|
7
|
-
import { GridTweenFactory } from "./base/GridMovementController";
|
|
7
|
+
import { DepthSortOptions, GridTweenFactory } from "./base/GridMovementController";
|
|
8
8
|
|
|
9
9
|
declare module "cc" {
|
|
10
10
|
|
|
@@ -57,7 +57,14 @@ declare global {
|
|
|
57
57
|
|
|
58
58
|
//#region 移动控制
|
|
59
59
|
/** 内部创建并绑定移动控制器 */
|
|
60
|
-
export function InitMovementController(scheduler: Component, options?: { retryDelay?: number; moveDuration?: number; tweenFactory?: GridTweenFactory }): void
|
|
60
|
+
export function InitMovementController(scheduler: Component, options?: { retryDelay?: number; moveDuration?: number; tweenFactory?: GridTweenFactory; depthSort?: boolean }): void
|
|
61
|
+
/**
|
|
62
|
+
* 开启 / 关闭深度排序
|
|
63
|
+
* @param setting 不传 → 内置插入式排序(默认按世界坐标 y,越小越靠前);
|
|
64
|
+
* 传 (node) => number → 内置排序 + 自定义排序键;
|
|
65
|
+
* 传 { handler: (nodes) => void } → 完全交给外部处理层级(一帧只回调一次)
|
|
66
|
+
*/
|
|
67
|
+
export function SetDepthSort(enabled: boolean, setting?: ((node: Node) => number) | DepthSortOptions): void
|
|
61
68
|
/**
|
|
62
69
|
* 注入自定义 tween 实现(比如换成项目自己的动画系统)
|
|
63
70
|
* @param factory 传 null 或从未调用时,自动使用 cc 自带 tween
|
|
@@ -12,11 +12,14 @@ import { Node } from 'cc';
|
|
|
12
12
|
*/
|
|
13
13
|
export class DepthSorter {
|
|
14
14
|
|
|
15
|
+
/** 默认排序键:y 越小(画面越靠下)越靠前显示 */
|
|
16
|
+
static readonly DEFAULT_ORDER = (node: Node): number => -node.worldPosition.y;
|
|
17
|
+
|
|
15
18
|
/**
|
|
16
19
|
* @param getOrder 排序键:键越大越靠前显示(siblingIndex 越大,越后绘制、盖在上面)。
|
|
17
|
-
*
|
|
20
|
+
* 默认按世界坐标 y:(node) => -node.worldPosition.y
|
|
18
21
|
*/
|
|
19
|
-
constructor(private readonly getOrder: (node: Node) => number) { }
|
|
22
|
+
constructor(private readonly getOrder: (node: Node) => number = DepthSorter.DEFAULT_ORDER) { }
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
25
|
* 把节点调整到正确的层级位置。位置已经正确时不做任何操作。
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Component, Node, Tween, tween, Vec3 } from 'cc';
|
|
2
2
|
import { AttackDistance } from './Common';
|
|
3
|
+
import { DepthSorter } from './DepthSorter';
|
|
3
4
|
import { Cell } from './GridMap';
|
|
4
5
|
import { PathGridStateManager } from './PathGridStateManager';
|
|
5
6
|
import { PathfindingModule } from './PathfindingModule';
|
|
@@ -47,6 +48,21 @@ export type MovementUpdateHandler = (playerNode: Node, worldPosition: Vec3) => v
|
|
|
47
48
|
*/
|
|
48
49
|
export type MovementFrameHandler = (playerNodes: Node[]) => void;
|
|
49
50
|
|
|
51
|
+
/**
|
|
52
|
+
* 深度排序配置。两种用法二选一(handler 优先):
|
|
53
|
+
* - getOrder:交给内置插入式排序,只传排序键,最省事;
|
|
54
|
+
* - handler:完全交给外部处理层级,内部只负责"脏标记 + 每帧合并成一次回调"。
|
|
55
|
+
*/
|
|
56
|
+
export interface DepthSortOptions {
|
|
57
|
+
/** 内置插入式排序的排序键:键越大越靠前显示(siblingIndex 越大) */
|
|
58
|
+
getOrder?: (node: Node) => number;
|
|
59
|
+
/** 外部处理器:每帧一次,参数是本帧坐标发生变化的单位 */
|
|
60
|
+
handler?: (nodes: Node[]) => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 直接传一个排序键函数,等价于 { getOrder } */
|
|
64
|
+
export type DepthSortSetting = ((node: Node) => number) | DepthSortOptions;
|
|
65
|
+
|
|
50
66
|
export interface GridMovementOptions {
|
|
51
67
|
/** 每次重试前的等待时间,单位秒 */
|
|
52
68
|
retryDelay?: number;
|
|
@@ -54,6 +70,11 @@ export interface GridMovementOptions {
|
|
|
54
70
|
moveDuration?: number;
|
|
55
71
|
/** 自定义 tween 实现,不传则用 cc 自带 tween */
|
|
56
72
|
tweenFactory?: GridTweenFactory;
|
|
73
|
+
/**
|
|
74
|
+
* 是否开启内置深度排序:移动中的单位每帧自动调整层级(只调整动过的节点)。
|
|
75
|
+
* 默认 false。排序键可通过 SetDepthSort 自定义。
|
|
76
|
+
*/
|
|
77
|
+
depthSort?: boolean;
|
|
57
78
|
}
|
|
58
79
|
|
|
59
80
|
/**
|
|
@@ -93,12 +114,19 @@ export class GridMovementController {
|
|
|
93
114
|
/** 本帧坐标发生变化的单位,合并回调后清空 */
|
|
94
115
|
private readonly dirtyNodes = new Set<Node>();
|
|
95
116
|
private frameFlushScheduled = false;
|
|
117
|
+
/** 内置深度排序器,开启后每帧自动调整动过的单位层级 */
|
|
118
|
+
private depthSorter: DepthSorter | null = null;
|
|
119
|
+
/** 外部深度排序处理器,设置后内置排序不生效 */
|
|
120
|
+
private depthSortHandler: ((nodes: Node[]) => void) | null = null;
|
|
96
121
|
|
|
97
122
|
constructor(private readonly scheduler: Component, options: GridMovementOptions = {}) {
|
|
98
123
|
// 允许外部按场景调整重试间隔和动画时长,默认值保持兼容。
|
|
99
124
|
this.retryDelay = options.retryDelay ?? 1;
|
|
100
125
|
this.moveDuration = options.moveDuration ?? 0.5;
|
|
101
126
|
this.customTweenFactory = options.tweenFactory ?? null;
|
|
127
|
+
if (options.depthSort) {
|
|
128
|
+
this.setDepthSort(true);
|
|
129
|
+
}
|
|
102
130
|
this.pathState = new PathGridStateManager({
|
|
103
131
|
isOccupied: (row, col) => PathfindingModule.IsOccupied(row, col),
|
|
104
132
|
occupy: (row, col) => PathfindingModule.OccupyNode(row, col, true),
|
|
@@ -147,6 +175,36 @@ export class GridMovementController {
|
|
|
147
175
|
}
|
|
148
176
|
}
|
|
149
177
|
|
|
178
|
+
/**
|
|
179
|
+
* 开启 / 关闭深度排序。
|
|
180
|
+
* @param setting 不传 → 内置插入式排序 + 默认键(世界坐标 y,越小越靠前);
|
|
181
|
+
* 传 (node) => number → 内置排序 + 自定义键;
|
|
182
|
+
* 传 { handler } → 完全交给外部处理层级(内部只做脏标记与每帧合并)。
|
|
183
|
+
*/
|
|
184
|
+
setDepthSort(enabled: boolean, setting?: DepthSortSetting): void {
|
|
185
|
+
if (!enabled) {
|
|
186
|
+
this.depthSorter = null;
|
|
187
|
+
this.depthSortHandler = null;
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (typeof setting === 'function') {
|
|
191
|
+
this.depthSorter = new DepthSorter(setting);
|
|
192
|
+
this.depthSortHandler = null;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
this.depthSortHandler = setting?.handler ?? null;
|
|
196
|
+
// 有外部处理器时内置排序不生效,避免两套逻辑重复跑
|
|
197
|
+
this.depthSorter = this.depthSortHandler ? null : new DepthSorter(setting?.getOrder);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** 是否需要在 tween 上挂每帧回调(外部监听 或 深度排序) */
|
|
201
|
+
private needsFrameUpdate(): boolean {
|
|
202
|
+
return this.movementUpdateHandler !== null
|
|
203
|
+
|| this.movementFrameHandler !== null
|
|
204
|
+
|| this.depthSorter !== null
|
|
205
|
+
|| this.depthSortHandler !== null;
|
|
206
|
+
}
|
|
207
|
+
|
|
150
208
|
/** 脏标记:只记录"动过",真正的回调合并到下一帧统一触发一次 */
|
|
151
209
|
private markDirty(playerNode: Node): void {
|
|
152
210
|
this.dirtyNodes.add(playerNode);
|
|
@@ -159,15 +217,16 @@ export class GridMovementController {
|
|
|
159
217
|
|
|
160
218
|
private readonly flushMovementFrame = (): void => {
|
|
161
219
|
this.frameFlushScheduled = false;
|
|
162
|
-
const handler = this.movementFrameHandler;
|
|
163
|
-
if (!handler) {
|
|
164
|
-
this.dirtyNodes.clear();
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
220
|
const nodes: Node[] = [];
|
|
168
221
|
this.dirtyNodes.forEach(node => nodes.push(node));
|
|
169
222
|
this.dirtyNodes.clear();
|
|
170
|
-
|
|
223
|
+
// 深度排序:有外部处理器就交给外部,否则用内置插入式排序
|
|
224
|
+
if (this.depthSortHandler) {
|
|
225
|
+
this.depthSortHandler(nodes);
|
|
226
|
+
} else {
|
|
227
|
+
this.depthSorter?.updateAll(nodes);
|
|
228
|
+
}
|
|
229
|
+
this.movementFrameHandler?.(nodes);
|
|
171
230
|
};
|
|
172
231
|
|
|
173
232
|
/** 获取某个单位当前实际生效的单格移动时长(秒) */
|
|
@@ -346,11 +405,11 @@ export class GridMovementController {
|
|
|
346
405
|
stepping = true;
|
|
347
406
|
// 有外部监听时才挂每帧回调;批量监听只做脏标记(一次 Set.add),排序等重活每帧合并一次
|
|
348
407
|
const updateHandler = this.movementUpdateHandler;
|
|
349
|
-
const needOnUpdate =
|
|
408
|
+
const needOnUpdate = this.needsFrameUpdate();
|
|
350
409
|
const toOptions: GridTweenOption | undefined = needOnUpdate
|
|
351
410
|
? {
|
|
352
411
|
onUpdate: () => {
|
|
353
|
-
if (this.movementFrameHandler) {
|
|
412
|
+
if (this.movementFrameHandler || this.depthSorter || this.depthSortHandler) {
|
|
354
413
|
this.markDirty(playerNode);
|
|
355
414
|
}
|
|
356
415
|
updateHandler?.(playerNode, playerNode.getWorldPosition(GridMovementController.TMP_POSITION));
|
|
@@ -4,7 +4,7 @@ import { Vec3 } from 'cc';
|
|
|
4
4
|
import { MapViewModel } from './MapViewModel';
|
|
5
5
|
import { Color } from 'cc';
|
|
6
6
|
import { AttackDistance } from './Common';
|
|
7
|
-
import { GridMovementController, GridTweenFactory } from './GridMovementController';
|
|
7
|
+
import { DepthSortOptions, GridMovementController, GridTweenFactory } from './GridMovementController';
|
|
8
8
|
const { ccclass, property } = _decorator;
|
|
9
9
|
|
|
10
10
|
@ccclass('PathfindingModule')
|
|
@@ -27,7 +27,7 @@ export class PathfindingModule {
|
|
|
27
27
|
this.movementController = controller;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
static InitMovementController(scheduler: Component, options?: { retryDelay?: number; moveDuration?: number; tweenFactory?: GridTweenFactory }): void {
|
|
30
|
+
static InitMovementController(scheduler: Component, options?: { retryDelay?: number; moveDuration?: number; tweenFactory?: GridTweenFactory; depthSort?: boolean }): void {
|
|
31
31
|
if (!this.movementController) {
|
|
32
32
|
this.movementController = new GridMovementController(scheduler, options);
|
|
33
33
|
}
|
|
@@ -66,6 +66,19 @@ export class PathfindingModule {
|
|
|
66
66
|
this.movementController?.setMovementUpdateHandler(handler);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* 开启 / 关闭深度排序(三种用法):
|
|
71
|
+
* 1. SetDepthSort(true) → 内置插入式排序,默认按世界坐标 y(y 越小越靠前)
|
|
72
|
+
* 2. SetDepthSort(true, (node) => -node.worldPosition.y) → 内置排序 + 自定义排序键
|
|
73
|
+
* 3. SetDepthSort(true, { handler: (nodes) => {...} }) → 完全交给外部处理层级,
|
|
74
|
+
* 内部只负责脏标记并把一帧内的多次变化合并成一次回调(nodes 是本帧动过的单位)
|
|
75
|
+
*
|
|
76
|
+
* 传 false 关闭。内置排序只调整本帧动过的节点,不做全量排序,单位再多也是 O(移动位数)。
|
|
77
|
+
*/
|
|
78
|
+
static SetDepthSort(enabled: boolean, setting?: ((node: Node) => number) | DepthSortOptions): void {
|
|
79
|
+
this.movementController?.setDepthSort(enabled, setting);
|
|
80
|
+
}
|
|
81
|
+
|
|
69
82
|
/**
|
|
70
83
|
* 批量监听"移动过程中位置更新"(推荐,多个单位时用它):
|
|
71
84
|
* 无论多少个单位在移动,每帧只回调一次,参数是本帧坐标发生变化的单位数组。
|