@cc-component/cc-pathfinding 1.1.3 → 1.1.4

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.
@@ -78,6 +78,19 @@ declare global {
78
78
  export function SetMoveDuration(playerNode: Node, moveDuration: number): void
79
79
  /** 获取单位当前的单格移动时长(秒) */
80
80
  export function GetMoveDuration(playerNode: Node): number
81
+ /**
82
+ * 监听移动过程中的位置更新:tween 每更新一次节点坐标就回调一次,
83
+ * 适合外部在这里刷新节点层级(按 y 做深度排序)
84
+ * @param handler (playerNode, worldPosition),worldPosition 是复用的临时向量,仅在回调内有效;
85
+ * 传 null 取消监听
86
+ */
87
+ export function SetMovementUpdateHandler(handler: ((playerNode: Node, worldPosition: Vec3) => void) | null): void
88
+ /**
89
+ * 批量监听移动中的位置更新(多个单位时推荐):无论多少个单位在移动,每帧只回调一次,
90
+ * 参数是本帧坐标发生变化的单位数组
91
+ * @param handler 传 null 取消监听;与 SetMovementUpdateHandler 二选一
92
+ */
93
+ export function SetMovementFrameHandler(handler: ((playerNodes: Node[]) => void) | null): void
81
94
  /**
82
95
  * 让单位移动到目标格子
83
96
  * @param allowOverlap 为 true 时该单位进入"允许重叠"模式,
@@ -0,0 +1,68 @@
1
+ import { Node } from 'cc';
2
+
3
+ /**
4
+ * 增量深度排序器(配合移动中的位置更新回调使用)。
5
+ *
6
+ * 为什么不用全量排序:上百个单位每帧对 parent.children 做一次 sort + 逐个 setSiblingIndex,
7
+ * 开销是 O(N log N) 且会打乱整个 children 数组;而实际每帧真正需要挪动的往往只有少数几个节点。
8
+ *
9
+ * 这里的做法是"插入式调整":只对动过的节点,与相邻兄弟逐个比较并交换,
10
+ * 直到它落在正确位置。大多数帧只需要 0~1 次 setSiblingIndex,
11
+ * 单次开销 O(移动的位数),与单位总数无关。
12
+ */
13
+ export class DepthSorter {
14
+
15
+ /**
16
+ * @param getOrder 排序键:键越大越靠前显示(siblingIndex 越大,越后绘制、盖在上面)。
17
+ * 常见写法:(node) => -node.worldPosition.y —— y 越小(画面越靠下)越靠前。
18
+ */
19
+ constructor(private readonly getOrder: (node: Node) => number) { }
20
+
21
+ /**
22
+ * 把节点调整到正确的层级位置。位置已经正确时不做任何操作。
23
+ * @returns 是否发生过调整
24
+ */
25
+ update(node: Node): boolean {
26
+ if (!node?.isValid) {
27
+ return false;
28
+ }
29
+ const parent = node.parent;
30
+ if (!parent) {
31
+ return false;
32
+ }
33
+
34
+ const order = this.getOrder(node);
35
+ const siblings = parent.children;
36
+ let index = node.getSiblingIndex();
37
+ const startIndex = index;
38
+
39
+ // 向前挪:前面的兄弟比自己更"靠前",就往后再换(index 变大 = 后绘制 = 盖在上面)
40
+ while (index < siblings.length - 1) {
41
+ const nextSibling = siblings[index + 1];
42
+ if (nextSibling === node || this.getOrder(nextSibling) >= order) {
43
+ break;
44
+ }
45
+ node.setSiblingIndex(index + 1);
46
+ index++;
47
+ }
48
+
49
+ // 向后挪:后面的兄弟比自己更"靠后",就往前换
50
+ while (index > 0) {
51
+ const prevSibling = siblings[index - 1];
52
+ if (prevSibling === node || this.getOrder(prevSibling) <= order) {
53
+ break;
54
+ }
55
+ node.setSiblingIndex(index - 1);
56
+ index--;
57
+ }
58
+
59
+ return index !== startIndex;
60
+ }
61
+
62
+ /** 批量调整:只对传入的(脏)节点做插入式调整 */
63
+ updateAll(nodes: Node[]): void {
64
+ for (const node of nodes) {
65
+ this.update(node);
66
+ }
67
+ }
68
+ }
@@ -4,9 +4,15 @@ import { Cell } from './GridMap';
4
4
  import { PathGridStateManager } from './PathGridStateManager';
5
5
  import { PathfindingModule } from './PathfindingModule';
6
6
 
7
+ /** to() 的可选项(与 cc 的 ITweenOption 用法一致) */
8
+ export interface GridTweenOption {
9
+ /** 每帧回调:tween 更新完目标属性后触发 */
10
+ onUpdate?: (target: Node) => void;
11
+ }
12
+
7
13
  /** 外部 tween 对象需要满足的最小接口(与 cc.Tween 用法一致) */
8
14
  export interface GridTween {
9
- to(duration: number, props: object): GridTween;
15
+ to(duration: number, props: object, opts?: GridTweenOption): GridTween;
10
16
  call(callback: () => void): GridTween;
11
17
  start(): GridTween;
12
18
  }
@@ -27,6 +33,20 @@ export interface GridTweenFactory {
27
33
  stopAllByTarget(target: Node): void;
28
34
  }
29
35
 
36
+ /**
37
+ * 移动过程中"位置更新"的回调。tween 每更新一次节点坐标就触发一次,
38
+ * 常用于外部刷新节点层级(按 y 做深度排序,越靠下越靠前显示)。
39
+ * @param worldPosition 复用同一个临时向量,只在本次回调内有效,不要保存引用
40
+ */
41
+ export type MovementUpdateHandler = (playerNode: Node, worldPosition: Vec3) => void;
42
+
43
+ /**
44
+ * 批量监听:多个单位同时移动时,每帧只回调一次,参数是"本帧坐标发生变化"的单位。
45
+ * 相比 MovementUpdateHandler(每个单位每帧各回调一次),把 N 次调用合并成 1 次,
46
+ * 排序等重活每帧只做一遍。单位越多收益越大。
47
+ */
48
+ export type MovementFrameHandler = (playerNodes: Node[]) => void;
49
+
30
50
  export interface GridMovementOptions {
31
51
  /** 每次重试前的等待时间,单位秒 */
32
52
  retryDelay?: number;
@@ -43,6 +63,8 @@ export interface GridMovementOptions {
43
63
  export class GridMovementController {
44
64
  /** 允许重叠的单位共用的共享令牌 */
45
65
  private static readonly OVERLAP_TOKEN = 'overlap';
66
+ /** 位置更新回调里复用,避免每帧分配向量 */
67
+ private static readonly TMP_POSITION = new Vec3();
46
68
 
47
69
  private readonly movementVersions = new Map<Node, number>();
48
70
  private readonly movementOwners = new Map<Node, symbol>();
@@ -64,6 +86,13 @@ export class GridMovementController {
64
86
  /** 正在播放的那一格的"重放"入口,用于改速度时立即生效 */
65
87
  private readonly activeStepReplayers = new Map<Node, () => void>();
66
88
  private customTweenFactory: GridTweenFactory | null;
89
+ /** 移动中位置更新的外部回调(每单位每帧) */
90
+ private movementUpdateHandler: MovementUpdateHandler | null = null;
91
+ /** 移动中位置更新的外部回调(每帧合并一次) */
92
+ private movementFrameHandler: MovementFrameHandler | null = null;
93
+ /** 本帧坐标发生变化的单位,合并回调后清空 */
94
+ private readonly dirtyNodes = new Set<Node>();
95
+ private frameFlushScheduled = false;
67
96
 
68
97
  constructor(private readonly scheduler: Component, options: GridMovementOptions = {}) {
69
98
  // 允许外部按场景调整重试间隔和动画时长,默认值保持兼容。
@@ -99,6 +128,48 @@ export class GridMovementController {
99
128
  this.activeStepReplayers.get(playerNode)?.();
100
129
  }
101
130
 
131
+ /**
132
+ * 设置"移动中位置更新"回调:tween 每更新一次节点坐标就触发一次,
133
+ * 外部可据此刷新节点层级(深度排序)。传 null 取消监听。
134
+ */
135
+ setMovementUpdateHandler(handler: MovementUpdateHandler | null): void {
136
+ this.movementUpdateHandler = handler ?? null;
137
+ }
138
+
139
+ /**
140
+ * 设置批量监听:多个单位同时移动时每帧只回调一次(参数是本帧动过的单位)。
141
+ * 传 null 取消监听。与 setMovementUpdateHandler 二选一即可,不要两个都设。
142
+ */
143
+ setMovementFrameHandler(handler: MovementFrameHandler | null): void {
144
+ this.movementFrameHandler = handler ?? null;
145
+ if (!this.movementFrameHandler) {
146
+ this.dirtyNodes.clear();
147
+ }
148
+ }
149
+
150
+ /** 脏标记:只记录"动过",真正的回调合并到下一帧统一触发一次 */
151
+ private markDirty(playerNode: Node): void {
152
+ this.dirtyNodes.add(playerNode);
153
+ if (this.frameFlushScheduled) {
154
+ return;
155
+ }
156
+ this.frameFlushScheduled = true;
157
+ this.scheduler.scheduleOnce(this.flushMovementFrame, 0);
158
+ }
159
+
160
+ private readonly flushMovementFrame = (): void => {
161
+ this.frameFlushScheduled = false;
162
+ const handler = this.movementFrameHandler;
163
+ if (!handler) {
164
+ this.dirtyNodes.clear();
165
+ return;
166
+ }
167
+ const nodes: Node[] = [];
168
+ this.dirtyNodes.forEach(node => nodes.push(node));
169
+ this.dirtyNodes.clear();
170
+ handler(nodes);
171
+ };
172
+
102
173
  /** 获取某个单位当前实际生效的单格移动时长(秒) */
103
174
  getMoveDuration(playerNode: Node): number {
104
175
  return this.resolveMoveDuration(
@@ -181,6 +252,9 @@ export class GridMovementController {
181
252
 
182
253
  /** 销毁控制器,并清理所有单位的动画、重试和格子占用状态。 */
183
254
  destroy(): void {
255
+ this.scheduler.unschedule(this.flushMovementFrame);
256
+ this.frameFlushScheduled = false;
257
+ this.dirtyNodes.clear();
184
258
  this.movementVersions.clear();
185
259
  this.movementOwners.forEach((owner, playerNode) => {
186
260
  this.pathState.release(owner);
@@ -270,8 +344,21 @@ export class GridMovementController {
270
344
  const ratio = total > 0 ? Math.min(1, remain / total) : 1;
271
345
 
272
346
  stepping = true;
347
+ // 有外部监听时才挂每帧回调;批量监听只做脏标记(一次 Set.add),排序等重活每帧合并一次
348
+ const updateHandler = this.movementUpdateHandler;
349
+ const needOnUpdate = updateHandler !== null || this.movementFrameHandler !== null;
350
+ const toOptions: GridTweenOption | undefined = needOnUpdate
351
+ ? {
352
+ onUpdate: () => {
353
+ if (this.movementFrameHandler) {
354
+ this.markDirty(playerNode);
355
+ }
356
+ updateHandler?.(playerNode, playerNode.getWorldPosition(GridMovementController.TMP_POSITION));
357
+ },
358
+ }
359
+ : undefined;
273
360
  this.tweenFactory.tween(playerNode)
274
- .to(stepDuration() * ratio, { worldPosition: to })
361
+ .to(stepDuration() * ratio, { worldPosition: to }, toOptions)
275
362
  .call(() => {
276
363
  stepping = false;
277
364
  this.pathState.completeMove(previous, next, owner, shareToken);
@@ -56,6 +56,26 @@ export class PathfindingModule {
56
56
  this.movementController?.setMoveDuration(playerNode, moveDuration);
57
57
  }
58
58
 
59
+ /**
60
+ * 监听"移动过程中位置更新":tween 每更新一次节点坐标就回调一次,
61
+ * 适合外部在这里刷新节点层级(按 y 做深度排序、越靠下越靠前显示)。
62
+ * 传 null 取消监听。
63
+ * @param handler (playerNode, worldPosition),worldPosition 是复用的临时向量,仅在回调内有效
64
+ */
65
+ static SetMovementUpdateHandler(handler: ((playerNode: Node, worldPosition: Vec3) => void) | null): void {
66
+ this.movementController?.setMovementUpdateHandler(handler);
67
+ }
68
+
69
+ /**
70
+ * 批量监听"移动过程中位置更新"(推荐,多个单位时用它):
71
+ * 无论多少个单位在移动,每帧只回调一次,参数是本帧坐标发生变化的单位数组。
72
+ * 单位越多收益越大:N 次回调 / N 次排序 收敛为 1 次。
73
+ * 传 null 取消监听。与 SetMovementUpdateHandler 二选一,不要两个都设。
74
+ */
75
+ static SetMovementFrameHandler(handler: ((playerNodes: Node[]) => void) | null): void {
76
+ this.movementController?.setMovementFrameHandler(handler);
77
+ }
78
+
59
79
  /** 获取单位当前的单格移动时长(秒) */
60
80
  static GetMoveDuration(playerNode: Node): number {
61
81
  return this.movementController?.getMoveDuration(playerNode) ?? 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cc-component/cc-pathfinding",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "engine": ">=3.8.6",
5
5
  "description": "系统组件添加常用扩展方法",
6
6
  "main": "index.ts",