Пример #1
0
        /// <summary>
        /// 随机一个空格子,与其他的格子保持距离在density以上
        /// </summary>
        /// <param name="list">其他格子j</param>
        /// <param name="density">距离</param>
        /// <returns></returns>
        MGrid RandomGrid(List <MGrid> list, int density)
        {
            int chance = maxChance;

            MGrid defGrid = null;

            while (chance > 0)
            {
                --chance;

                int x = Random.Range(0, width);
                int y = Random.Range(0, height);

                var grid = grids[x, y];
                if (grid.GridType != GridType.Empty)//当前已是其他类型
                {
                    continue;
                }

                if (defGrid == null)
                {
                    defGrid = grid;
                }

                bool inBounds = false;//是否在范围内
                foreach (var item in list)
                {
                    if ((item.Position - grid.Position).AbsLen < density)
                    {
                        inBounds = true;
                        break;
                    }
                }

                if (!inBounds)
                {
                    return(grid);
                }
            }

            if (defGrid == null)
            {
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        if (grids[i, j].GridType == GridType.Empty)
                        {
                            defGrid = grids[i, j];
                            break;
                        }
                    }
                }
            }

            return(defGrid);
        }
Пример #2
0
        /// <summary>
        /// 连接房间和中间点
        /// </summary>
        void ConnectRoomField()
        {
            List <MGrid> fieldFlag = new List <MGrid>();
            int          minStep;

            INode[] path;
            foreach (var room in rooms)
            {
                minStep = int.MaxValue;
                path    = null;
                foreach (var field in fields)
                {
                    if (finder.Search(room, field, SearchLayer.PathFieldAndEmpty))
                    {
                        var list = finder.GetPath();
                        if (list.Count < minStep)
                        {
                            minStep = list.Count;
                            path    = list.ToArray();
                        }
                    }
                }

                if (path != null)
                {
                    INode pre = null;
                    foreach (var node in path)
                    {
                        if (node.GridType == GridType.Empty)
                        {
                            node.GridType = GridType.Path;
                        }

                        if (pre != null)
                        {
                            node.SetNeighbor(pre);
                            pre.SetNeighbor(node);
                        }
                        pre = node;
                    }

                    MGrid field = (MGrid)path[0];
                    fieldFlag.Add(field);
                }
            }

            foreach (var field in fields)
            {
                if (!fieldFlag.Contains(field))
                {
                    field.GridType = GridType.Empty;
                }
            }

            fields = fieldFlag;
        }