`
hereson
  • 浏览: 1454198 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

简单区块寻路程序

阅读更多

最近工作忙,没什么时间写新东西,翻点以前的东西出来,呵呵
这是以前准备做MMO的时候搞的一些测试,当时准备自己写A*的,不过这个只是简单的,没有智能的,两点一线的,具体算法自己也忘了,哈哈......
Flash动画


下面是算法的具体代码,里面有注释,我这里不说了,无非就是先算一下竖的路能不能走,然后再算一下横的路能不能,再把所有的点存起来放在数组里....然后返回结果,呵呵
程序代码 程序代码

/**
* ...
* @author Default
* @version 0.1
*/

package soda {


    public class FindPath 
    {
        //地图数组
        private var map:Array;
        //路径数组
        private var pathX:Array;
        private var pathY:Array;
        private var result:Object;
        //存放已经寻找到的路
        private var findResult:Object;
        //平移路径所花费的代价
        private var titleWidth:int = 10;
        //斜移路径所花费的代价
        private var titleTilted:int = 14;
        
        public function FindPath()
        {
            pathX = new Array();
            pathY = new Array();
            findResult = new Object();
            result = new Object();
        }
        
        /**
        * 设置一个寻找路径的数组
        * @param    map
        * @return
        */
        public function setMap(map:Array):void
        {
            this.map = map;
        }
        /**
         * 计算该点是否可走
         * @param    endX
         * @param    endY
         * @return 一个布尔值
         */
        public function isWalk(endX:int,endY:int):Boolean
        {
            if(map[endY][endX] == 1)
            {
                return true;
            }
            return false;
        }
        /**
         * 寻找一条可走的路径
         * @param    startX
         * @param    startY
         * @param    endX
         * @param    endY
         * @return 返回寻找到的最适合的路径数组
         */
        public function findPath(startX:int,startY:int,endX:int,endY:int):Object
        {
            //x坐标的差距
            var tempX:int = Math.abs(startX - endX);
            //y坐标的差距
            var tempY:int = Math.abs(startY - endY);
            pathX = new Array();
            pathY = new Array();
            if(endX > startX)
            {
                //检查x位置
                //var xx:int = 0;
                var yy:int = startY;
                for(var i:int = startX + 1; i <= endX; i++)
                {
                    //xx = i;
                    if(map[yy][i] != 1)
                    {
                        //break;
                        pathX.unshift(i - 1);
                        yy++;
                        i--;
                        pathY.unshift(yy);
                    }
                    else
                    {
                        pathX.unshift(i);
                        pathY.unshift(yy);
                    }
                }
                if(yy > endY)
                {
                    //检查y位置
                    for(var i:int = yy; i >= endY; i--)
                    {
                        if(map[i][endX] != 1)
                        {
                            break;
                        }
                        else
                        {
                            pathX.unshift(endX);
                            pathY.unshift(i);
                        }
                    }
                }
                else
                {
                    //检查y位置
                    for(var i:int = yy + 1; i <= endY; i++)
                    {
                        if(map[i][endX] != 1)
                        {
                            break;
                        }
                        else
                        {
                            pathX.unshift(endX);
                            pathY.unshift(i);
                        }
                    }
                }
            }
            result.x = pathX;
            result.y = pathY;
            return result;
        }
        //检查纵向的数组是否可通行
        private function vertical(start:int, end:int, variable:int):void
        {
            //检查y位置
            for(var i:int = start; i <= end; i = i + variable)
            {
                if(map[i][end] != 1)
                {
                    break;
                }
                else
                {
                    pathX.unshift(end);
                    pathY.unshift(i);
                }
            }
        }
    }
    
}




下载文件 点击下载源代码



文章来自: 本站原创
引用通告地址: http://www.sujun.org/trackback.asp?tbID=26

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics