@esengine/pathfinding 1.0.1

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/.idea/misc.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/source.iml" filepath="$PROJECT_DIR$/.idea/source.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,13 @@
1
+ {
2
+ // See https://go.microsoft.com/fwlink/?LinkId=733558
3
+ // for the documentation about the tasks.json format
4
+ "version": "2.0.0",
5
+ "tasks": [
6
+ {
7
+ "type": "gulp",
8
+ "task": "build",
9
+ "group": "build",
10
+ "problemMatcher": []
11
+ }
12
+ ]
13
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "typescript.tsdk": "./node_modules/typescript/lib"
3
+ }
package/README.md ADDED
@@ -0,0 +1,245 @@
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
package/bin/README.md ADDED
@@ -0,0 +1,245 @@
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