cocos2d-js的a星四方向寻路开源

开源一个cocos2d-js简单的A星四方向寻路。


说明:
- 本代码是我本人原创写的,并已经正常使用在我个人的项目里了

- 与TiledMap深度兼容
- MAP格式为二维数组,注意map[Y][X]里Y轴在前X轴在后,这样的目的是方便生成地图,当然你可以自己改
- MAP里障碍的数值为0
- 略高效
- 已经封装成类
- 可拓展成八反向
- 带注解
- 可随便使用,无需版权声明


var aStar = cc.Class.extend({
    open : null, //待遍历的数组
    close : null, //关闭数组
    starPoint : null, //开始点
    closePoint : null, //结束点
    map : null, //地图数组
    dirs : null, //上左下右
    path : null, //路径数组
    ctor:function(){},
    findPath:function(start,end,m){
        this.open = new Array();
        this.close = new Array();
        this.path = new Array();
        this.starPoint = start;
        this.closePoint = end;
        this.map = m;
        this.dirs = [[0,1],[-1,0],[0,-1],[1,0]];
        for (var i = 0; i < this.map.length; i++) {
            this.close[i] = new Array();
            for (var n = 0; n < this.map[i].length; n++) {
                this.close[i][n] = 0;
            };
        };
        //加入起始节点  [x, y , G ,F ,father]
        this.open.push([this.starPoint[0],
            this.starPoint[1],
            0,
            (Math.abs(this.closePoint[0]-this.starPoint[0])*10 + Math.abs(this.closePoint[1]-this.starPoint[1])*10),
            null
        ]);
        return this.ergodicGrid(this.open[0]);
    },
    //根据F值进行排序
    fSort:function(a,b){
        return a[3] - b[3];
    },
    //循环遍历网格    
    ergodicGrid:function(g){
        var around = this.getGridAround(g);
        for (var i = 0; i < around.length; i++) {
            //4.判断网格点是否为终点,为真则回溯路径点并结束遍历,为假则向下执行
            if (around[i][0] == this.closePoint[0] && around[i][1] == this.closePoint[1]) {
                console.log('end');
                var ele = g;
                this.path.unshift([around[i][0],around[i][1]]);
                do{
                    this.path.unshift([ele[0],ele[1]]);
                    ele = ele[4];
                }while(ele[4] != null);
                return this.path;
            };
            //5.计算四周网格点的GHF值压入open堆栈并设当前网格点为父坐标;
            var G = g[2] + 10;
            var H = Math.abs(this.closePoint[0] - around[i][0]) * 10 + Math.abs(this.closePoint[1] - around[i][1]) * 10;
            var F = G + H;
            this.open.push([around[i][0],around[i][1],G,F,g]);
        };
        //6.将当前网格点压出open堆栈,并设置close数组坐标为1
        var out = this.open.shift(); //open.shift删除并返回数组的第一个元素
        this.close[out[1]][out[0]] = 1;
        //7.根据四周网格点F值重新排列open堆栈
        this.open.sort(this.fSort);
        //8.判断open堆栈是否为0,为真则返回null,为假则向下执行
        if (this.open.length == 0) return null;
        return this.ergodicGrid(this.open[0]);
    },
    //获取网格点上左下右
    getGridAround:function(g){
        var a = new Array();
        var xl = this.map[0].length;
        var yl = this.map.length;
        //1.获取当前网格点四周网格点坐标;
        for (var i = 0; i < this.dirs.length; i++) {
            var x = g[0] + this.dirs[i][0];
            var y = g[1] + this.dirs[i][1];
            if(x < 0 || x >= xl || y < 0 || y >= yl) continue;
            //2.判断四周网格点是否为父坐标;为真则不做处理,为假则向下执行
            if (g[4] != null) {
                if (x == g[4][0] && y == g[4][1]) continue;
            };
            //3.判断四周网格点是否为障碍或close数组里的坐标;为真则不处理,为假则向下执行
            if (this.map[y][x] == 0 || this.close[y][x] == 1) continue;
            a.push([x,y]);
        };
        return a;
    }
});



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值