Пример #1
0
 public MazeWall(int size)
 {
     row      = new bool[size, size + 1];
     col      = new bool[size, size + 1];
     wallarea = new WallArea(size, size);
     ClosedAllWall();
 }
Пример #2
0
 public MazeWall(int rowlength, int collength)
 {
     row      = new bool[rowlength, collength + 1];
     col      = new bool[collength, rowlength + 1];
     wallarea = new WallArea(rowlength, collength);
     ClosedAllWall();
 }
Пример #3
0
        /// <summary>
        /// 把区域附近未打通的墙加入维护的墙列表
        /// </summary>
        /// <param name="area"></param>
        private static void AddNerabyWall(WallArea area)
        {
            List <WallArea> areas = GetNearbyArea(area);

            for (int i = 0; i < areas.Count; ++i)
            {
                walls.Add(new KeyValuePair <WallArea, WallArea>(
                              area, areas[i]
                              ));
            }
        }
Пример #4
0
        /// <summary>
        /// 打通区域
        /// </summary>
        public void OpenArea(WallArea area1, WallArea area2)
        {
            if(area1.rowLength == area2.rowLength)
            {
                row[area1.rowLength, Mathf.Max(area1.colLength, area2.colLength)] = false;
                NotificationCenter<distroyWall>.Get().DispatchEvent("distroyWall", 
                    new distroyWall(wall.row, area1.rowLength, Mathf.Max(area1.colLength, area2.colLength)));
                return;
            }

            if (area1.colLength == area2.colLength)
            {
                col[area1.colLength, Mathf.Max(area1.rowLength, area2.rowLength)] = false;
                NotificationCenter<distroyWall>.Get().DispatchEvent("distroyWall",
                    new distroyWall(wall.col, area1.colLength, Mathf.Max(area1.rowLength, area2.rowLength)));
            }
        }
Пример #5
0
        /// <summary>
        /// 检测附近区域是否访问过,并随机选择其中一个附件区域打通
        /// </summary>
        private static WallArea?CheckNearby(WallArea area)
        {
            //获得未访问的邻接区域
            List <WallArea> nerabyAreas = GetNearbyArea(area);

            if (0 == nerabyAreas.Count)
            {
                return(null);
            }

            //打通的新区域
            ++count;
            WallArea newarea = nerabyAreas[Random.Range(0, nerabyAreas.Count - 1)];

            mazeWall.OpenArea(area, newarea);
            _stack.Push(newarea);

            return(newarea);
        }
Пример #6
0
        public static MazeWall Generate(MazeWall wall)
        {
            walls.Clear();

            //封闭全部墙壁
            mazeWall.ClosedAllWall();

            //随机选择一个开始区域
            WallArea fistArea = RandChooseBeginArea();

            AddNerabyWall(fistArea);

            while (walls.Count > 0)
            {
                int randomIndex = Random.Range(0, walls.Count);
                var _wall       = walls[randomIndex];
                if (checkWall(_wall))
                {
                    if (checkArea(_wall.Key))
                    {
                        AddNerabyWall(_wall.Key);
                    }
                    if (checkArea(_wall.Key))
                    {
                        AddNerabyWall(_wall.Value);
                    }
                    mazeWall.OpenArea(_wall.Key, _wall.Value);
                }
                else
                {
                    walls.RemoveAt(randomIndex);
                }
            }

            //随机选择迷宫起点终点
            mazeWall.RandomOpenStartAndPoint();
            mazeWall.RandomOpenStartAndPoint();

            return(mazeWall);
        }
Пример #7
0
        public static MazeWall Generate(MazeWall wall)
        {
            mazeWall = wall;
            count    = 0;
            _stack.Clear();

            //封闭全部墙壁
            mazeWall.ClosedAllWall();

            int maxCount = mazeWall.RowLength * mazeWall.ColLength;

            //随机选择一个开始区域
            WallArea fistArea = RandChooseBeginArea();

            ++count;
            _stack.Push(fistArea);

            WallArea?curArea = fistArea;

            while (count < maxCount)
            {
                if (null == curArea)
                {
                    //退回上一个区域
                    _stack.Pop();
                    curArea = _stack.Peek();
                    continue;
                }
                curArea = CheckNearby((WallArea)curArea);
            }

            //随机选择迷宫起点终点
            mazeWall.RandomOpenStartAndPoint();
            mazeWall.RandomOpenStartAndPoint();

            return(mazeWall);
        }
