cocoscreator A* 寻路算法

简介: cocoscreator A* 寻路算法
let Grid = cc.Class({
    ctor(){
        this.x = 0;
        this.y = 0;
        this.f = 0;
        this.g = 0;
        this.h = 0;
        this.parent = null;
        this.type = 0; // -1障碍物, 0正常, 1起点, 2目的点
    }
});
let AStar = cc.Class({
    extends: cc.Component,
    properties:{
        map: cc.Graphics,
    },
    onLoad(){
        this._gridW = 50;   // 单元格子宽度
        this._gridH = 50;   // 单元格子高度
        this.mapH = 13;     // 纵向格子数量
        this.mapW = 24;     // 横向格子数量
        this.is8dir = true; // 是否8方向寻路
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.initMap();
    },
    onTouchMove(event){
        let pos = event.getLocation();
        let x = Math.floor(pos.x / (this._gridW + 2));
        let y = Math.floor(pos.y / (this._gridH + 2));
        if (this.gridsList[x][y].type == 0){
            this.gridsList[x][y].type = -1;
            this.draw(x, y, cc.Color.RED);
        }
        cc.log(x + "," + y);
    },
    onTouchEnd(){
        // 开始寻路
        this.findPath(cc.v2(3, 8), cc.v2(19, 5));
    },
    initMap(){
        this.openList = [];
        this.closeList = [];
        this.path = [];
        // 初始化格子二维数组
        this.gridsList = new Array(this.mapW + 1);
        for (let col=0;col<this.gridsList.length; col++){
            this.gridsList[col] = new Array(this.mapH + 1);
        }
        this.map.clear();
        for (let col=0; col<= this.mapW; col++){
            for (let row=0; row<=this.mapH; row++){
                this.draw(col, row);
                this.addGrid(col, row, 0);
            }
        }
        // 设置起点和终点
        let startX = 3;
        let startY = 8;
        let endX = 19;
        let endY = 5;
        this.gridsList[startX][startY].type = 1;
        this.draw(startX, startY, cc.Color.YELLOW);
        this.gridsList[endX][endY].type = 2;
        this.draw(endX, endY, cc.Color.BLUE);
    },
    addGrid(x, y, type){
        let grid = new Grid();
        grid.x = x;
        grid.y = y;
        grid.type = type;
        this.gridsList[x][y] = grid;
    },
    _sortFunc(x, y){
        return x.f - y.f;
    },
    generatePath(grid){
        this.path.push(grid);
        while (grid.parent){
            grid = grid.parent;
            this.path.push(grid);
        }
        cc.log("path.length: " + this.path.length);
        for (let i=0; i<this.path.length; i++){
            // 起点终点不覆盖,方便看效果
            if (i!=0 && i!= this.path.length-1){
                let grid = this.path[i];
                this.draw(grid.x, grid.y, cc.Color.GREEN);
            }
        }
    },
    findPath(startPos, endPos){
        let startGrid = this.gridsList[startPos.x][startPos.y];
        let endGrid = this.gridsList[endPos.x][endPos.y];
        this.openList.push(startGrid);
        let curGrid = this.openList[0];
        while (this.openList.length > 0 && curGrid.type != 2){
            // 每次都取出f值最小的节点进行查找
            curGrid = this.openList[0];
            if (curGrid.type == 2){
                cc.log("find path success.");
                this.generatePath(curGrid);
                return;
            }
            for(let i=-1; i<=1; i++){
                for(let j=-1; j<=1; j++){
                    if (i !=0 || j != 0){
                        let col = curGrid.x + i;
                        let row = curGrid.y + j;
                        if (col >= 0 && row >= 0 && col <= this.mapW && row <= this.mapH
                            && this.gridsList[col][row].type != -1
                            && this.closeList.indexOf(this.gridsList[col][row]) < 0){
                                if (this.is8dir){
                                    // 8方向 斜向走动时要考虑相邻的是不是障碍物
                                    if (this.gridsList[col-i][row].type == -1 || this.gridsList[col][row-j].type == -1){
                                        continue;
                                    }
                                } else {
                                    // 四方形行走
                                    if (Math.abs(i) == Math.abs(j)){
                                        continue;
                                    }
                                }
                                // 计算g值
                                let g = curGrid.g + parseInt(Math.sqrt(Math.pow(i*10,2) + Math.pow(j*10,2)));
                                if (this.gridsList[col][row].g == 0 || this.gridsList[col][row].g > g){
                                    this.gridsList[col][row].g = g;
                                    // 更新父节点
                                    this.gridsList[col][row].parent = curGrid;
                                }
                                // 计算h值 manhattan估算法
                                this.gridsList[col][row].h = Math.abs(endPos.x - col) + Math.abs(endPos.y - row);
                                // 更新f值
                                this.gridsList[col][row].f = this.gridsList[col][row].g + this.gridsList[col][row].h;
                                // 如果不在开放列表里则添加到开放列表里
                                if (this.openList.indexOf(this.gridsList[col][row]) < 0){
                                    this.openList.push(this.gridsList[col][row]);
                                }
                                // // 重新按照f值排序(升序排列)
                                // this.openList.sort(this._sortFunc);
                        }
                    }
                }
            }
            // 遍历完四周节点后把当前节点加入关闭列表
            this.closeList.push(curGrid);
            // 从开放列表把当前节点移除
            this.openList.splice(this.openList.indexOf(curGrid), 1);
            if (this.openList.length <= 0){
                cc.log("find path failed.");
            }
            // 重新按照f值排序(升序排列)
            this.openList.sort(this._sortFunc);
        }
    },
    draw(col, row, color){
        color = color != undefined ? color : cc.Color.GRAY;
        this.map.fillColor = color;
        let posX = 2 + col * (this._gridW + 2);
        let posY = 2 + row * (this._gridH + 2);
        this.map.fillRect(posX,posY,this._gridW,this._gridH);
    }
});

