Laya 踩坑日记 ---A* 导航寻路

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Laya 踩坑日记 ---A* 导航寻路

nuolo   2021-01-14 我要评论

要做寻路,然后看了看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

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们