Ejemplo n.º 1
0
        /// <summary>
        /// 在全地图内获取没有被占用的cell
        /// </summary>
        /// <param name="size">占用大小</param>
        /// <param name="centerCell">占用的中心点</param>
        /// <param name="allCells">所有占用的格子</param>
        /// <returns></returns>
        public HexagonsWithCenter FindClearCells(int size)
        {
            if (size <= 1)
            {
                HexCell centerCell = FindClearCell();
                if (centerCell != null)
                {
                    return(new HexagonsWithCenter(centerCell, new List <HexCell>()
                    {
                        centerCell
                    }));
                }
                else
                {
                    return(null);
                }
            }

            int ringRadius = size - 1;

            foreach (int index in GetRandomCellIndexCache())
            {
                HexagonsWithCenter result = GetSpiralRingsWithSameHeight(cells[index], ringRadius);
                if (result != null)
                {
                    return(null);
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 在指定的范围内获取一个Cell
        /// </summary>
        /// <param name="range">0是代表只选取center,小于0是代表全范围,只是计算center这点,size大于一时可能会超出Range的范围</param>
        ///<param name="random">是否全部随机,如果不是就从近到远获取</param>
        ///<param name="exceptRange">排除的范围,0表示排除center,只是排除center这点,并不会影响到全范围的cell</param>
        public HexagonsWithCenter FindClearCellsInRange(int size, HexCell center, int range = -1, int exceptRange = -1, bool random = false)
        {
            if (size <= 1)
            {
                HexCell cell = FindClearCellInRange(center, range, exceptRange, random);
                if (cell != null)
                {
                    return(new HexagonsWithCenter(cell));
                }
                else
                {
                    return(null);
                }
            }

            if (range == 0)
            {
                if (center.isTaken)
                {
                    return(GetSpiralRingsWithSameHeight(center, size - 1));
                }
                else
                {
                    return(null);
                }
            }

            if (random)
            {
                //从全范围内随机
                List <HexCell> tempList = null;
                if (exceptRange >= 0)
                {
                    //被除队的范围
                    List <HexCell> exceptTempList = center.GetSpiralRings(exceptRange);
                    if (range < 0)
                    {
                        tempList = cells.Except(exceptTempList).ToList <HexCell>();
                    }
                    else
                    {
                        tempList = center.GetSpiralRings(range).Except(exceptTempList).ToList <HexCell>();
                    }
                }
                else
                {
                    if (range < 0)
                    {
                        tempList = new List <HexCell>(cells);
                    }
                    else
                    {
                        tempList = center.GetSpiralRings(range);
                    }
                }

                Untility.Tool.RandSortList <HexCell>(ref tempList);
                foreach (HexCell tempCell in tempList)
                {
                    if (!tempCell.isTaken)
                    {
                        HexagonsWithCenter result = GetSpiralRingsWithSameHeight(tempCell, size - 1);
                        if (result != null)
                        {
                            return(result);
                        }
                    }
                }
                return(null);
            }
            else
            {
                //从近到远
                int i        = exceptRange >= 0 ? exceptRange + 1 : 0;
                int maxRange = range > 0 ? range : int.MaxValue;
                for (; i <= maxRange; i++)
                {
                    List <HexCell> tempList = center.GetRings(i);
                    if (tempList.Count <= 0)
                    {
                        break;
                    }
                    Untility.Tool.RandSortList <HexCell>(ref tempList);
                    foreach (HexCell tempCell in tempList)
                    {
                        if (!tempCell.isTaken)
                        {
                            HexagonsWithCenter result = GetSpiralRingsWithSameHeight(tempCell, size - 1);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }
                return(null);
            }
        }