Пример #1
0
        //查找一定范围内的敌人
        public List <BattleUnit> SearchBattleUnits(GridUnit centerGridUnit, int range)
        {
            List <BattleUnit> battleUnits = new List <BattleUnit>();
            BattleMap         battleMap   = centerGridUnit.battleMap;

            bool[,] flag = new bool[battleMap.mapWidth, battleMap.mapHeight];
            flag[centerGridUnit.column, centerGridUnit.row] = true;
            Queue <GridUnit> checkList   = new Queue <GridUnit>();
            GridUnit         currentUnit = centerGridUnit;

            checkList.Enqueue(currentUnit);
            while (checkList.Count > 0)
            {
                currentUnit = checkList.Dequeue();
                List <GridUnit> gridUnitList = battleMap.GetNearbyGrid(currentUnit);
                foreach (GridUnit gridUnit in gridUnitList)
                {
                    if (range < centerGridUnit.Distance(gridUnit))
                    {
                        continue;
                    }
                    if (!flag[gridUnit.column, gridUnit.row])
                    {
                        flag[gridUnit.column, gridUnit.row] = true;
                        if (gridUnit.battleUnit != null)
                        {
                            battleUnits.Add(gridUnit.battleUnit);
                        }
                        checkList.Enqueue(gridUnit);
                    }
                }
            }
            return(battleUnits);
        }
Пример #2
0
        //查找所有可以到达的格子
        public List <GridUnit> GetAccessibleGrids(BattleUnit battleUnit, BattleMap battleMap)
        {
            List <GridUnit> AccessibleGrids = new List <GridUnit>();

            bool[,] flag = new bool[battleMap.mapWidth, battleMap.mapHeight];
            flag[battleUnit.mapGrid.column, battleUnit.mapGrid.row] = true;
            Queue <PathTraceData> checkList   = new Queue <PathTraceData>();
            PathTraceData         currentUnit = new PathTraceData(battleUnit.battleUnitAttribute.mobility, battleUnit.mapGrid);

            checkList.Enqueue(currentUnit);
            while (checkList.Count > 0)
            {
                currentUnit = checkList.Dequeue();
                List <GridUnit> gridUnitList = battleMap.GetNearbyGrid(currentUnit.currentGridUnit);
                foreach (GridUnit gridUnit in gridUnitList)
                {
                    float MovePointRemainder = currentUnit.MovePointRemainder - currentUnit.currentGridUnit.m_GridAttribute.m_CrossCost;
                    //绝对障碍不可通过
                    if (gridUnit.GridType == GridType.Obstacle)
                    {
                        continue;
                    }
                    //不能跳过去,高度太高
                    if (gridUnit.m_GridAttribute.m_Height - currentUnit.currentGridUnit.m_GridAttribute.m_Height > battleUnit.battleUnitAttribute.springPower)
                    {
                        continue;
                    }
                    //体积太大穿不过去
                    if (gridUnit.m_GridAttribute.m_MaxPassVolume < battleUnit.battleUnitAttribute.volume)
                    {
                        continue;
                    }
                    //剩余移动点数不足
                    if (MovePointRemainder < 0)
                    {
                        continue;
                    }
                    //有敌人挡着(暂时还没判是否是友方)
                    if (gridUnit.battleUnit != null && gridUnit.battleUnit.CanAction && !gridUnit.NavigationPassable)
                    {
                        continue;
                    }

                    if (!flag[gridUnit.column, gridUnit.row])
                    {
                        flag[gridUnit.column, gridUnit.row] = true;
                        AccessibleGrids.Add(gridUnit);
                        checkList.Enqueue(new PathTraceData(MovePointRemainder, gridUnit));
                    }
                }
            }

            return(AccessibleGrids);
        }
Пример #3
0
        //寻找某点到另一点的路径
        public bool NewNavigate(
            BattleUnit battleUnit,
            BattleMap battleMap,
            GridUnit from,
            GridUnit to,
            List <GridUnit> path,
            int stopDistance = 0)
        {
            PathTraceData[,] flag = new PathTraceData[battleMap.mapWidth, battleMap.mapHeight];
            PriorityQueue <PathTraceData> checkList = new PriorityQueue <PathTraceData>();
            PathTraceData currentUnit = new PathTraceData(battleUnit.battleUnitAttribute.mobility, from);

            flag[from.column, from.row] = currentUnit;
            bool isFind = false;

            checkList.Push(currentUnit);
            while (checkList.Count > 0)
            {
                currentUnit = checkList.Pop();
                if (currentUnit.currentGridUnit.Distance(to) <= stopDistance)
                {
                    isFind = true;
                    break;
                }
                List <GridUnit> gridUnitList = battleMap.GetNearbyGrid(currentUnit.currentGridUnit);
                foreach (GridUnit gridUnit in gridUnitList)
                {
                    float MovePointRemainder = currentUnit.MovePointRemainder - currentUnit.currentGridUnit.m_GridAttribute.m_CrossCost;
                    //绝对障碍不可通过
                    if (gridUnit.GridType == GridType.Obstacle)
                    {
                        continue;
                    }
                    //不能跳过去,高度太高
                    if (gridUnit.m_GridAttribute.m_Height - currentUnit.currentGridUnit.m_GridAttribute.m_Height > battleUnit.battleUnitAttribute.springPower)
                    {
                        continue;
                    }
                    //体积太大穿不过去
                    if (gridUnit.m_GridAttribute.m_MaxPassVolume < battleUnit.battleUnitAttribute.volume)
                    {
                        continue;
                    }
                    //剩余移动点数不足
                    //if (MovePointRemainder <= 0) continue;
                    //有敌人挡着(暂时还没判是否是友方)
                    if (gridUnit.battleUnit != null && gridUnit.battleUnit.CanAction && !gridUnit.NavigationPassable)
                    {
                        continue;
                    }

                    if (flag[gridUnit.column, gridUnit.row] == null || flag[gridUnit.column, gridUnit.row].MovePointRemainder < MovePointRemainder)
                    {
                        flag[gridUnit.column, gridUnit.row] = currentUnit;
                        checkList.Push(new PathTraceData(MovePointRemainder, gridUnit));
                    }
                }
            }

            if (!isFind)
            {
                return(false);
            }
            path.Clear();
            Stack <GridUnit> temp = new Stack <GridUnit>();

            while (!currentUnit.currentGridUnit.Equals(from))
            {
                if (currentUnit.MovePointRemainder >= 0)
                {
                    temp.Push(currentUnit.currentGridUnit);
                }
                currentUnit = flag[currentUnit.currentGridUnit.column, currentUnit.currentGridUnit.row];
            }
            while (temp.Count != 0)
            {
                path.Add(temp.Pop());
            }
            return(true);
        }