20200417214834892.gif


相关文章
|
算法 定位技术
Threejs中使用A*算法寻路导航,Threejs室内室外地图导航
Threejs中使用A*算法寻路导航,Threejs室内室外地图导航
1079 0
Threejs中使用A*算法寻路导航,Threejs室内室外地图导航
|
2月前
|
算法
互动游戏解决遇到问题之基于射线投射寻路算法的问题如何解决
互动游戏解决遇到问题之基于射线投射寻路算法的问题如何解决
|
3月前
|
Dart 算法 数据可视化
用flutter实现五种寻路算法的可视化效果,快来看看!
半年前我写了一篇有关排序算法可视化的文章,挺有意思,还被张风捷特烈-张老师收录进了FlutterUnit,今天让我们再来做一个有关寻路算法的可视化效果吧!
|
5月前
|
算法 定位技术 图形学
unity3d寻路算法
unity3d寻路算法
112 8
|
算法
Threejs中使用astar(A*)算法寻路导航,Threejs寻路定位导航
Threejs中使用astar(A*)算法寻路导航,Threejs寻路定位导航
617 0
Threejs中使用astar(A*)算法寻路导航,Threejs寻路定位导航
|
算法 定位技术
“ 探索迷局:解密广度寻路算法 “(二)
“ 探索迷局:解密广度寻路算法 “
|
存储 算法 定位技术
“ 探索迷局:解密广度寻路算法 “(一)
“ 探索迷局:解密广度寻路算法 “
|
算法 关系型数据库 MySQL
数据结构与算法——深度寻路算法
📖作者介绍:22级树莓人(计算机专业),热爱编程<目前在c++阶段,因为最近参加新星计划算法赛道(白佬),所以加快了脚步,果然急迫感会增加动力>——目标Windows,MySQL,Qt,数据结构与算法,Linux,多线程,会持续分享学习成果和小项目的 📖作者主页:king&南星 📖专栏链接:数据结构 🎉欢迎各位→点赞👏 + 收藏💞 + 留言🔔​ 💬总结:希望你看完之后,能对你有所帮助,不足请指正!共同学习交流 🐾 ———————————————— 版权声明:本文为CSDN博主「热爱编程的小K」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
|
存储 人工智能 算法
Unity 实现A* 寻路算法
Unity 实现A* 寻路算法
386 2
Unity 实现A* 寻路算法
|
人工智能 算法 安全
游戏人工智能——A*寻路算法实践
A*寻路算法实践 一、题目背景 随着多媒体设备、虚拟现实、增强现实、物联网等技术的飞跃发展,计算速度与存储容量的日益提高以及相关软件的研究取得长足进步,人工智能的应用得以进一步推广发展起来。地图寻径问题是人工智能技术的一个重要领域。在网络游戏中,寻径问题必须考虑多方面的因素,比如游戏地图中文件结构和起点与目标点之间是否可以连通以及游戏运行时运行内存资源占用、可扩展更新性、安全程度等。长久以来,游戏开发者在开发过程中为了实现这些绞尽脑汁。 在搜索寻径问题中,Dijkstra算法是目前许多工程解决最短路径
360 0
游戏人工智能——A*寻路算法实践
下一篇
无影云桌面