@esengine/pathfinding 1.0.1 → 1.0.3

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.
package/bin/README.md DELETED
@@ -1,245 +0,0 @@
1
- # 寻路算法库
2
-
3
- 适用于Cocos Creator和Laya引擎的寻路算法库,支持A*和广度优先搜索算法。
4
-
5
- ## 特性
6
-
7
- - **多算法支持**:A*、广度优先搜索
8
- - **引擎兼容**:支持Cocos Creator和Laya引擎
9
- - **TypeScript**:完整的类型定义
10
- - **模块化**:支持按需加载
11
-
12
- ## 安装
13
-
14
- ```bash
15
- npm install @esengine/pathfinding
16
- ```
17
-
18
- ## 算法选择
19
-
20
- ### A*算法
21
- - **适用场景**:大部分游戏寻路需求
22
- - **特点**:支持权重地形,路径质量好
23
- - **推荐用于**:RPG、策略游戏、塔防游戏
24
-
25
- ### 广度优先搜索
26
- - **适用场景**:简单网格寻路
27
- - **特点**:保证最短路径(步数最少)
28
- - **推荐用于**:迷宫游戏、推箱子游戏
29
-
30
- ## 快速开始
31
-
32
- ### 基本使用
33
-
34
- ```typescript
35
- import { AstarGridGraph, Vector2Utils } from '@esengine/pathfinding';
36
-
37
- // 创建20x20的网格
38
- const graph = new AstarGridGraph(20, 20);
39
-
40
- // 添加障碍物
41
- graph.addWall(Vector2Utils.create(10, 10));
42
- graph.addWall(Vector2Utils.create(10, 11));
43
-
44
- // 添加难走地形(权重节点)
45
- graph.addWeightedNode(Vector2Utils.create(5, 5));
46
-
47
- // 搜索路径
48
- const start = Vector2Utils.create(0, 0);
49
- const goal = Vector2Utils.create(19, 19);
50
- const path = graph.searchPath(start, goal);
51
-
52
- console.log('路径:', path);
53
- ```
54
-
55
- ### 广度优先搜索
56
-
57
- ```typescript
58
- import { UnweightedGridGraph } from '@esengine/pathfinding';
59
-
60
- // 创建网格,支持对角线移动
61
- const graph = new UnweightedGridGraph(10, 10, true);
62
-
63
- // 添加障碍物
64
- graph.walls.push({ x: 5, y: 5 });
65
-
66
- // 搜索路径
67
- const path = graph.searchPath({ x: 0, y: 0 }, { x: 9, y: 9 });
68
- ```
69
-
70
- ### 按需加载
71
-
72
- ```typescript
73
- // 只使用A*算法
74
- import { AstarGridGraph } from '@esengine/pathfinding/modules/astar';
75
-
76
- // 只使用广度优先算法
77
- import { UnweightedGridGraph } from '@esengine/pathfinding/modules/breadth-first';
78
- ```
79
-
80
- ## 在游戏引擎中使用
81
-
82
- ### Cocos Creator
83
-
84
- ```typescript
85
- import { AstarGridGraph } from '@esengine/pathfinding';
86
-
87
- export default class PathfindingComponent extends cc.Component {
88
- private graph: AstarGridGraph;
89
-
90
- onLoad() {
91
- this.graph = new AstarGridGraph(50, 50);
92
- this.setupObstacles();
93
- }
94
-
95
- findPath(start: cc.Vec2, goal: cc.Vec2): cc.Vec2[] {
96
- return this.graph.searchPath(start, goal) as cc.Vec2[];
97
- }
98
-
99
- private setupObstacles() {
100
- // 添加地图障碍物
101
- for (let x = 10; x < 15; x++) {
102
- this.graph.addWall(cc.v2(x, 10));
103
- }
104
- }
105
- }
106
- ```
107
-
108
- ### Laya引擎
109
-
110
- ```typescript
111
- import { AstarGridGraph } from '@esengine/pathfinding';
112
-
113
- export class PathfindingManager {
114
- private graph: AstarGridGraph;
115
-
116
- constructor(mapWidth: number, mapHeight: number) {
117
- this.graph = new AstarGridGraph(mapWidth, mapHeight);
118
- }
119
-
120
- findPath(start: Laya.Vector2, goal: Laya.Vector2): Laya.Vector2[] {
121
- return this.graph.searchPath(start, goal) as Laya.Vector2[];
122
- }
123
-
124
- addObstacle(pos: Laya.Vector2) {
125
- this.graph.addWall(pos);
126
- }
127
- }
128
- ```
129
-
130
- ## API文档
131
-
132
- ### AstarGridGraph
133
-
134
- A*算法网格图实现。
135
-
136
- ```typescript
137
- const graph = new AstarGridGraph(width: number, height: number);
138
-
139
- // 属性
140
- graph.walls: IVector2[] // 障碍物数组
141
- graph.weightedNodes: IVector2[] // 加权节点数组
142
- graph.defaultWeight: number // 默认移动成本(默认1)
143
- graph.weightedNodeWeight: number // 加权节点成本(默认5)
144
-
145
- // 方法
146
- graph.searchPath(start, goal): IVector2[] // 搜索完整路径
147
- graph.search(start, goal): boolean // 检查是否存在路径
148
- graph.addWall(wall): void // 添加单个障碍物
149
- graph.addWalls(walls): void // 批量添加障碍物
150
- graph.clearWalls(): void // 清空障碍物
151
- graph.addWeightedNode(node): void // 添加加权节点
152
- graph.clearWeightedNodes(): void // 清空加权节点
153
- ```
154
-
155
- ### UnweightedGridGraph
156
-
157
- 广度优先搜索网格图实现。
158
-
159
- ```typescript
160
- const graph = new UnweightedGridGraph(
161
- width: number,
162
- height: number,
163
- allowDiagonal?: boolean // 是否允许对角线移动
164
- );
165
-
166
- // 属性
167
- graph.walls: IVector2[] // 障碍物数组
168
-
169
- // 方法
170
- graph.searchPath(start, goal): IVector2[] // 搜索完整路径
171
- graph.hasPath(start, goal): boolean // 检查是否存在路径
172
- ```
173
-
174
- ### Vector2Utils
175
-
176
- 向量操作工具类。
177
-
178
- ```typescript
179
- Vector2Utils.create(x, y) // 创建向量
180
- Vector2Utils.equals(a, b) // 判断相等
181
- Vector2Utils.add(a, b) // 向量加法
182
- Vector2Utils.manhattanDistance(a, b) // 曼哈顿距离
183
- Vector2Utils.distance(a, b) // 欧几里得距离
184
- ```
185
-
186
- ## 游戏场景示例
187
-
188
- ### 塔防游戏
189
-
190
- ```typescript
191
- // 敌人寻路到基地
192
- const graph = new AstarGridGraph(mapWidth, mapHeight);
193
- towers.forEach(tower => graph.addWall(tower.position));
194
- const path = graph.searchPath(spawnPoint, basePosition);
195
- ```
196
-
197
- ### RPG游戏
198
-
199
- ```typescript
200
- // 角色移动寻路,设置不同地形权重
201
- const graph = new AstarGridGraph(mapWidth, mapHeight);
202
- swampTiles.forEach(tile => graph.addWeightedNode(tile));
203
- graph.weightedNodeWeight = 3; // 沼泽地移动慢
204
- const path = graph.searchPath(playerPos, targetPos);
205
- ```
206
-
207
- ### 迷宫游戏
208
-
209
- ```typescript
210
- // 简单迷宫寻路
211
- const graph = new UnweightedGridGraph(mazeWidth, mazeHeight);
212
- walls.forEach(wall => graph.walls.push(wall));
213
- const path = graph.searchPath(startPos, exitPos);
214
- ```
215
-
216
- ## 文件结构
217
-
218
- ```
219
- bin/
220
- ├── pathfinding.js # 完整版本
221
- ├── pathfinding.min.js # 完整版本(压缩)
222
- ├── pathfinding.d.ts # TypeScript类型定义
223
- └── modules/ # 分模块版本
224
- ├── astar.js # A*算法模块
225
- ├── astar.min.js # A*算法模块(压缩)
226
- ├── breadth-first.js # 广度优先模块
227
- └── breadth-first.min.js # 广度优先模块(压缩)
228
- ```
229
-
230
- ## 开发
231
-
232
- ```bash
233
- # 安装依赖
234
- npm install
235
-
236
- # 构建
237
- npm run build
238
-
239
- # 清理构建目录
240
- gulp clean
241
- ```
242
-
243
- ## 许可证
244
-
245
- MIT License
@@ -1 +0,0 @@
1
- export{Vector2Utils}from"./Types/IVector2";export{PriorityQueue}from"./Utils/PriorityQueue";export{AStarPathfinder}from"./AI/Pathfinding/AStar/AStarPathfinder";export{AstarGridGraph}from"./AI/Pathfinding/AStar/AstarGridGraph";export{BreadthFirstPathfinder}from"./AI/Pathfinding/BreadthFirst/BreadthFirstPathfinder";export{UnweightedGraph}from"./AI/Pathfinding/BreadthFirst/UnweightedGraph";export{UnweightedGridGraph}from"./AI/Pathfinding/BreadthFirst/UnweightedGridGraph";var t=function(){function t(){}return t.equals=function(t,e){return t.equals?t.equals(e):t.x===e.x&&t.y===e.y},t.create=function(t,e){return{x:t,y:e}},t.clone=function(t){return{x:t.x,y:t.y}},t.add=function(t,e){return{x:t.x+e.x,y:t.y+e.y}},t.manhattanDistance=function(t,e){return Math.abs(t.x-e.x)+Math.abs(t.y-e.y)},t.distance=function(t,e){var i=t.x-e.x,r=t.y-e.y;return Math.sqrt(i*i+r*r)},t.toHash=function(t){return(t.x+this.MAX_COORD|0)<<16|(t.y+this.MAX_COORD|0)},t.toKey=function(t){return"".concat(t.x,",").concat(t.y)},t.fromHash=function(t){return{x:(t>>16)-this.MAX_COORD,y:(65535&t)-this.MAX_COORD}},t.HASH_MULTIPLIER=73856093,t.MAX_COORD=32767,t}();export{t as Vector2Utils};var e=function(){function t(){this._heap=[],this._size=0}return Object.defineProperty(t.prototype,"size",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isEmpty",{get:function(){return 0===this._size},enumerable:!1,configurable:!0}),t.prototype.clear=function(){this._heap.length=0,this._size=0},t.prototype.enqueue=function(t){this._heap[this._size]=t,this._bubbleUp(this._size),this._size++},t.prototype.dequeue=function(){if(0!==this._size){var t=this._heap[0];return this._size--,this._size>0&&(this._heap[0]=this._heap[this._size],this._bubbleDown(0)),t}},t.prototype.peek=function(){return this._size>0?this._heap[0]:void 0},t.prototype._bubbleUp=function(t){for(;t>0;){var e=Math.floor((t-1)/2);if(this._heap[t].priority>=this._heap[e].priority)break;this._swap(t,e),t=e}},t.prototype._bubbleDown=function(t){for(;;){var e=t,i=2*t+1,r=2*t+2;if(i<this._size&&this._heap[i].priority<this._heap[e].priority&&(e=i),r<this._size&&this._heap[r].priority<this._heap[e].priority&&(e=r),e===t)break;this._swap(t,e),t=e}},t.prototype._swap=function(t,e){var i=this._heap[t];this._heap[t]=this._heap[e],this._heap[e]=i},t}();export{e as PriorityQueue};var i=this&&this.__spreadArray||function(t,e,i){if(i||2===arguments.length)for(var r,s=0,o=e.length;s<o;s++)!r&&s in e||(r||(r=Array.prototype.slice.call(e,0,s)),r[s]=e[s]);return t.concat(r||Array.prototype.slice.call(e))};import{Vector2Utils as t}from"../../../Types/IVector2";import{PriorityQueue as e}from"../../../Utils/PriorityQueue";var r=function(){function e(e,i,r,s){void 0===i&&(i=0),void 0===r&&(r=0),void 0===s&&(s=null),this.priority=0,this.gCost=0,this.hCost=0,this.parent=null,this.hash=0,this.node=e,this.gCost=i,this.hCost=r,this.priority=i+r,this.parent=s,this.hash=t.toHash(e)}return e.prototype.updateCosts=function(t,e,i){void 0===i&&(i=null),this.gCost=t,this.hCost=e,this.priority=t+e,this.parent=i},e.prototype.updateNode=function(e,i,r,s){void 0===i&&(i=0),void 0===r&&(r=0),void 0===s&&(s=null),this.node=e,this.gCost=i,this.hCost=r,this.priority=i+r,this.parent=s,this.hash=t.toHash(e)},e.prototype.reset=function(){this.node=null,this.priority=0,this.gCost=0,this.hCost=0,this.parent=null,this.hash=0},e}(),s=function(){function s(){}return s._getNode=function(t,e,i,s){void 0===e&&(e=0),void 0===i&&(i=0),void 0===s&&(s=null);var o=this._nodePool.pop();return o?o.updateNode(t,e,i,s):o=new r(t,e,i,s),o},s._recycleNode=function(t){this._nodePool.length<1e3&&(t.reset(),this._nodePool.push(t))},s.search=function(i,r,s){var o=new e,h=new Set,n=new Map,a=t.toHash(r),u=t.toHash(s);if(a===u)return{found:!0,goalNode:this._getNode(r,0,0)};var d,p=this._getNode(r,0,i.heuristic(r,s));for(o.enqueue(p),n.set(a,p);!o.isEmpty;){var c=o.dequeue(),l=c.hash;if(n.delete(l),l===u){d=c;break}h.add(l);for(var f=0,_=i.getNeighbors(c.node);f<_.length;f++){var g=_[f],y=t.toHash(g);if(!h.has(y)){var w=c.gCost+i.cost(c.node,g),N=n.get(y);if(N){if(w<N.gCost){var v=N.hCost;N.updateCosts(w,v,c)}}else{v=i.heuristic(g,s);var P=this._getNode(g,w,v,c);o.enqueue(P),n.set(y,P)}}}c!==d&&this._recycleNode(c)}for(;!o.isEmpty;){var S=o.dequeue();S!==d&&this._recycleNode(S)}return{found:!!d,goalNode:d}},s.searchPath=function(t,e,i){var r=this.search(t,e,i);return r.found&&r.goalNode?this.reconstructPathFromNode(r.goalNode,e):[]},s.reconstructPathFromNode=function(e,r){this._tempPath.length=0;for(var s=e,o=t.toHash(r);s;){this._tempPath.unshift(s.node);var h=s.hash,n=s.parent;h!==o&&this._recycleNode(s),s=n}return i([],this._tempPath,!0)},s.hasPath=function(t,e,i){var r=this.search(t,e,i);return r.goalNode&&this._recycleNode(r.goalNode),r.found},s.clearPool=function(){this._nodePool.length=0,this._tempPath.length=0},s.getPoolStats=function(){return{poolSize:this._nodePool.length,maxPoolSize:1e3}},s._nodePool=[],s._tempPath=[],s}();export{s as AStarPathfinder};import{Vector2Utils as t}from"../../../Types/IVector2";import{AStarPathfinder as s}from"./AStarPathfinder";var o=function(){function e(e,i){this.dirs=[t.create(1,0),t.create(0,-1),t.create(-1,0),t.create(0,1)],this.walls=[],this.weightedNodes=[],this.defaultWeight=1,this.weightedNodeWeight=5,this._neighbors=new Array(4),this._wallsSet=new Set,this._weightedNodesSet=new Set,this._wallsDirty=!0,this._weightedNodesDirty=!0,this._width=e,this._height=i}return e.prototype.addWall=function(t){this.walls.push(t),this._wallsDirty=!0},e.prototype.addWalls=function(t){var e;(e=this.walls).push.apply(e,t),this._wallsDirty=!0},e.prototype.clearWalls=function(){this.walls.length=0,this._wallsSet.clear(),this._wallsDirty=!1},e.prototype.addWeightedNode=function(t){this.weightedNodes.push(t),this._weightedNodesDirty=!0},e.prototype.addWeightedNodes=function(t){var e;(e=this.weightedNodes).push.apply(e,t),this._weightedNodesDirty=!0},e.prototype.clearWeightedNodes=function(){this.weightedNodes.length=0,this._weightedNodesSet.clear(),this._weightedNodesDirty=!1},e.prototype._updateHashSets=function(){if(this._wallsDirty){this._wallsSet.clear();for(var e=0,i=this.walls;e<i.length;e++){var r=i[e];this._wallsSet.add(t.toHash(r))}this._wallsDirty=!1}if(this._weightedNodesDirty){this._weightedNodesSet.clear();for(var s=0,o=this.weightedNodes;s<o.length;s++){var h=o[s];this._weightedNodesSet.add(t.toHash(h))}this._weightedNodesDirty=!1}},e.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x<this._width&&0<=t.y&&t.y<this._height},e.prototype.isNodePassable=function(e){return this._updateHashSets(),!this._wallsSet.has(t.toHash(e))},e.prototype.search=function(t,e){return s.hasPath(this,t,e)},e.prototype.searchPath=function(t,e){return s.searchPath(this,t,e)},e.prototype.getNeighbors=function(e){this._neighbors.length=0;for(var i=0,r=this.dirs;i<r.length;i++){var s=r[i],o=t.add(e,s);this.isNodeInBounds(o)&&this.isNodePassable(o)&&this._neighbors.push(o)}return this._neighbors},e.prototype.cost=function(e,i){return this._updateHashSets(),this._weightedNodesSet.has(t.toHash(i))?this.weightedNodeWeight:this.defaultWeight},e.prototype.heuristic=function(e,i){return t.manhattanDistance(e,i)},e.prototype.getStats=function(){return this._updateHashSets(),{walls:this.walls.length,weightedNodes:this.weightedNodes.length,gridSize:"".concat(this._width,"x").concat(this._height),wallsSetSize:this._wallsSet.size,weightedNodesSetSize:this._weightedNodesSet.size}},e}();export{o as AstarGridGraph};export{};import{Vector2Utils as t}from"../../../Types/IVector2";var h=function(){function e(){}return e.search=function(e,i,r,s){var o=[],h=new Set,n=s||new Map,a=t.toHash(i),u=t.toHash(r);if(a===u)return!0;for(o.push(i),h.add(a);o.length>0;){var d=o.shift();if(t.toHash(d)===u)return!0;for(var p=0,c=e.getNeighbors(d);p<c.length;p++){var l=c[p],f=t.toHash(l);h.has(f)||(h.add(f),n.set(f,d),o.push(l))}}return!1},e.searchPath=function(t,e,i){var r=new Map;return this.search(t,e,i,r)?this.reconstructPath(r,e,i):[]},e.reconstructPath=function(e,i,r){for(var s=[],o=r,h=t.toHash(i);t.toHash(o)!==h;){s.unshift(o);var n=t.toHash(o),a=e.get(n);if(!a)break;o=a}return s.unshift(i),s},e}();export{h as BreadthFirstPathfinder};export{};var n=function(){function t(){this.edges=new Map}return t.prototype.addEdgesForNode=function(t,e){return this.edges.set(t,e),this},t.prototype.getNeighbors=function(t){return this.edges.get(t)||[]},t}();export{n as UnweightedGraph};import{Vector2Utils as t}from"../../../Types/IVector2";import{BreadthFirstPathfinder as h}from"./BreadthFirstPathfinder";var a=function(){function e(t,i,r){void 0===r&&(r=!1),this.walls=[],this._neighbors=[],this._width=t,this._height=i,this._dirs=r?e.COMPASS_DIRS:e.CARDINAL_DIRS}return e.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x<this._width&&0<=t.y&&t.y<this._height},e.prototype.isNodePassable=function(e){return!this.walls.find((function(i){return t.equals(i,e)}))},e.prototype.getNeighbors=function(e){this._neighbors.length=0;for(var i=0,r=this._dirs;i<r.length;i++){var s=r[i],o=t.add(e,s);this.isNodeInBounds(o)&&this.isNodePassable(o)&&this._neighbors.push(o)}return this._neighbors},e.prototype.searchPath=function(t,e){return h.searchPath(this,t,e)},e.prototype.hasPath=function(t,e){return h.search(this,t,e)},e.CARDINAL_DIRS=[t.create(1,0),t.create(0,-1),t.create(-1,0),t.create(0,1)],e.COMPASS_DIRS=[t.create(1,0),t.create(1,-1),t.create(0,-1),t.create(-1,-1),t.create(-1,0),t.create(-1,1),t.create(0,1),t.create(1,1)],e}();export{a as UnweightedGridGraph};
@@ -1 +0,0 @@
1
- export{Vector2Utils}from"./Types/IVector2";export{PriorityQueue}from"./Utils/PriorityQueue";export{AStarPathfinder}from"./AI/Pathfinding/AStar/AStarPathfinder";export{AstarGridGraph}from"./AI/Pathfinding/AStar/AstarGridGraph";export{BreadthFirstPathfinder}from"./AI/Pathfinding/BreadthFirst/BreadthFirstPathfinder";export{UnweightedGraph}from"./AI/Pathfinding/BreadthFirst/UnweightedGraph";export{UnweightedGridGraph}from"./AI/Pathfinding/BreadthFirst/UnweightedGridGraph";var t=function(){function t(){}return t.equals=function(t,e){return t.equals?t.equals(e):t.x===e.x&&t.y===e.y},t.create=function(t,e){return{x:t,y:e}},t.clone=function(t){return{x:t.x,y:t.y}},t.add=function(t,e){return{x:t.x+e.x,y:t.y+e.y}},t.manhattanDistance=function(t,e){return Math.abs(t.x-e.x)+Math.abs(t.y-e.y)},t.distance=function(t,e){var i=t.x-e.x,r=t.y-e.y;return Math.sqrt(i*i+r*r)},t.toHash=function(t){return(t.x+this.MAX_COORD|0)<<16|(t.y+this.MAX_COORD|0)},t.toKey=function(t){return"".concat(t.x,",").concat(t.y)},t.fromHash=function(t){return{x:(t>>16)-this.MAX_COORD,y:(65535&t)-this.MAX_COORD}},t.HASH_MULTIPLIER=73856093,t.MAX_COORD=32767,t}();export{t as Vector2Utils};var e=function(){function t(){this._heap=[],this._size=0}return Object.defineProperty(t.prototype,"size",{get:function(){return this._size},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isEmpty",{get:function(){return 0===this._size},enumerable:!1,configurable:!0}),t.prototype.clear=function(){this._heap.length=0,this._size=0},t.prototype.enqueue=function(t){this._heap[this._size]=t,this._bubbleUp(this._size),this._size++},t.prototype.dequeue=function(){if(0!==this._size){var t=this._heap[0];return this._size--,this._size>0&&(this._heap[0]=this._heap[this._size],this._bubbleDown(0)),t}},t.prototype.peek=function(){return this._size>0?this._heap[0]:void 0},t.prototype._bubbleUp=function(t){for(;t>0;){var e=Math.floor((t-1)/2);if(this._heap[t].priority>=this._heap[e].priority)break;this._swap(t,e),t=e}},t.prototype._bubbleDown=function(t){for(;;){var e=t,i=2*t+1,r=2*t+2;if(i<this._size&&this._heap[i].priority<this._heap[e].priority&&(e=i),r<this._size&&this._heap[r].priority<this._heap[e].priority&&(e=r),e===t)break;this._swap(t,e),t=e}},t.prototype._swap=function(t,e){var i=this._heap[t];this._heap[t]=this._heap[e],this._heap[e]=i},t}();export{e as PriorityQueue};var i=this&&this.__spreadArray||function(t,e,i){if(i||2===arguments.length)for(var r,s=0,o=e.length;s<o;s++)!r&&s in e||(r||(r=Array.prototype.slice.call(e,0,s)),r[s]=e[s]);return t.concat(r||Array.prototype.slice.call(e))};import{Vector2Utils as t}from"../../../Types/IVector2";import{PriorityQueue as e}from"../../../Utils/PriorityQueue";var r=function(){function e(e,i,r,s){void 0===i&&(i=0),void 0===r&&(r=0),void 0===s&&(s=null),this.priority=0,this.gCost=0,this.hCost=0,this.parent=null,this.hash=0,this.node=e,this.gCost=i,this.hCost=r,this.priority=i+r,this.parent=s,this.hash=t.toHash(e)}return e.prototype.updateCosts=function(t,e,i){void 0===i&&(i=null),this.gCost=t,this.hCost=e,this.priority=t+e,this.parent=i},e.prototype.updateNode=function(e,i,r,s){void 0===i&&(i=0),void 0===r&&(r=0),void 0===s&&(s=null),this.node=e,this.gCost=i,this.hCost=r,this.priority=i+r,this.parent=s,this.hash=t.toHash(e)},e.prototype.reset=function(){this.node=null,this.priority=0,this.gCost=0,this.hCost=0,this.parent=null,this.hash=0},e}(),s=function(){function s(){}return s._getNode=function(t,e,i,s){void 0===e&&(e=0),void 0===i&&(i=0),void 0===s&&(s=null);var o=this._nodePool.pop();return o?o.updateNode(t,e,i,s):o=new r(t,e,i,s),o},s._recycleNode=function(t){this._nodePool.length<1e3&&(t.reset(),this._nodePool.push(t))},s.search=function(i,r,s){var o=new e,h=new Set,n=new Map,a=t.toHash(r),u=t.toHash(s);if(a===u)return{found:!0,goalNode:this._getNode(r,0,0)};var d,p=this._getNode(r,0,i.heuristic(r,s));for(o.enqueue(p),n.set(a,p);!o.isEmpty;){var c=o.dequeue(),l=c.hash;if(n.delete(l),l===u){d=c;break}h.add(l);for(var f=0,_=i.getNeighbors(c.node);f<_.length;f++){var g=_[f],y=t.toHash(g);if(!h.has(y)){var w=c.gCost+i.cost(c.node,g),N=n.get(y);if(N){if(w<N.gCost){var v=N.hCost;N.updateCosts(w,v,c)}}else{v=i.heuristic(g,s);var P=this._getNode(g,w,v,c);o.enqueue(P),n.set(y,P)}}}c!==d&&this._recycleNode(c)}for(;!o.isEmpty;){var S=o.dequeue();S!==d&&this._recycleNode(S)}return{found:!!d,goalNode:d}},s.searchPath=function(t,e,i){var r=this.search(t,e,i);return r.found&&r.goalNode?this.reconstructPathFromNode(r.goalNode,e):[]},s.reconstructPathFromNode=function(e,r){this._tempPath.length=0;for(var s=e,o=t.toHash(r);s;){this._tempPath.unshift(s.node);var h=s.hash,n=s.parent;h!==o&&this._recycleNode(s),s=n}return i([],this._tempPath,!0)},s.hasPath=function(t,e,i){var r=this.search(t,e,i);return r.goalNode&&this._recycleNode(r.goalNode),r.found},s.clearPool=function(){this._nodePool.length=0,this._tempPath.length=0},s.getPoolStats=function(){return{poolSize:this._nodePool.length,maxPoolSize:1e3}},s._nodePool=[],s._tempPath=[],s}();export{s as AStarPathfinder};import{Vector2Utils as t}from"../../../Types/IVector2";import{AStarPathfinder as s}from"./AStarPathfinder";var o=function(){function e(e,i){this.dirs=[t.create(1,0),t.create(0,-1),t.create(-1,0),t.create(0,1)],this.walls=[],this.weightedNodes=[],this.defaultWeight=1,this.weightedNodeWeight=5,this._neighbors=new Array(4),this._wallsSet=new Set,this._weightedNodesSet=new Set,this._wallsDirty=!0,this._weightedNodesDirty=!0,this._width=e,this._height=i}return e.prototype.addWall=function(t){this.walls.push(t),this._wallsDirty=!0},e.prototype.addWalls=function(t){var e;(e=this.walls).push.apply(e,t),this._wallsDirty=!0},e.prototype.clearWalls=function(){this.walls.length=0,this._wallsSet.clear(),this._wallsDirty=!1},e.prototype.addWeightedNode=function(t){this.weightedNodes.push(t),this._weightedNodesDirty=!0},e.prototype.addWeightedNodes=function(t){var e;(e=this.weightedNodes).push.apply(e,t),this._weightedNodesDirty=!0},e.prototype.clearWeightedNodes=function(){this.weightedNodes.length=0,this._weightedNodesSet.clear(),this._weightedNodesDirty=!1},e.prototype._updateHashSets=function(){if(this._wallsDirty){this._wallsSet.clear();for(var e=0,i=this.walls;e<i.length;e++){var r=i[e];this._wallsSet.add(t.toHash(r))}this._wallsDirty=!1}if(this._weightedNodesDirty){this._weightedNodesSet.clear();for(var s=0,o=this.weightedNodes;s<o.length;s++){var h=o[s];this._weightedNodesSet.add(t.toHash(h))}this._weightedNodesDirty=!1}},e.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x<this._width&&0<=t.y&&t.y<this._height},e.prototype.isNodePassable=function(e){return this._updateHashSets(),!this._wallsSet.has(t.toHash(e))},e.prototype.search=function(t,e){return s.hasPath(this,t,e)},e.prototype.searchPath=function(t,e){return s.searchPath(this,t,e)},e.prototype.getNeighbors=function(e){this._neighbors.length=0;for(var i=0,r=this.dirs;i<r.length;i++){var s=r[i],o=t.add(e,s);this.isNodeInBounds(o)&&this.isNodePassable(o)&&this._neighbors.push(o)}return this._neighbors},e.prototype.cost=function(e,i){return this._updateHashSets(),this._weightedNodesSet.has(t.toHash(i))?this.weightedNodeWeight:this.defaultWeight},e.prototype.heuristic=function(e,i){return t.manhattanDistance(e,i)},e.prototype.getStats=function(){return this._updateHashSets(),{walls:this.walls.length,weightedNodes:this.weightedNodes.length,gridSize:"".concat(this._width,"x").concat(this._height),wallsSetSize:this._wallsSet.size,weightedNodesSetSize:this._weightedNodesSet.size}},e}();export{o as AstarGridGraph};export{};import{Vector2Utils as t}from"../../../Types/IVector2";var h=function(){function e(){}return e.search=function(e,i,r,s){var o=[],h=new Set,n=s||new Map,a=t.toHash(i),u=t.toHash(r);if(a===u)return!0;for(o.push(i),h.add(a);o.length>0;){var d=o.shift();if(t.toHash(d)===u)return!0;for(var p=0,c=e.getNeighbors(d);p<c.length;p++){var l=c[p],f=t.toHash(l);h.has(f)||(h.add(f),n.set(f,d),o.push(l))}}return!1},e.searchPath=function(t,e,i){var r=new Map;return this.search(t,e,i,r)?this.reconstructPath(r,e,i):[]},e.reconstructPath=function(e,i,r){for(var s=[],o=r,h=t.toHash(i);t.toHash(o)!==h;){s.unshift(o);var n=t.toHash(o),a=e.get(n);if(!a)break;o=a}return s.unshift(i),s},e}();export{h as BreadthFirstPathfinder};export{};var n=function(){function t(){this.edges=new Map}return t.prototype.addEdgesForNode=function(t,e){return this.edges.set(t,e),this},t.prototype.getNeighbors=function(t){return this.edges.get(t)||[]},t}();export{n as UnweightedGraph};import{Vector2Utils as t}from"../../../Types/IVector2";import{BreadthFirstPathfinder as h}from"./BreadthFirstPathfinder";var a=function(){function e(t,i,r){void 0===r&&(r=!1),this.walls=[],this._neighbors=[],this._width=t,this._height=i,this._dirs=r?e.COMPASS_DIRS:e.CARDINAL_DIRS}return e.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x<this._width&&0<=t.y&&t.y<this._height},e.prototype.isNodePassable=function(e){return!this.walls.find((function(i){return t.equals(i,e)}))},e.prototype.getNeighbors=function(e){this._neighbors.length=0;for(var i=0,r=this._dirs;i<r.length;i++){var s=r[i],o=t.add(e,s);this.isNodeInBounds(o)&&this.isNodePassable(o)&&this._neighbors.push(o)}return this._neighbors},e.prototype.searchPath=function(t,e){return h.searchPath(this,t,e)},e.prototype.hasPath=function(t,e){return h.search(this,t,e)},e.CARDINAL_DIRS=[t.create(1,0),t.create(0,-1),t.create(-1,0),t.create(0,1)],e.COMPASS_DIRS=[t.create(1,0),t.create(1,-1),t.create(0,-1),t.create(-1,-1),t.create(-1,0),t.create(-1,1),t.create(0,1),t.create(1,1)],e}();export{a as UnweightedGridGraph};
package/bin/package.json DELETED
@@ -1,54 +0,0 @@
1
- {
2
- "name": "@esengine/pathfinding",
3
- "version": "1.0.1",
4
- "description": "寻路算法库,支持A*、广度优先等算法,适用于Cocos Creator、Laya等游戏引擎",
5
- "main": "bin/index.js",
6
- "types": "bin/index.d.ts",
7
- "keywords": [
8
- "pathfinding",
9
- "astar",
10
- "a-star",
11
- "algorithm",
12
- "game",
13
- "cocos",
14
- "laya",
15
- "typescript",
16
- "navigation",
17
- "grid"
18
- ],
19
- "directories": {
20
- "lib": "lib"
21
- },
22
- "scripts": {
23
- "test": "mocha --recursive --reporter tap --growl",
24
- "eslint": "eslint src --ext .ts",
25
- "build": "gulp build",
26
- "dev": "gulp buildJs"
27
- },
28
- "author": "yhh",
29
- "license": "MIT",
30
- "devDependencies": {
31
- "@babel/core": "^7.23.0",
32
- "@babel/preset-env": "^7.23.0",
33
- "browserify": "^17.0.0",
34
- "del": "^8.0.0",
35
- "gulp": "^5.0.0",
36
- "gulp-babel": "^8.0.0",
37
- "gulp-concat": "^2.6.1",
38
- "gulp-inject-string": "^1.1.2",
39
- "gulp-string-replace": "^1.1.2",
40
- "gulp-terser": "^2.1.0",
41
- "gulp-typescript": "^6.0.0-alpha.1",
42
- "tsify": "^5.0.4",
43
- "typedoc": "^0.25.0",
44
- "typescript": "^4.9.5",
45
- "vinyl-source-stream": "^2.0.0",
46
- "watchify": "^4.0.0"
47
- },
48
- "publishConfig": {
49
- "registry": "https://npm.pkg.github.com/359807859@qq.com"
50
- },
51
- "dependencies": {
52
- "@esengine/ecs-framework": "^2.0.5"
53
- }
54
- }