Пример #8
0
        protected static List <WallArea> GetNearbyArea(WallArea area)
        {
            //获得未访问的邻接区域
            List <WallArea> nerabyAreas = new List <WallArea>();

            if (area.rowLength > 0)
            {
                if (!mazeWall[area.rowLength - 1, area.colLength])
                {
                    nerabyAreas.Add(new WallArea(area.rowLength - 1, area.colLength));
                }
            }
            if (area.rowLength < mazeWall.RowLength - 1)
            {
                if (!mazeWall[area.rowLength + 1, area.colLength])
                {
                    nerabyAreas.Add(new WallArea(area.rowLength + 1, area.colLength));
                }
            }
            if (area.colLength > 0)
            {
                if (!mazeWall[area.rowLength, area.colLength - 1])
                {
                    nerabyAreas.Add(new WallArea(area.rowLength, area.colLength - 1));
                }
            }
            if (area.colLength < mazeWall.ColLength - 1)
            {
                if (!mazeWall[area.rowLength, area.colLength + 1])
                {
                    nerabyAreas.Add(new WallArea(area.rowLength, area.colLength + 1));
                }
            }

            return(nerabyAreas);
        }
        /// <summary>
        /// 分割
        /// </summary>
        private static void RecursiveDivision(
            int rowstart,
            int rowend,
            int colstart,
            int colend)
        {
            if (rowend < rowstart)
            {
                return;
            }
            if (colend < colstart)
            {
                return;
            }

            //条件不允许再分成四块了
            if (1 >= rowend - rowstart)
            {
                WallArea preArea = new WallArea(rowstart, colstart);
                for (int i = colstart + 1; i < colend; ++i)
                {
                    WallArea curArea = new WallArea(rowstart, i);
                    mazeWall.OpenArea(preArea, curArea);
                    preArea = curArea;
                }
                return;
            }
            if (1 >= colend - colstart)
            {
                WallArea preArea = new WallArea(rowstart, colstart);
                for (int i = rowstart + 1; i < rowend; ++i)
                {
                    WallArea curArea = new WallArea(i, colstart);
                    mazeWall.OpenArea(preArea, curArea);
                    preArea = curArea;
                }
                return;
            }

            //分块
            int randomRow = UnityEngine.Random.Range(rowstart + 1, rowend);
            int randomCol = UnityEngine.Random.Range(colstart + 1, colend);

            //随机找到四个分界线上四个洞
            List <KeyValuePair <WallArea, WallArea> > FourHoles = new List <KeyValuePair <WallArea, WallArea> >();
            int index = Random.Range(colstart, randomCol);

            FourHoles.Add(new KeyValuePair <WallArea, WallArea>(
                              new WallArea(randomRow - 1, index),
                              new WallArea(randomRow, index)
                              ));
            index = Random.Range(randomCol, colend);
            FourHoles.Add(new KeyValuePair <WallArea, WallArea>(
                              new WallArea(randomRow - 1, index),
                              new WallArea(randomRow, index)
                              ));
            index = Random.Range(rowstart, randomRow);
            FourHoles.Add(new KeyValuePair <WallArea, WallArea>(
                              new WallArea(index, randomCol - 1),
                              new WallArea(index, randomCol)
                              ));
            index = Random.Range(randomRow, rowend);
            FourHoles.Add(new KeyValuePair <WallArea, WallArea>(
                              new WallArea(index, randomCol - 1),
                              new WallArea(index, randomCol)
                              ));

            //打通其中三个洞
            index = Random.Range(0, 4);
            FourHoles.RemoveAt(index);
            for (int i = 0; i < 3; ++i)
            {
                mazeWall.OpenArea(FourHoles[i].Key, FourHoles[i].Value);
            }


            //递归分割
            RecursiveDivision(rowstart, randomRow, colstart, randomCol);
            RecursiveDivision(randomRow, rowend, colstart, randomCol);
            RecursiveDivision(rowstart, randomRow, randomCol, colend);
            RecursiveDivision(randomRow, rowend, randomCol, colend);
        }
Пример #10
0
 /// <summary>
 /// 判断区域是否被打通
 /// </summary>
 /// <param name="area"></param>
 /// <returns></returns>
 protected static bool checkArea(WallArea area)
 {
     return(mazeWall[area.rowLength, area.colLength]);
 }
Пример #11
0
 /// <summary>
 /// 计算并查集的索引
 /// </summary>
 /// <param name="area"></param>
 /// <returns></returns>
 private static int GetUnionSetIndex(WallArea area)
 {
     return(area.colLength * mazeWall.ColLength +
            area.rowLength);
 }