├── .gitignore ├── NewProject1.rar ├── README.md ├── README.md.bak ├── assets ├── Scene.meta ├── Scene │ ├── helloworld.fire │ └── helloworld.fire.meta ├── Script.meta ├── Script │ ├── AutoFindPath.js │ ├── AutoFindPath.js.meta │ ├── HelloWorld.js │ ├── HelloWorld.js.meta │ ├── canvasControl.js │ └── canvasControl.js.meta ├── Texture.meta ├── Texture │ ├── HelloWorld.png │ ├── HelloWorld.png.meta │ ├── gz.png │ ├── gz.png.meta │ ├── singleColor.png │ └── singleColor.png.meta ├── gz.prefab └── gz.prefab.meta ├── creator.d.ts ├── jsconfig.json ├── project.json ├── settings ├── builder.json ├── builder.panel.json └── project.json ├── template-banner.png └── template.json /.gitignore: -------------------------------------------------------------------------------- 1 | #///////////////////////////////////////////////////////////////////////////// 2 | # Fireball Projects 3 | #///////////////////////////////////////////////////////////////////////////// 4 | 5 | library/ 6 | temp/ 7 | local/ 8 | build/ 9 | 10 | #///////////////////////////////////////////////////////////////////////////// 11 | # Logs and databases 12 | #///////////////////////////////////////////////////////////////////////////// 13 | 14 | *.log 15 | *.sql 16 | *.sqlite 17 | 18 | #///////////////////////////////////////////////////////////////////////////// 19 | # files for debugger 20 | #///////////////////////////////////////////////////////////////////////////// 21 | 22 | *.sln 23 | *.csproj 24 | *.pidb 25 | *.unityproj 26 | *.suo 27 | 28 | #///////////////////////////////////////////////////////////////////////////// 29 | # OS generated files 30 | #///////////////////////////////////////////////////////////////////////////// 31 | 32 | .DS_Store 33 | ehthumbs.db 34 | Thumbs.db 35 | 36 | #///////////////////////////////////////////////////////////////////////////// 37 | # exvim files 38 | #///////////////////////////////////////////////////////////////////////////// 39 | 40 | *UnityVS.meta 41 | *.err 42 | *.err.meta 43 | *.exvim 44 | *.exvim.meta 45 | *.vimentry 46 | *.vimentry.meta 47 | *.vimproject 48 | *.vimproject.meta 49 | .vimfiles.*/ 50 | .exvim.*/ 51 | quick_gen_project_*_autogen.bat 52 | quick_gen_project_*_autogen.bat.meta 53 | quick_gen_project_*_autogen.sh 54 | quick_gen_project_*_autogen.sh.meta 55 | .exvim.app 56 | 57 | #///////////////////////////////////////////////////////////////////////////// 58 | # webstorm files 59 | #///////////////////////////////////////////////////////////////////////////// 60 | 61 | .idea/ 62 | 63 | #////////////////////////// 64 | # VS Code 65 | #////////////////////////// 66 | 67 | .vscode/ -------------------------------------------------------------------------------- /NewProject1.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microcisco/astar/bb4a1c191618919e6633a54002442fd6fc869230/NewProject1.rar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # astar 2 | 3 | ## 最近更新 4 | + 新增了JPS(跳点寻路),欢迎大家提issue 5 | https://github.com/microcisco/jpsFindPath 6 | + 新增了TS版本,进一步的优化代码,以及进一步的简化用法,欢迎大家提issue 7 | https://github.com/microcisco/astartForTS 8 | --- 9 | 10 | 商业项目放心使用,有问题会及时更新! 11 | 最近要用到寻路算法,偷懒网上搜了一批,尼玛惨不忍睹,各种bug,没办法,自己动手丰衣足食。 12 | 本版本优化了两个地方,第一个是将经典的递归改成了循环避免了堆栈溢出,第二个是优化了路径尽量走直线。 13 | 14 | 关于用法 15 | 工程项目是cocos create的,其他项目直接调用 /assets/Script/AutoFindPath.js下的getPath即可参数和返回值如下, 另外点对象的结构如下(x: 0, y: 0) 16 | /** 17 | * 18 | * @param {object} bron 出生点 19 | * @param {object} obstacles 障碍物数组 20 | * @param {object} target 目标 21 | * @param {object} map 地图 22 | * @param {object} girdLength 格子长度 23 | * @return{Array} route 路径点数组(空数组为死路) 24 | * */ 25 | 26 | 最后我的注释写的非常详细,如果大家发现bug欢迎提issues 27 | 28 | -------------------------------------------------------------------------------- /README.md.bak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microcisco/astar/bb4a1c191618919e6633a54002442fd6fc869230/README.md.bak -------------------------------------------------------------------------------- /assets/Scene.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.1", 3 | "uuid": "29f52784-2fca-467b-92e7-8fd9ef8c57b7", 4 | "isGroup": false, 5 | "subMetas": {} 6 | } -------------------------------------------------------------------------------- /assets/Scene/helloworld.fire: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "__type__": "cc.SceneAsset", 4 | "_name": "", 5 | "_objFlags": 0, 6 | "_rawFiles": null, 7 | "scene": { 8 | "__id__": 1 9 | } 10 | }, 11 | { 12 | "__type__": "cc.Scene", 13 | "_objFlags": 0, 14 | "_parent": null, 15 | "_children": [ 16 | { 17 | "__id__": 2 18 | } 19 | ], 20 | "_tag": -1, 21 | "_active": true, 22 | "_components": [], 23 | "_prefab": null, 24 | "_id": "2d2f792f-a40c-49bb-a189-ed176a246e49", 25 | "_opacity": 255, 26 | "_color": { 27 | "__type__": "cc.Color", 28 | "r": 255, 29 | "g": 255, 30 | "b": 255, 31 | "a": 255 32 | }, 33 | "_cascadeOpacityEnabled": true, 34 | "_anchorPoint": { 35 | "__type__": "cc.Vec2", 36 | "x": 0, 37 | "y": 0 38 | }, 39 | "_contentSize": { 40 | "__type__": "cc.Size", 41 | "width": 0, 42 | "height": 0 43 | }, 44 | "_localZOrder": 0, 45 | "_globalZOrder": 0, 46 | "_opacityModifyRGB": false, 47 | "groupIndex": 0, 48 | "autoReleaseAssets": false 49 | }, 50 | { 51 | "__type__": "cc.Node", 52 | "_name": "Canvas", 53 | "_objFlags": 0, 54 | "_parent": { 55 | "__id__": 1 56 | }, 57 | "_children": [ 58 | { 59 | "__id__": 3 60 | }, 61 | { 62 | "__id__": 6 63 | }, 64 | { 65 | "__id__": 8 66 | } 67 | ], 68 | "_tag": -1, 69 | "_active": true, 70 | "_components": [ 71 | { 72 | "__id__": 29 73 | }, 74 | { 75 | "__id__": 30 76 | } 77 | ], 78 | "_prefab": null, 79 | "_id": "a286bbGknJLZpRpxROV6M94", 80 | "_opacity": 255, 81 | "_color": { 82 | "__type__": "cc.Color", 83 | "r": 252, 84 | "g": 252, 85 | "b": 252, 86 | "a": 255 87 | }, 88 | "_cascadeOpacityEnabled": true, 89 | "_anchorPoint": { 90 | "__type__": "cc.Vec2", 91 | "x": 0.5, 92 | "y": 0.5 93 | }, 94 | "_contentSize": { 95 | "__type__": "cc.Size", 96 | "width": 960, 97 | "height": 640 98 | }, 99 | "_rotationX": 0, 100 | "_rotationY": 0, 101 | "_scaleX": 1, 102 | "_scaleY": 1, 103 | "_position": { 104 | "__type__": "cc.Vec2", 105 | "x": 480, 106 | "y": 320 107 | }, 108 | "_skewX": 0, 109 | "_skewY": 0, 110 | "_localZOrder": 0, 111 | "_globalZOrder": 0, 112 | "_opacityModifyRGB": false, 113 | "groupIndex": 0 114 | }, 115 | { 116 | "__type__": "cc.Node", 117 | "_name": "background", 118 | "_objFlags": 0, 119 | "_parent": { 120 | "__id__": 2 121 | }, 122 | "_children": [], 123 | "_tag": -1, 124 | "_active": true, 125 | "_components": [ 126 | { 127 | "__id__": 4 128 | }, 129 | { 130 | "__id__": 5 131 | } 132 | ], 133 | "_prefab": null, 134 | "_id": "e2e0crkOLxGrpMxpbC4iQg1", 135 | "_opacity": 255, 136 | "_color": { 137 | "__type__": "cc.Color", 138 | "r": 34, 139 | "g": 24, 140 | "b": 16, 141 | "a": 255 142 | }, 143 | "_cascadeOpacityEnabled": true, 144 | "_anchorPoint": { 145 | "__type__": "cc.Vec2", 146 | "x": 0.5, 147 | "y": 0.5 148 | }, 149 | "_contentSize": { 150 | "__type__": "cc.Size", 151 | "width": 960, 152 | "height": 640 153 | }, 154 | "_rotationX": 0, 155 | "_rotationY": 0, 156 | "_scaleX": 1, 157 | "_scaleY": 1, 158 | "_position": { 159 | "__type__": "cc.Vec2", 160 | "x": 0, 161 | "y": 0 162 | }, 163 | "_skewX": 0, 164 | "_skewY": 0, 165 | "_localZOrder": 0, 166 | "_globalZOrder": 0, 167 | "_opacityModifyRGB": false, 168 | "groupIndex": 0 169 | }, 170 | { 171 | "__type__": "cc.Widget", 172 | "_name": "", 173 | "_objFlags": 0, 174 | "node": { 175 | "__id__": 3 176 | }, 177 | "_enabled": true, 178 | "isAlignOnce": true, 179 | "_target": null, 180 | "_alignFlags": 45, 181 | "_left": 0, 182 | "_right": 0, 183 | "_top": 0, 184 | "_bottom": 0, 185 | "_verticalCenter": 0, 186 | "_horizontalCenter": 0, 187 | "_isAbsLeft": true, 188 | "_isAbsRight": true, 189 | "_isAbsTop": true, 190 | "_isAbsBottom": true, 191 | "_isAbsHorizontalCenter": true, 192 | "_isAbsVerticalCenter": true, 193 | "_originalWidth": 200, 194 | "_originalHeight": 150 195 | }, 196 | { 197 | "__type__": "cc.Sprite", 198 | "_name": "", 199 | "_objFlags": 0, 200 | "node": { 201 | "__id__": 3 202 | }, 203 | "_enabled": true, 204 | "_spriteFrame": { 205 | "__uuid__": "410fb916-8721-4663-bab8-34397391ace7" 206 | }, 207 | "_type": 1, 208 | "_sizeMode": 0, 209 | "_fillType": 0, 210 | "_fillCenter": { 211 | "__type__": "cc.Vec2", 212 | "x": 0, 213 | "y": 0 214 | }, 215 | "_fillStart": 0, 216 | "_fillRange": 0, 217 | "_isTrimmedMode": true, 218 | "_srcBlendFactor": 770, 219 | "_dstBlendFactor": 771, 220 | "_atlas": null 221 | }, 222 | { 223 | "__type__": "cc.Node", 224 | "_name": "map", 225 | "_objFlags": 0, 226 | "_parent": { 227 | "__id__": 2 228 | }, 229 | "_children": [], 230 | "_tag": -1, 231 | "_active": true, 232 | "_components": [ 233 | { 234 | "__id__": 7 235 | } 236 | ], 237 | "_prefab": null, 238 | "_id": "81334kUr2RKTKqT7OQNtApg", 239 | "_opacity": 255, 240 | "_color": { 241 | "__type__": "cc.Color", 242 | "r": 255, 243 | "g": 255, 244 | "b": 255, 245 | "a": 255 246 | }, 247 | "_cascadeOpacityEnabled": true, 248 | "_anchorPoint": { 249 | "__type__": "cc.Vec2", 250 | "x": 0, 251 | "y": 0 252 | }, 253 | "_contentSize": { 254 | "__type__": "cc.Size", 255 | "width": 0, 256 | "height": 0 257 | }, 258 | "_rotationX": 0, 259 | "_rotationY": 0, 260 | "_scaleX": 1, 261 | "_scaleY": 1, 262 | "_position": { 263 | "__type__": "cc.Vec2", 264 | "x": -480, 265 | "y": -320 266 | }, 267 | "_skewX": 0, 268 | "_skewY": 0, 269 | "_localZOrder": 0, 270 | "_globalZOrder": 0, 271 | "_opacityModifyRGB": false, 272 | "groupIndex": 0 273 | }, 274 | { 275 | "__type__": "280c3rsZJJKnZ9RqbALVwtK", 276 | "_name": "", 277 | "_objFlags": 0, 278 | "node": { 279 | "__id__": 6 280 | }, 281 | "_enabled": true, 282 | "gird": { 283 | "__uuid__": "0b857c5c-70e4-49ce-aaa8-17282b732a99" 284 | }, 285 | "canv": { 286 | "__id__": 2 287 | } 288 | }, 289 | { 290 | "__type__": "cc.Node", 291 | "_name": "control", 292 | "_objFlags": 0, 293 | "_parent": { 294 | "__id__": 2 295 | }, 296 | "_children": [ 297 | { 298 | "__id__": 9 299 | }, 300 | { 301 | "__id__": 14 302 | }, 303 | { 304 | "__id__": 19 305 | }, 306 | { 307 | "__id__": 24 308 | } 309 | ], 310 | "_tag": -1, 311 | "_active": true, 312 | "_components": [], 313 | "_prefab": null, 314 | "_id": "368cb5CZ0dMHIps67HNWfjK", 315 | "_opacity": 255, 316 | "_color": { 317 | "__type__": "cc.Color", 318 | "r": 255, 319 | "g": 255, 320 | "b": 255, 321 | "a": 255 322 | }, 323 | "_cascadeOpacityEnabled": true, 324 | "_anchorPoint": { 325 | "__type__": "cc.Vec2", 326 | "x": 0.5, 327 | "y": 0.5 328 | }, 329 | "_contentSize": { 330 | "__type__": "cc.Size", 331 | "width": 0, 332 | "height": 0 333 | }, 334 | "_rotationX": 0, 335 | "_rotationY": 0, 336 | "_scaleX": 1, 337 | "_scaleY": 1, 338 | "_position": { 339 | "__type__": "cc.Vec2", 340 | "x": 0, 341 | "y": 0 342 | }, 343 | "_skewX": 0, 344 | "_skewY": 0, 345 | "_localZOrder": 0, 346 | "_globalZOrder": 0, 347 | "_opacityModifyRGB": false, 348 | "groupIndex": 0 349 | }, 350 | { 351 | "__type__": "cc.Node", 352 | "_name": "bron", 353 | "_objFlags": 0, 354 | "_parent": { 355 | "__id__": 8 356 | }, 357 | "_children": [ 358 | { 359 | "__id__": 10 360 | } 361 | ], 362 | "_tag": -1, 363 | "_active": true, 364 | "_components": [ 365 | { 366 | "__id__": 12 367 | }, 368 | { 369 | "__id__": 13 370 | } 371 | ], 372 | "_prefab": null, 373 | "_id": "ae9ceu1rLtKO5RVDBqzlWIr", 374 | "_opacity": 255, 375 | "_color": { 376 | "__type__": "cc.Color", 377 | "r": 255, 378 | "g": 255, 379 | "b": 255, 380 | "a": 255 381 | }, 382 | "_cascadeOpacityEnabled": true, 383 | "_anchorPoint": { 384 | "__type__": "cc.Vec2", 385 | "x": 0.5, 386 | "y": 0.5 387 | }, 388 | "_contentSize": { 389 | "__type__": "cc.Size", 390 | "width": 100, 391 | "height": 40 392 | }, 393 | "_rotationX": 0, 394 | "_rotationY": 0, 395 | "_scaleX": 1, 396 | "_scaleY": 1, 397 | "_position": { 398 | "__type__": "cc.Vec2", 399 | "x": -355, 400 | "y": -239 401 | }, 402 | "_skewX": 0, 403 | "_skewY": 0, 404 | "_localZOrder": 0, 405 | "_globalZOrder": 0, 406 | "_opacityModifyRGB": false, 407 | "groupIndex": 0 408 | }, 409 | { 410 | "__type__": "cc.Node", 411 | "_name": "Label", 412 | "_objFlags": 0, 413 | "_parent": { 414 | "__id__": 9 415 | }, 416 | "_children": [], 417 | "_tag": -1, 418 | "_active": true, 419 | "_components": [ 420 | { 421 | "__id__": 11 422 | } 423 | ], 424 | "_prefab": null, 425 | "_id": "f9f9eJVwj9EhqBbThRyw7ZQ", 426 | "_opacity": 255, 427 | "_color": { 428 | "__type__": "cc.Color", 429 | "r": 0, 430 | "g": 0, 431 | "b": 0, 432 | "a": 255 433 | }, 434 | "_cascadeOpacityEnabled": true, 435 | "_anchorPoint": { 436 | "__type__": "cc.Vec2", 437 | "x": 0.5, 438 | "y": 0.5 439 | }, 440 | "_contentSize": { 441 | "__type__": "cc.Size", 442 | "width": 100, 443 | "height": 40 444 | }, 445 | "_rotationX": 0, 446 | "_rotationY": 0, 447 | "_scaleX": 1, 448 | "_scaleY": 1, 449 | "_position": { 450 | "__type__": "cc.Vec2", 451 | "x": 0, 452 | "y": 0 453 | }, 454 | "_skewX": 0, 455 | "_skewY": 0, 456 | "_localZOrder": 0, 457 | "_globalZOrder": 0, 458 | "_opacityModifyRGB": false, 459 | "groupIndex": 0 460 | }, 461 | { 462 | "__type__": "cc.Label", 463 | "_name": "", 464 | "_objFlags": 0, 465 | "node": { 466 | "__id__": 10 467 | }, 468 | "_enabled": true, 469 | "_useOriginalSize": false, 470 | "_actualFontSize": 20, 471 | "_fontSize": 20, 472 | "_lineHeight": 40, 473 | "_enableWrapText": false, 474 | "_N$file": null, 475 | "_isSystemFontUsed": true, 476 | "_spacingX": 0, 477 | "_N$string": "出生点", 478 | "_N$horizontalAlign": 1, 479 | "_N$verticalAlign": 1, 480 | "_N$fontFamily": "Arial", 481 | "_N$overflow": 1 482 | }, 483 | { 484 | "__type__": "cc.Sprite", 485 | "_name": "", 486 | "_objFlags": 0, 487 | "node": { 488 | "__id__": 9 489 | }, 490 | "_enabled": true, 491 | "_spriteFrame": { 492 | "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" 493 | }, 494 | "_type": 1, 495 | "_sizeMode": 0, 496 | "_fillType": 0, 497 | "_fillCenter": { 498 | "__type__": "cc.Vec2", 499 | "x": 0, 500 | "y": 0 501 | }, 502 | "_fillStart": 0, 503 | "_fillRange": 0, 504 | "_isTrimmedMode": true, 505 | "_srcBlendFactor": 770, 506 | "_dstBlendFactor": 771, 507 | "_atlas": null 508 | }, 509 | { 510 | "__type__": "cc.Button", 511 | "_name": "", 512 | "_objFlags": 0, 513 | "node": { 514 | "__id__": 9 515 | }, 516 | "_enabled": true, 517 | "transition": 2, 518 | "pressedColor": { 519 | "__type__": "cc.Color", 520 | "r": 255, 521 | "g": 255, 522 | "b": 255, 523 | "a": 255 524 | }, 525 | "hoverColor": { 526 | "__type__": "cc.Color", 527 | "r": 255, 528 | "g": 255, 529 | "b": 255, 530 | "a": 255 531 | }, 532 | "duration": 0.1, 533 | "zoomScale": 1.2, 534 | "pressedSprite": { 535 | "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" 536 | }, 537 | "hoverSprite": { 538 | "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" 539 | }, 540 | "clickEvents": [], 541 | "_N$interactable": true, 542 | "_N$enableAutoGrayEffect": false, 543 | "_N$normalColor": { 544 | "__type__": "cc.Color", 545 | "r": 255, 546 | "g": 255, 547 | "b": 255, 548 | "a": 255 549 | }, 550 | "_N$disabledColor": { 551 | "__type__": "cc.Color", 552 | "r": 255, 553 | "g": 255, 554 | "b": 255, 555 | "a": 255 556 | }, 557 | "_N$normalSprite": { 558 | "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" 559 | }, 560 | "_N$disabledSprite": { 561 | "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" 562 | }, 563 | "_N$target": { 564 | "__id__": 9 565 | } 566 | }, 567 | { 568 | "__type__": "cc.Node", 569 | "_name": "obstacles", 570 | "_objFlags": 0, 571 | "_parent": { 572 | "__id__": 8 573 | }, 574 | "_children": [ 575 | { 576 | "__id__": 15 577 | } 578 | ], 579 | "_tag": -1, 580 | "_active": true, 581 | "_components": [ 582 | { 583 | "__id__": 17 584 | }, 585 | { 586 | "__id__": 18 587 | } 588 | ], 589 | "_prefab": null, 590 | "_id": "7db52Q28tJCmqIbLtW1qT/H", 591 | "_opacity": 255, 592 | "_color": { 593 | "__type__": "cc.Color", 594 | "r": 255, 595 | "g": 255, 596 | "b": 255, 597 | "a": 255 598 | }, 599 | "_cascadeOpacityEnabled": true, 600 | "_anchorPoint": { 601 | "__type__": "cc.Vec2", 602 | "x": 0.5, 603 | "y": 0.5 604 | }, 605 | "_contentSize": { 606 | "__type__": "cc.Size", 607 | "width": 100, 608 | "height": 40 609 | }, 610 | "_rotationX": 0, 611 | "_rotationY": 0, 612 | "_scaleX": 1, 613 | "_scaleY": 1, 614 | "_position": { 615 | "__type__": "cc.Vec2", 616 | "x": -200, 617 | "y": -239 618 | }, 619 | "_skewX": 0, 620 | "_skewY": 0, 621 | "_localZOrder": 0, 622 | "_globalZOrder": 0, 623 | "_opacityModifyRGB": false, 624 | "groupIndex": 0 625 | }, 626 | { 627 | "__type__": "cc.Node", 628 | "_name": "Label", 629 | "_objFlags": 0, 630 | "_parent": { 631 | "__id__": 14 632 | }, 633 | "_children": [], 634 | "_tag": -1, 635 | "_active": true, 636 | "_components": [ 637 | { 638 | "__id__": 16 639 | } 640 | ], 641 | "_prefab": null, 642 | "_id": "6508caudM5AMoGCkPAEKs7Y", 643 | "_opacity": 255, 644 | "_color": { 645 | "__type__": "cc.Color", 646 | "r": 0, 647 | "g": 0, 648 | "b": 0, 649 | "a": 255 650 | }, 651 | "_cascadeOpacityEnabled": true, 652 | "_anchorPoint": { 653 | "__type__": "cc.Vec2", 654 | "x": 0.5, 655 | "y": 0.5 656 | }, 657 | "_contentSize": { 658 | "__type__": "cc.Size", 659 | "width": 100, 660 | "height": 40 661 | }, 662 | "_rotationX": 0, 663 | "_rotationY": 0, 664 | "_scaleX": 1, 665 | "_scaleY": 1, 666 | "_position": { 667 | "__type__": "cc.Vec2", 668 | "x": 0, 669 | "y": 0 670 | }, 671 | "_skewX": 0, 672 | "_skewY": 0, 673 | "_localZOrder": 0, 674 | "_globalZOrder": 0, 675 | "_opacityModifyRGB": false, 676 | "groupIndex": 0 677 | }, 678 | { 679 | "__type__": "cc.Label", 680 | "_name": "", 681 | "_objFlags": 0, 682 | "node": { 683 | "__id__": 15 684 | }, 685 | "_enabled": true, 686 | "_useOriginalSize": false, 687 | "_actualFontSize": 20, 688 | "_fontSize": 20, 689 | "_lineHeight": 40, 690 | "_enableWrapText": false, 691 | "_N$file": null, 692 | "_isSystemFontUsed": true, 693 | "_spacingX": 0, 694 | "_N$string": "障碍物", 695 | "_N$horizontalAlign": 1, 696 | "_N$verticalAlign": 1, 697 | "_N$fontFamily": "Arial", 698 | "_N$overflow": 1 699 | }, 700 | { 701 | "__type__": "cc.Sprite", 702 | "_name": "", 703 | "_objFlags": 0, 704 | "node": { 705 | "__id__": 14 706 | }, 707 | "_enabled": true, 708 | "_spriteFrame": { 709 | "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" 710 | }, 711 | "_type": 1, 712 | "_sizeMode": 0, 713 | "_fillType": 0, 714 | "_fillCenter": { 715 | "__type__": "cc.Vec2", 716 | "x": 0, 717 | "y": 0 718 | }, 719 | "_fillStart": 0, 720 | "_fillRange": 0, 721 | "_isTrimmedMode": true, 722 | "_srcBlendFactor": 770, 723 | "_dstBlendFactor": 771, 724 | "_atlas": null 725 | }, 726 | { 727 | "__type__": "cc.Button", 728 | "_name": "", 729 | "_objFlags": 0, 730 | "node": { 731 | "__id__": 14 732 | }, 733 | "_enabled": true, 734 | "transition": 2, 735 | "pressedColor": { 736 | "__type__": "cc.Color", 737 | "r": 255, 738 | "g": 255, 739 | "b": 255, 740 | "a": 255 741 | }, 742 | "hoverColor": { 743 | "__type__": "cc.Color", 744 | "r": 255, 745 | "g": 255, 746 | "b": 255, 747 | "a": 255 748 | }, 749 | "duration": 0.1, 750 | "zoomScale": 1.2, 751 | "pressedSprite": { 752 | "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" 753 | }, 754 | "hoverSprite": { 755 | "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" 756 | }, 757 | "clickEvents": [], 758 | "_N$interactable": true, 759 | "_N$enableAutoGrayEffect": false, 760 | "_N$normalColor": { 761 | "__type__": "cc.Color", 762 | "r": 255, 763 | "g": 255, 764 | "b": 255, 765 | "a": 255 766 | }, 767 | "_N$disabledColor": { 768 | "__type__": "cc.Color", 769 | "r": 255, 770 | "g": 255, 771 | "b": 255, 772 | "a": 255 773 | }, 774 | "_N$normalSprite": { 775 | "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" 776 | }, 777 | "_N$disabledSprite": { 778 | "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" 779 | }, 780 | "_N$target": { 781 | "__id__": 14 782 | } 783 | }, 784 | { 785 | "__type__": "cc.Node", 786 | "_name": "target", 787 | "_objFlags": 0, 788 | "_parent": { 789 | "__id__": 8 790 | }, 791 | "_children": [ 792 | { 793 | "__id__": 20 794 | } 795 | ], 796 | "_tag": -1, 797 | "_active": true, 798 | "_components": [ 799 | { 800 | "__id__": 22 801 | }, 802 | { 803 | "__id__": 23 804 | } 805 | ], 806 | "_prefab": null, 807 | "_id": "7d685X72TNA1ZkUMWLXtxKQ", 808 | "_opacity": 255, 809 | "_color": { 810 | "__type__": "cc.Color", 811 | "r": 255, 812 | "g": 255, 813 | "b": 255, 814 | "a": 255 815 | }, 816 | "_cascadeOpacityEnabled": true, 817 | "_anchorPoint": { 818 | "__type__": "cc.Vec2", 819 | "x": 0.5, 820 | "y": 0.5 821 | }, 822 | "_contentSize": { 823 | "__type__": "cc.Size", 824 | "width": 100, 825 | "height": 40 826 | }, 827 | "_rotationX": 0, 828 | "_rotationY": 0, 829 | "_scaleX": 1, 830 | "_scaleY": 1, 831 | "_position": { 832 | "__type__": "cc.Vec2", 833 | "x": -45, 834 | "y": -239 835 | }, 836 | "_skewX": 0, 837 | "_skewY": 0, 838 | "_localZOrder": 0, 839 | "_globalZOrder": 0, 840 | "_opacityModifyRGB": false, 841 | "groupIndex": 0 842 | }, 843 | { 844 | "__type__": "cc.Node", 845 | "_name": "Label", 846 | "_objFlags": 0, 847 | "_parent": { 848 | "__id__": 19 849 | }, 850 | "_children": [], 851 | "_tag": -1, 852 | "_active": true, 853 | "_components": [ 854 | { 855 | "__id__": 21 856 | } 857 | ], 858 | "_prefab": null, 859 | "_id": "42f66BzuqhHH7uVpah7owX7", 860 | "_opacity": 255, 861 | "_color": { 862 | "__type__": "cc.Color", 863 | "r": 0, 864 | "g": 0, 865 | "b": 0, 866 | "a": 255 867 | }, 868 | "_cascadeOpacityEnabled": true, 869 | "_anchorPoint": { 870 | "__type__": "cc.Vec2", 871 | "x": 0.5, 872 | "y": 0.5 873 | }, 874 | "_contentSize": { 875 | "__type__": "cc.Size", 876 | "width": 100, 877 | "height": 40 878 | }, 879 | "_rotationX": 0, 880 | "_rotationY": 0, 881 | "_scaleX": 1, 882 | "_scaleY": 1, 883 | "_position": { 884 | "__type__": "cc.Vec2", 885 | "x": 0, 886 | "y": 0 887 | }, 888 | "_skewX": 0, 889 | "_skewY": 0, 890 | "_localZOrder": 0, 891 | "_globalZOrder": 0, 892 | "_opacityModifyRGB": false, 893 | "groupIndex": 0 894 | }, 895 | { 896 | "__type__": "cc.Label", 897 | "_name": "", 898 | "_objFlags": 0, 899 | "node": { 900 | "__id__": 20 901 | }, 902 | "_enabled": true, 903 | "_useOriginalSize": false, 904 | "_actualFontSize": 20, 905 | "_fontSize": 20, 906 | "_lineHeight": 40, 907 | "_enableWrapText": false, 908 | "_N$file": null, 909 | "_isSystemFontUsed": true, 910 | "_spacingX": 0, 911 | "_N$string": "目标点", 912 | "_N$horizontalAlign": 1, 913 | "_N$verticalAlign": 1, 914 | "_N$fontFamily": "Arial", 915 | "_N$overflow": 1 916 | }, 917 | { 918 | "__type__": "cc.Sprite", 919 | "_name": "", 920 | "_objFlags": 0, 921 | "node": { 922 | "__id__": 19 923 | }, 924 | "_enabled": true, 925 | "_spriteFrame": { 926 | "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" 927 | }, 928 | "_type": 1, 929 | "_sizeMode": 0, 930 | "_fillType": 0, 931 | "_fillCenter": { 932 | "__type__": "cc.Vec2", 933 | "x": 0, 934 | "y": 0 935 | }, 936 | "_fillStart": 0, 937 | "_fillRange": 0, 938 | "_isTrimmedMode": true, 939 | "_srcBlendFactor": 770, 940 | "_dstBlendFactor": 771, 941 | "_atlas": null 942 | }, 943 | { 944 | "__type__": "cc.Button", 945 | "_name": "", 946 | "_objFlags": 0, 947 | "node": { 948 | "__id__": 19 949 | }, 950 | "_enabled": true, 951 | "transition": 2, 952 | "pressedColor": { 953 | "__type__": "cc.Color", 954 | "r": 255, 955 | "g": 255, 956 | "b": 255, 957 | "a": 255 958 | }, 959 | "hoverColor": { 960 | "__type__": "cc.Color", 961 | "r": 255, 962 | "g": 255, 963 | "b": 255, 964 | "a": 255 965 | }, 966 | "duration": 0.1, 967 | "zoomScale": 1.2, 968 | "pressedSprite": { 969 | "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" 970 | }, 971 | "hoverSprite": { 972 | "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" 973 | }, 974 | "clickEvents": [], 975 | "_N$interactable": true, 976 | "_N$enableAutoGrayEffect": false, 977 | "_N$normalColor": { 978 | "__type__": "cc.Color", 979 | "r": 255, 980 | "g": 255, 981 | "b": 255, 982 | "a": 255 983 | }, 984 | "_N$disabledColor": { 985 | "__type__": "cc.Color", 986 | "r": 255, 987 | "g": 255, 988 | "b": 255, 989 | "a": 255 990 | }, 991 | "_N$normalSprite": { 992 | "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" 993 | }, 994 | "_N$disabledSprite": { 995 | "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" 996 | }, 997 | "_N$target": { 998 | "__id__": 19 999 | } 1000 | }, 1001 | { 1002 | "__type__": "cc.Node", 1003 | "_name": "action", 1004 | "_objFlags": 0, 1005 | "_parent": { 1006 | "__id__": 8 1007 | }, 1008 | "_children": [ 1009 | { 1010 | "__id__": 25 1011 | } 1012 | ], 1013 | "_tag": -1, 1014 | "_active": true, 1015 | "_components": [ 1016 | { 1017 | "__id__": 27 1018 | }, 1019 | { 1020 | "__id__": 28 1021 | } 1022 | ], 1023 | "_prefab": null, 1024 | "_id": "9dfd1ivJHJJWow+mq0fATSz", 1025 | "_opacity": 255, 1026 | "_color": { 1027 | "__type__": "cc.Color", 1028 | "r": 255, 1029 | "g": 255, 1030 | "b": 255, 1031 | "a": 255 1032 | }, 1033 | "_cascadeOpacityEnabled": true, 1034 | "_anchorPoint": { 1035 | "__type__": "cc.Vec2", 1036 | "x": 0.5, 1037 | "y": 0.5 1038 | }, 1039 | "_contentSize": { 1040 | "__type__": "cc.Size", 1041 | "width": 100, 1042 | "height": 40 1043 | }, 1044 | "_rotationX": 0, 1045 | "_rotationY": 0, 1046 | "_scaleX": 1, 1047 | "_scaleY": 1, 1048 | "_position": { 1049 | "__type__": "cc.Vec2", 1050 | "x": 110, 1051 | "y": -239 1052 | }, 1053 | "_skewX": 0, 1054 | "_skewY": 0, 1055 | "_localZOrder": 0, 1056 | "_globalZOrder": 0, 1057 | "_opacityModifyRGB": false, 1058 | "groupIndex": 0 1059 | }, 1060 | { 1061 | "__type__": "cc.Node", 1062 | "_name": "Label", 1063 | "_objFlags": 0, 1064 | "_parent": { 1065 | "__id__": 24 1066 | }, 1067 | "_children": [], 1068 | "_tag": -1, 1069 | "_active": true, 1070 | "_components": [ 1071 | { 1072 | "__id__": 26 1073 | } 1074 | ], 1075 | "_prefab": null, 1076 | "_id": "f5cbdx0eshFD5LRdujdgtkG", 1077 | "_opacity": 255, 1078 | "_color": { 1079 | "__type__": "cc.Color", 1080 | "r": 0, 1081 | "g": 0, 1082 | "b": 0, 1083 | "a": 255 1084 | }, 1085 | "_cascadeOpacityEnabled": true, 1086 | "_anchorPoint": { 1087 | "__type__": "cc.Vec2", 1088 | "x": 0.5, 1089 | "y": 0.5 1090 | }, 1091 | "_contentSize": { 1092 | "__type__": "cc.Size", 1093 | "width": 100, 1094 | "height": 40 1095 | }, 1096 | "_rotationX": 0, 1097 | "_rotationY": 0, 1098 | "_scaleX": 1, 1099 | "_scaleY": 1, 1100 | "_position": { 1101 | "__type__": "cc.Vec2", 1102 | "x": 0, 1103 | "y": 0 1104 | }, 1105 | "_skewX": 0, 1106 | "_skewY": 0, 1107 | "_localZOrder": 0, 1108 | "_globalZOrder": 0, 1109 | "_opacityModifyRGB": false, 1110 | "groupIndex": 0 1111 | }, 1112 | { 1113 | "__type__": "cc.Label", 1114 | "_name": "", 1115 | "_objFlags": 0, 1116 | "node": { 1117 | "__id__": 25 1118 | }, 1119 | "_enabled": true, 1120 | "_useOriginalSize": false, 1121 | "_actualFontSize": 20, 1122 | "_fontSize": 20, 1123 | "_lineHeight": 40, 1124 | "_enableWrapText": false, 1125 | "_N$file": null, 1126 | "_isSystemFontUsed": true, 1127 | "_spacingX": 0, 1128 | "_N$string": "开始寻路", 1129 | "_N$horizontalAlign": 1, 1130 | "_N$verticalAlign": 1, 1131 | "_N$fontFamily": "Arial", 1132 | "_N$overflow": 1 1133 | }, 1134 | { 1135 | "__type__": "cc.Sprite", 1136 | "_name": "", 1137 | "_objFlags": 0, 1138 | "node": { 1139 | "__id__": 24 1140 | }, 1141 | "_enabled": true, 1142 | "_spriteFrame": { 1143 | "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" 1144 | }, 1145 | "_type": 1, 1146 | "_sizeMode": 0, 1147 | "_fillType": 0, 1148 | "_fillCenter": { 1149 | "__type__": "cc.Vec2", 1150 | "x": 0, 1151 | "y": 0 1152 | }, 1153 | "_fillStart": 0, 1154 | "_fillRange": 0, 1155 | "_isTrimmedMode": true, 1156 | "_srcBlendFactor": 770, 1157 | "_dstBlendFactor": 771, 1158 | "_atlas": null 1159 | }, 1160 | { 1161 | "__type__": "cc.Button", 1162 | "_name": "", 1163 | "_objFlags": 0, 1164 | "node": { 1165 | "__id__": 24 1166 | }, 1167 | "_enabled": true, 1168 | "transition": 2, 1169 | "pressedColor": { 1170 | "__type__": "cc.Color", 1171 | "r": 255, 1172 | "g": 255, 1173 | "b": 255, 1174 | "a": 255 1175 | }, 1176 | "hoverColor": { 1177 | "__type__": "cc.Color", 1178 | "r": 255, 1179 | "g": 255, 1180 | "b": 255, 1181 | "a": 255 1182 | }, 1183 | "duration": 0.1, 1184 | "zoomScale": 1.2, 1185 | "pressedSprite": { 1186 | "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" 1187 | }, 1188 | "hoverSprite": { 1189 | "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" 1190 | }, 1191 | "clickEvents": [], 1192 | "_N$interactable": true, 1193 | "_N$enableAutoGrayEffect": false, 1194 | "_N$normalColor": { 1195 | "__type__": "cc.Color", 1196 | "r": 255, 1197 | "g": 255, 1198 | "b": 255, 1199 | "a": 255 1200 | }, 1201 | "_N$disabledColor": { 1202 | "__type__": "cc.Color", 1203 | "r": 255, 1204 | "g": 255, 1205 | "b": 255, 1206 | "a": 255 1207 | }, 1208 | "_N$normalSprite": { 1209 | "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" 1210 | }, 1211 | "_N$disabledSprite": { 1212 | "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" 1213 | }, 1214 | "_N$target": { 1215 | "__id__": 24 1216 | } 1217 | }, 1218 | { 1219 | "__type__": "cc.Canvas", 1220 | "_name": "", 1221 | "_objFlags": 0, 1222 | "node": { 1223 | "__id__": 2 1224 | }, 1225 | "_enabled": true, 1226 | "_designResolution": { 1227 | "__type__": "cc.Size", 1228 | "width": 960, 1229 | "height": 640 1230 | }, 1231 | "_fitWidth": false, 1232 | "_fitHeight": true 1233 | }, 1234 | { 1235 | "__type__": "447ecKO4ABLpYWWQy88JhDB", 1236 | "_name": "", 1237 | "_objFlags": 0, 1238 | "node": { 1239 | "__id__": 2 1240 | }, 1241 | "_enabled": true, 1242 | "edit": false, 1243 | "edit1": false, 1244 | "map": { 1245 | "__id__": 6 1246 | } 1247 | } 1248 | ] -------------------------------------------------------------------------------- /assets/Scene/helloworld.fire.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.0", 3 | "uuid": "2d2f792f-a40c-49bb-a189-ed176a246e49", 4 | "asyncLoadAssets": false, 5 | "autoReleaseAssets": false, 6 | "subMetas": {} 7 | } -------------------------------------------------------------------------------- /assets/Script.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.1", 3 | "uuid": "4734c20c-0db8-4eb2-92ea-e692f4d70934", 4 | "isGroup": false, 5 | "subMetas": {} 6 | } -------------------------------------------------------------------------------- /assets/Script/AutoFindPath.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @param {object} bron 出生点 4 | * @param {object} obstacles 障碍物数组 5 | * @param {object} target 目标 6 | * @param {object} map 地图 7 | * @param {object} girdLength 格子长度 8 | * @return{Array} route 路径点数组(空数组为死路) 9 | * */ 10 | function getPath(bron, obstacles, target, map, girdLength) { 11 | var noWay = false; //死路 12 | var route = [bron]; //最佳路径 13 | var open = []; 14 | var closed = []; 15 | var fastTable = {}; //地图坐标的字典 16 | 17 | //生成地图坐标的字典 18 | map.forEach(function (t) { 19 | fastTable[t.x + '' + t.y] = t; 20 | }); 21 | //处理参数 22 | var params = formatParams(bron, obstacles, target, map); 23 | bron = params.bron; 24 | obstacles = params.obstacles; 25 | target = params.target; 26 | //处理出生点值 27 | bron.G = 0; 28 | bron.H = H(bron, target, girdLength); 29 | bron.F = F(bron); 30 | //更改当前作用点 31 | var nowGird = bron; 32 | //将障碍物加到closed列表 33 | obstacles.forEach(function (t) { 34 | closed.push(t); 35 | }); 36 | //先把出生点放到closed列表中 37 | closeFn(closed, bron); 38 | //开始BFS所有 39 | for (; closed.indexOf(target) < 0;) { 40 | //把可以走的格子放到open列表 41 | try { 42 | openFn(open, closed, nowGird, girdLength, fastTable); 43 | } catch (e) { 44 | noWay = true; 45 | } 46 | if (noWay) { 47 | return route; 48 | } 49 | //计算open列表中格子的G H F值 50 | caleF(open, map, nowGird, target, bron, girdLength); 51 | //根据F值排序 52 | open.sort(function (t1, t2) { 53 | return t1.F - t2.F; 54 | }); 55 | //优化尽量走直线 56 | var tmp = null; 57 | open[0].p = calcP(open[0]); 58 | for(var i = 1; i < open.length; ++i) { 59 | if(open[0].F > open[i].F) { 60 | break; 61 | } 62 | else { 63 | open[i].p = calcP(open[i]); 64 | if(open[i].p > open[0].p) { 65 | tmp = open[0]; 66 | open[0] = open[i]; 67 | open[i] = tmp; 68 | } 69 | } 70 | } 71 | //移动作用点 72 | nowGird = open.shift(); 73 | closeFn(closed, nowGird); 74 | } 75 | /////////////////////////////////////////////////////////////////////////显示最优路径中的一条 76 | var pathLink = target; 77 | while (true) { 78 | if (!pathLink.hasOwnProperty('parent')) { 79 | break; 80 | } 81 | route.unshift(pathLink.parent); 82 | pathLink = pathLink.parent; 83 | } 84 | //加上目标点 85 | route.push(target); 86 | return route; 87 | } 88 | 89 | /** 90 | * 获取移动量 (地形越难走移动量越大) 91 | * @param {object} now 当前点 92 | * @return {number} 93 | * */ 94 | function G(now) { 95 | return now.G + 1; 96 | } 97 | 98 | /** 99 | * 移动量估算值 (从当前方块到终点的移动量估算值) 100 | * @param {object} self 当前点 101 | * @param {object} target 目标点 102 | * @param {object} girdLength 格子长度 103 | * @return {number} 104 | * */ 105 | function H(self, target, girdLength) { 106 | return Math.abs(target.x - self.x) / girdLength + Math.abs(target.y - self.y) / girdLength; 107 | } 108 | 109 | /** 110 | * 和值 (等于G+H) 111 | * @param {object} self 当前点 112 | * */ 113 | function F(self) { 114 | return self.G + self.H; 115 | } 116 | 117 | /** 118 | * open函数 (添加到open列表) 119 | * @param {object} open open列表 120 | * @param {object} closed closed列表 121 | * @param {object} point 当前点 122 | * @param {object} girdLength 格子长度 123 | * @param {object} fastTable 快速查找表 124 | * */ 125 | function openFn(open, closed, point, girdLength, fastTable) { 126 | var results = []; 127 | if (fastTable[point.x + '' + (point.y + girdLength)]) { 128 | results.push(fastTable[point.x + '' + (point.y + girdLength)]); 129 | } 130 | if (fastTable[point.x + '' + (point.y - girdLength)]) { 131 | results.push(fastTable[point.x + '' + (point.y - girdLength)]); 132 | } 133 | if (fastTable[(point.x + girdLength) + '' + point.y]) { 134 | results.push(fastTable[(point.x + girdLength) + '' + point.y]); 135 | } 136 | if (fastTable[(point.x - girdLength) + '' + point.y]) { 137 | results.push(fastTable[(point.x - girdLength) + '' + point.y]); 138 | } 139 | results.forEach(function (t) { 140 | if (!canReach(open, closed, t)) { 141 | return; 142 | } 143 | open.push(t); 144 | }); 145 | } 146 | 147 | /** 148 | * close函数 (添加到close列表) 149 | * @param {object} closed closed列表 150 | * @param {object} item 待加入点 151 | * */ 152 | function closeFn(closed, item) { 153 | closed.push(item); 154 | } 155 | 156 | /** 157 | * 计算open列表中所有项F值 158 | * @param {object} open open列表 159 | * @param {object} map closed列表 160 | * @param {object} now 当前点 161 | * @param {object} target 目标点 162 | * @param {object} girdLength 格子长度 163 | * */ 164 | function caleF(open, map, now, target, bron, girdLength) { 165 | open.forEach(function (t) { 166 | if (!t.F) { 167 | //找出最优的G 168 | var k = []; 169 | var around = getAround(map, [], [], t, girdLength); 170 | around.forEach(function (t2) { 171 | if (t2.G >= 0) { 172 | k.push(t2); 173 | } 174 | }); 175 | k.sort(function (t1, t2) { 176 | return t1.F - t2.F; 177 | }); 178 | var shift = k.shift(); 179 | t.G = G(shift); 180 | //处理H值和F值 181 | t.H = H(t, target, girdLength); 182 | t.F = F(t); 183 | t.parent = shift; 184 | } 185 | }); 186 | } 187 | 188 | /** 189 | * 检测是否可以走 190 | * @param {object} open open列表 191 | * @param {object} close closed列表 192 | * @param {object} gird 检测的点 193 | * */ 194 | function canReach(open, close, gird) { 195 | var i, l; 196 | for (i = 0, l = open.length; i < open.length; ++i) { 197 | if (open.hasOwnProperty(i)) { 198 | if (open.indexOf(gird) > -1) { 199 | return false; 200 | } 201 | } 202 | } 203 | for (i = 0, l = close.length; i < close.length; ++i) { 204 | if (close.hasOwnProperty(i)) { 205 | if (close.indexOf(gird) > -1) { 206 | return false; 207 | } 208 | } 209 | } 210 | return true; 211 | } 212 | 213 | /** 214 | * 检测他四周的格子 215 | * @param {object} container 所有可选点的集合 216 | * @param {object} obstacles 障碍物 217 | * @param {object} route 已走的点 218 | * @param {object} point 原点 219 | * @param {object} girdLength 格子尺寸 220 | * */ 221 | function getAround(container, obstacles, route, point, girdLength) { 222 | var result = []; 223 | container.forEach(function (t) { 224 | if (obstacles.indexOf(t) >= 0) { 225 | return; 226 | } 227 | if (route.indexOf(t) >= 0) { 228 | return; 229 | } 230 | if ((Math.abs(t.x - point.x) <= girdLength && 231 | Math.abs(t.y - point.y) <= girdLength) && 232 | !(Math.abs(t.x - point.x) === girdLength && 233 | Math.abs(t.y - point.y) === girdLength) && 234 | t !== point 235 | ) { 236 | result.push(t); 237 | } 238 | }); 239 | return result; 240 | } 241 | 242 | /** 243 | * 格式化参数 244 | * @param {object} bron 出生点 245 | * @param {object} obstacles 障碍物数组 246 | * @param {object} target 目标 247 | * @param {object} map 地图 248 | * */ 249 | function formatParams(bron, obstacles, target, map) { 250 | var res = {bron: null, obstacles: [], target: null}; 251 | for (var i in map) { 252 | if (map.hasOwnProperty(i)) { 253 | //处理出生点 254 | if (map[i].x === bron.x && map[i].y === bron.y) { 255 | res.bron = map[i]; 256 | } 257 | //处理目标点 258 | if (map[i].x === target.x && map[i].y === target.y) { 259 | res.target = map[i]; 260 | } 261 | //处理障碍物 262 | obstacles.forEach(function (t) { 263 | if (t.x === map[i].x && map[i].y === t.y) { 264 | res.obstacles.push(map[i]); 265 | } 266 | }); 267 | } 268 | } 269 | return res; 270 | } 271 | 272 | /** 273 | * 计算P值(优化用) 274 | * @param {object} point 待计算点 275 | * */ 276 | function calcP(point) { 277 | var res = 0; 278 | if((point.parent && point.parent.x === point.x) || 279 | (point.parent && point.parent.y === point.y) 280 | ) { 281 | ++res; 282 | if((point.parent.parent && point.parent.parent.x === point.x) || 283 | (point.parent.parent && point.parent.parent.y === point.y) 284 | ) { 285 | ++res; 286 | } 287 | } 288 | return res; 289 | } 290 | 291 | module.exports = { 292 | getPath: getPath 293 | }; -------------------------------------------------------------------------------- /assets/Script/AutoFindPath.js.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.3", 3 | "uuid": "2fb3cbc7-7999-4926-bbe8-cf40b8e9233d", 4 | "isPlugin": false, 5 | "loadPluginInWeb": true, 6 | "loadPluginInNative": true, 7 | "loadPluginInEditor": false, 8 | "subMetas": {} 9 | } -------------------------------------------------------------------------------- /assets/Script/HelloWorld.js: -------------------------------------------------------------------------------- 1 | var authFindPath = require('AutoFindPath'); 2 | 3 | cc.Class({ 4 | extends: cc.Component, 5 | 6 | properties: { 7 | gird: { 8 | default: null, 9 | type: cc.Prefab 10 | }, 11 | canv: { 12 | default: null, 13 | type: cc.Node 14 | }, 15 | 16 | operatorAction: { 17 | default: 0 // 0: 无操作 1: 设置出生点 2: 设置障碍物 3: 设置目标点 4: 开始寻路 18 | } 19 | }, 20 | 21 | // use this for initialization 22 | onLoad: function () { 23 | 24 | var self = this; 25 | 26 | //地图脚本 27 | this.canvScript = this.canv.getComponent('canvasControl'); 28 | 29 | //初始化地图 30 | var winSize = cc.director.getWinSize(); 31 | var instantiate = cc.instantiate(this.gird); 32 | var widthNum = (winSize.width / instantiate.width); 33 | var heightNum = (winSize.height / instantiate.height); 34 | for(var i = 0; i < widthNum; ++i) { 35 | for(var j = 0; j < heightNum; ++j) { 36 | var _instantiate = cc.instantiate(this.gird); 37 | _instantiate.x = i * instantiate.width; 38 | _instantiate.y = j * instantiate.height; 39 | _instantiate.setTag(_instantiate.x + '' + _instantiate.y); 40 | this.node.addChild(_instantiate); 41 | } 42 | } 43 | 44 | //事件监听 45 | //出身点 46 | cc.find('Canvas/control/bron').on('mousedown', function ( event ) { 47 | self.canvScript.SetEdit1(1); 48 | self.operatorAction = 1; 49 | }.bind(this)); 50 | //障碍物 51 | cc.find('Canvas/control/obstacles').on('mousedown', function ( event ) { 52 | self.canvScript.SetEdit1(2); 53 | self.operatorAction = 2; 54 | }.bind(this)); 55 | //目标点 56 | cc.find('Canvas/control/target').on('mousedown', function ( event ) { 57 | self.canvScript.SetEdit1(3); 58 | self.operatorAction = 3; 59 | }.bind(this)); 60 | //开始寻路 61 | cc.find('Canvas/control/action').on('mousedown', function ( event ) { 62 | self.canvScript.SetEdit1(4); 63 | self.operatorAction = 4; 64 | 65 | var navigationInfo = self.getNavigationInfo(); 66 | var path = authFindPath.getPath(navigationInfo.bron, navigationInfo.obstacles, navigationInfo.target, navigationInfo.map, navigationInfo.girdLength); 67 | 68 | console.log(path); 69 | 70 | 71 | var index = 0; 72 | setInterval(function () { 73 | if(index === path.length) { 74 | return; 75 | } 76 | var t = path[index++]; 77 | var node = self.node.getChildByTag(t.x + '' + t.y); 78 | node.setColor(cc.color(255,230,150)); 79 | }, 200); 80 | 81 | 82 | /* 83 | path[1].forEach(function (t) { 84 | 85 | if(t.F) { 86 | var node = self.node.getChildByTag(t.x + '' + t.y); 87 | 88 | // node.setColor(cc.color(255,230,150)); 89 | 90 | node.getChildByName('tt').getComponent('cc.Label').string = t.F; 91 | 92 | } 93 | 94 | }); 95 | 96 | path[2].forEach(function (t) { 97 | 98 | if(t.F) { 99 | var node = self.node.getChildByTag(t.x + '' + t.y); 100 | 101 | node.setColor(cc.color(255,230,150)); 102 | 103 | 104 | } 105 | 106 | }); 107 | */ 108 | 109 | }.bind(this)); 110 | 111 | }, 112 | 113 | //获取导航信息 114 | getNavigationInfo: function () { 115 | var res = {bron: null, obstacles: [], target: null, map: [], girdLength: 0}; 116 | res.bron = new cc.Vec2(this.bron.x, this.bron.y); 117 | res.target = new cc.Vec2(this.target.x, this.target.y); 118 | res.girdLength = this.bron.width; 119 | this.obstacles.forEach(function (t) { 120 | res.obstacles.push(new cc.Vec2(t.x, t.y)) 121 | }); 122 | this.node.getChildren().forEach(function (t) { 123 | res.map.push(new cc.Vec2(t.x, t.y)); 124 | }); 125 | return res; 126 | }, 127 | 128 | //更新地图变化 129 | getMapNotify: function (position) { 130 | var gridObject = this.getGirdObject(position); 131 | 132 | 133 | 134 | //设置出生点 135 | if(this.operatorAction === 1) { 136 | //已经有出生点就清空 137 | if(this.bron) { 138 | this.bron.setColor(cc.color(255,255,255)); 139 | } 140 | this.bron = gridObject; 141 | gridObject.setColor(cc.color(0,255,0)); 142 | } 143 | //设置障碍物 144 | if(this.operatorAction === 2) { 145 | if(!this.obstacles) { 146 | this.obstacles = []; 147 | } 148 | this.obstacles.push(gridObject); 149 | gridObject.setColor(cc.color(0,0,0)); 150 | } 151 | //设置目标点 152 | if(this.operatorAction === 3) { 153 | //已经结束点就清空 154 | if(this.target) { 155 | this.target.setColor(cc.color(255,255,255)); 156 | } 157 | this.target = gridObject; 158 | gridObject.setColor(cc.color(255,165,0)); 159 | } 160 | }, 161 | 162 | //获取个字对象 163 | getGirdObject: function (position) { 164 | var children = this.node.getChildren(); 165 | for(var i = 0, l = children.length ; i < l; ++i) { 166 | if(children.hasOwnProperty(i)) { 167 | var t = children[i]; 168 | //包含该点 169 | if(cc.rectContainsPoint(t.getBoundingBox(), position)) { 170 | return t; 171 | } 172 | } 173 | } 174 | }, 175 | 176 | // called every frame 177 | update: function (dt) { 178 | 179 | } 180 | 181 | 182 | 183 | }); 184 | -------------------------------------------------------------------------------- /assets/Script/HelloWorld.js.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.3", 3 | "uuid": "280c3aec-6492-4a9d-9f51-a9b00b570b4a", 4 | "isPlugin": false, 5 | "loadPluginInWeb": true, 6 | "loadPluginInNative": true, 7 | "loadPluginInEditor": false, 8 | "subMetas": {} 9 | } -------------------------------------------------------------------------------- /assets/Script/canvasControl.js: -------------------------------------------------------------------------------- 1 | cc.Class({ 2 | extends: cc.Component, 3 | 4 | properties: { 5 | 6 | //用户点击地图后开启 7 | edit: { 8 | visible:false, 9 | default: false 10 | }, 11 | //用户点击编辑模式后开启 12 | edit1: { 13 | visible:false, 14 | default: 0 15 | }, 16 | map: { 17 | default: null, 18 | type: cc.Node 19 | } 20 | 21 | }, 22 | 23 | // use this for initialization 24 | onLoad: function () { 25 | 26 | //地图脚本 27 | this.mapScript = this.map.getComponent('HelloWorld'); 28 | 29 | var self = this; 30 | // 使用枚举类型来注册 31 | this.node.on(cc.Node.EventType.MOUSE_DOWN, function (event) { 32 | self.edit = true; 33 | //正在编辑模式 34 | if(self.edit && (this.edit1 !== 0 && this.edit1 !== 4) ) { 35 | self.notifyMapChange(event.getLocation()); 36 | } 37 | }, this); 38 | this.node.on(cc.Node.EventType.MOUSE_MOVE, function (event) { 39 | //正在编辑模式 40 | if(self.edit && (this.edit1 !== 0 && this.edit1 !== 4) ) { 41 | self.notifyMapChange(event.getLocation()); 42 | } 43 | }, this); 44 | this.node.on(cc.Node.EventType.MOUSE_UP, function (event) { 45 | self.edit = false; 46 | }, this); 47 | 48 | }, 49 | 50 | SetEdit1: function (state) { 51 | this.edit1 = state; 52 | }, 53 | 54 | //通知地图变化 55 | notifyMapChange: function (position) { 56 | this.mapScript.getMapNotify(position); 57 | } 58 | 59 | 60 | 61 | // update: function (dt) { 62 | 63 | // }, 64 | }); 65 | -------------------------------------------------------------------------------- /assets/Script/canvasControl.js.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.3", 3 | "uuid": "447ec28e-e000-4ba5-8596-432f3c2610c1", 4 | "isPlugin": false, 5 | "loadPluginInWeb": true, 6 | "loadPluginInNative": true, 7 | "loadPluginInEditor": false, 8 | "subMetas": {} 9 | } -------------------------------------------------------------------------------- /assets/Texture.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.1", 3 | "uuid": "7b81d4e8-ec84-4716-968d-500ac1d78a54", 4 | "isGroup": false, 5 | "subMetas": {} 6 | } -------------------------------------------------------------------------------- /assets/Texture/HelloWorld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microcisco/astar/bb4a1c191618919e6633a54002442fd6fc869230/assets/Texture/HelloWorld.png -------------------------------------------------------------------------------- /assets/Texture/HelloWorld.png.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.0", 3 | "uuid": "6aa0aa6a-ebee-4155-a088-a687a6aadec4", 4 | "type": "sprite", 5 | "wrapMode": "clamp", 6 | "filterMode": "bilinear", 7 | "subMetas": { 8 | "HelloWorld": { 9 | "ver": "1.0.3", 10 | "uuid": "31bc895a-c003-4566-a9f3-2e54ae1c17dc", 11 | "rawTextureUuid": "6aa0aa6a-ebee-4155-a088-a687a6aadec4", 12 | "trimType": "auto", 13 | "trimThreshold": 1, 14 | "rotated": false, 15 | "offsetX": 0, 16 | "offsetY": 0, 17 | "trimX": 0, 18 | "trimY": 0, 19 | "width": 195, 20 | "height": 270, 21 | "rawWidth": 195, 22 | "rawHeight": 270, 23 | "borderTop": 0, 24 | "borderBottom": 0, 25 | "borderLeft": 0, 26 | "borderRight": 0, 27 | "subMetas": {} 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /assets/Texture/gz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microcisco/astar/bb4a1c191618919e6633a54002442fd6fc869230/assets/Texture/gz.png -------------------------------------------------------------------------------- /assets/Texture/gz.png.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.0", 3 | "uuid": "7b548c52-595c-4b83-a50b-a5a3962604a8", 4 | "type": "sprite", 5 | "wrapMode": "clamp", 6 | "filterMode": "bilinear", 7 | "subMetas": { 8 | "gz": { 9 | "ver": "1.0.3", 10 | "uuid": "8842ccbb-3a36-430c-b02e-1f549d0f6036", 11 | "rawTextureUuid": "7b548c52-595c-4b83-a50b-a5a3962604a8", 12 | "trimType": "auto", 13 | "trimThreshold": 1, 14 | "rotated": false, 15 | "offsetX": 0, 16 | "offsetY": 0, 17 | "trimX": 0, 18 | "trimY": 0, 19 | "width": 20, 20 | "height": 20, 21 | "rawWidth": 20, 22 | "rawHeight": 20, 23 | "borderTop": 0, 24 | "borderBottom": 0, 25 | "borderLeft": 0, 26 | "borderRight": 0, 27 | "subMetas": {} 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /assets/Texture/singleColor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microcisco/astar/bb4a1c191618919e6633a54002442fd6fc869230/assets/Texture/singleColor.png -------------------------------------------------------------------------------- /assets/Texture/singleColor.png.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.0", 3 | "uuid": "a8027877-d8d6-4645-97a0-52d4a0123dba", 4 | "type": "sprite", 5 | "wrapMode": "clamp", 6 | "filterMode": "bilinear", 7 | "subMetas": { 8 | "singleColor": { 9 | "ver": "1.0.3", 10 | "uuid": "410fb916-8721-4663-bab8-34397391ace7", 11 | "rawTextureUuid": "a8027877-d8d6-4645-97a0-52d4a0123dba", 12 | "trimType": "auto", 13 | "trimThreshold": 1, 14 | "rotated": false, 15 | "offsetX": 0, 16 | "offsetY": 0, 17 | "trimX": 0, 18 | "trimY": 0, 19 | "width": 2, 20 | "height": 2, 21 | "rawWidth": 2, 22 | "rawHeight": 2, 23 | "borderTop": 0, 24 | "borderBottom": 0, 25 | "borderLeft": 0, 26 | "borderRight": 0, 27 | "subMetas": {} 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /assets/gz.prefab: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "__type__": "cc.Prefab", 4 | "_name": "", 5 | "_objFlags": 0, 6 | "_rawFiles": null, 7 | "data": { 8 | "__id__": 1 9 | } 10 | }, 11 | { 12 | "__type__": "cc.Node", 13 | "_name": "gz", 14 | "_objFlags": 0, 15 | "_parent": null, 16 | "_children": [ 17 | { 18 | "__id__": 2 19 | } 20 | ], 21 | "_tag": -1, 22 | "_active": true, 23 | "_components": [ 24 | { 25 | "__id__": 5 26 | } 27 | ], 28 | "_prefab": { 29 | "__id__": 6 30 | }, 31 | "_id": "", 32 | "_opacity": 255, 33 | "_color": { 34 | "__type__": "cc.Color", 35 | "r": 255, 36 | "g": 255, 37 | "b": 255, 38 | "a": 255 39 | }, 40 | "_cascadeOpacityEnabled": true, 41 | "_anchorPoint": { 42 | "__type__": "cc.Vec2", 43 | "x": 0, 44 | "y": 0 45 | }, 46 | "_contentSize": { 47 | "__type__": "cc.Size", 48 | "width": 20, 49 | "height": 20 50 | }, 51 | "_rotationX": 0, 52 | "_rotationY": 0, 53 | "_scaleX": 1, 54 | "_scaleY": 1, 55 | "_position": { 56 | "__type__": "cc.Vec2", 57 | "x": 0, 58 | "y": 0 59 | }, 60 | "_skewX": 0, 61 | "_skewY": 0, 62 | "_localZOrder": 0, 63 | "_globalZOrder": 0, 64 | "_opacityModifyRGB": false, 65 | "groupIndex": 0 66 | }, 67 | { 68 | "__type__": "cc.Node", 69 | "_name": "tt", 70 | "_objFlags": 0, 71 | "_parent": { 72 | "__id__": 1 73 | }, 74 | "_children": [], 75 | "_tag": -1, 76 | "_active": true, 77 | "_components": [ 78 | { 79 | "__id__": 3 80 | } 81 | ], 82 | "_prefab": { 83 | "__id__": 4 84 | }, 85 | "_id": "", 86 | "_opacity": 255, 87 | "_color": { 88 | "__type__": "cc.Color", 89 | "r": 166, 90 | "g": 36, 91 | "b": 172, 92 | "a": 255 93 | }, 94 | "_cascadeOpacityEnabled": true, 95 | "_anchorPoint": { 96 | "__type__": "cc.Vec2", 97 | "x": 0, 98 | "y": 0 99 | }, 100 | "_contentSize": { 101 | "__type__": "cc.Size", 102 | "width": 0, 103 | "height": 10 104 | }, 105 | "_rotationX": 0, 106 | "_rotationY": 0, 107 | "_scaleX": 1, 108 | "_scaleY": 1, 109 | "_position": { 110 | "__type__": "cc.Vec2", 111 | "x": 0, 112 | "y": 0 113 | }, 114 | "_skewX": 0, 115 | "_skewY": 0, 116 | "_localZOrder": 0, 117 | "_globalZOrder": 0, 118 | "_opacityModifyRGB": false, 119 | "groupIndex": 0 120 | }, 121 | { 122 | "__type__": "cc.Label", 123 | "_name": "", 124 | "_objFlags": 0, 125 | "node": { 126 | "__id__": 2 127 | }, 128 | "_enabled": true, 129 | "_useOriginalSize": false, 130 | "_actualFontSize": 10, 131 | "_fontSize": 10, 132 | "_lineHeight": 0, 133 | "_enableWrapText": true, 134 | "_N$file": null, 135 | "_isSystemFontUsed": true, 136 | "_spacingX": 0, 137 | "_N$string": "", 138 | "_N$horizontalAlign": 1, 139 | "_N$verticalAlign": 1, 140 | "_N$fontFamily": "Arial", 141 | "_N$overflow": 0 142 | }, 143 | { 144 | "__type__": "cc.PrefabInfo", 145 | "root": { 146 | "__id__": 1 147 | }, 148 | "asset": { 149 | "__uuid__": "0b857c5c-70e4-49ce-aaa8-17282b732a99" 150 | }, 151 | "fileId": "464aanb0FlHj6lX0y+p46mu", 152 | "sync": false 153 | }, 154 | { 155 | "__type__": "cc.Sprite", 156 | "_name": "", 157 | "_objFlags": 0, 158 | "node": { 159 | "__id__": 1 160 | }, 161 | "_enabled": true, 162 | "_spriteFrame": { 163 | "__uuid__": "8842ccbb-3a36-430c-b02e-1f549d0f6036" 164 | }, 165 | "_type": 0, 166 | "_sizeMode": 1, 167 | "_fillType": 0, 168 | "_fillCenter": { 169 | "__type__": "cc.Vec2", 170 | "x": 0, 171 | "y": 0 172 | }, 173 | "_fillStart": 0, 174 | "_fillRange": 0, 175 | "_isTrimmedMode": true, 176 | "_srcBlendFactor": 770, 177 | "_dstBlendFactor": 771, 178 | "_atlas": null 179 | }, 180 | { 181 | "__type__": "cc.PrefabInfo", 182 | "root": { 183 | "__id__": 1 184 | }, 185 | "asset": { 186 | "__uuid__": "0b857c5c-70e4-49ce-aaa8-17282b732a99" 187 | }, 188 | "fileId": "1393eAAawxJIJyKHSF2KFUY", 189 | "sync": false 190 | } 191 | ] -------------------------------------------------------------------------------- /assets/gz.prefab.meta: -------------------------------------------------------------------------------- 1 | { 2 | "ver": "1.0.0", 3 | "uuid": "0b857c5c-70e4-49ce-aaa8-17282b732a99", 4 | "asyncLoadAssets": false, 5 | "subMetas": {} 6 | } -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es6", 4 | "module": "commonjs", 5 | "experimentalDecorators": true 6 | }, 7 | "exclude": [ 8 | "node_modules", 9 | ".vscode", 10 | "library", 11 | "local", 12 | "settings", 13 | "temp" 14 | ] 15 | } -------------------------------------------------------------------------------- /project.json: -------------------------------------------------------------------------------- 1 | { 2 | "engine": "cocos2d-html5", 3 | "packages": "packages" 4 | } -------------------------------------------------------------------------------- /settings/builder.json: -------------------------------------------------------------------------------- 1 | { 2 | "excludeScenes": [], 3 | "orientation": { 4 | "landscapeLeft": true, 5 | "landscapeRight": true, 6 | "portrait": false, 7 | "upsideDown": false 8 | }, 9 | "packageName": "org.cocos2d.helloworld", 10 | "startScene": "2d2f792f-a40c-49bb-a189-ed176a246e49", 11 | "title": "hello_world", 12 | "webOrientation": "auto" 13 | } -------------------------------------------------------------------------------- /settings/builder.panel.json: -------------------------------------------------------------------------------- 1 | { 2 | "excludeScenes": [], 3 | "packageName": "org.cocos2d.helloworld", 4 | "platform": "web-mobile", 5 | "startScene": "2d2f792f-a40c-49bb-a189-ed176a246e49", 6 | "title": "HelloWorld" 7 | } -------------------------------------------------------------------------------- /settings/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "collision-matrix": [ 3 | [ 4 | true 5 | ] 6 | ], 7 | "excluded-modules": [], 8 | "group-list": [ 9 | "default" 10 | ], 11 | "start-scene": "current", 12 | "design-resolution-width": 960, 13 | "design-resolution-height": 640, 14 | "fit-width": false, 15 | "fit-height": true, 16 | "use-project-simulator-setting": false, 17 | "simulator-orientation": false, 18 | "use-customize-simulator": false, 19 | "simulator-resolution": { 20 | "width": 960, 21 | "height": 640 22 | } 23 | } -------------------------------------------------------------------------------- /template-banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microcisco/astar/bb4a1c191618919e6633a54002442fd6fc869230/template-banner.png -------------------------------------------------------------------------------- /template.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "TEMPLATES.helloworld.name", 3 | "desc": "TEMPLATES.helloworld.desc", 4 | "banner": "template-banner.png" 5 | } --------------------------------------------------------------------------------