Laya 踩坑日記 ---A* 導航尋路


要做尋路,然后看了看laya 官方的例子,感覺看的一臉懵逼,早了半天的api 也沒找到在哪有尋路的,最后一看代碼,原來是用的github上的A星方案  https://github.com/bgrins/javascript-astar

這就尷尬了,,然后研究了一下,果然還是要unity支持,導出烘焙過的地圖信息,在網上找了一些方案,然后自己也做了一下修改和調整,現在給大家發出來,(當做參考用 。。。)

需要unity 插件 A*   (AstarPathfindingProject  )

然后配置好地圖,然后烘焙

 

然后 通過AstartExpLayaTool  /exp json   導出地圖的json信息 (json信息 已自動復制到 剪切板中了)

在通過laya 去調用

 

Laya 這邊 將json 信息給存起來然后調用

通過github 把 astar.js 給下載下來,然后放到項目的libs下,編寫  .d.ts  輔助文件

 

declare class astar {
      /**
         * Perform an A* Search on a graph given a start and end node.
         * @param {Graph} graph
         * @param {GridNode} start
         * @param {GridNode} end
         * @param {Object} [options]
         * @param {bool} [options.closest] Specifies whether to return the
                     path to the closest node if the target is unreachable.
        * @param {Function} [options.heuristic] Heuristic function (see
        *          astar.heuristics).
        */
    static search(graph, start, end, options?);
    cleanNode(node);
}

declare class Graph {
    /**
     * A graph memory structure
     * @param {Array} gridIn 2D array of input weights
     * @param {Object} [options]
     * @param {bool} [options.diagonal] Specifies whether diagonal moves are allowed
     */
    constructor(gridIn, options?)
    grid: any;
   
}

declare class GridNode {
    x;
    y;
    weight;
    constructor(x?, y?, weight?)
}

然后編寫自己尋路代碼就行了

 

 

備注:此方案當前性能偏低,將坐標對應矩陣部分有問題,但作為一個參考還是可以的

laya地址  https://gitee.com/eryuefeng/laya-astar-pathfinding

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM