示例#1
0
    public MapVecAndPriority Dequeue()
    {
        MapVecAndPriority retT = list[0];

        list.RemoveAt(0);
        return(retT);
    }
示例#2
0
文件: MapVec.cs 项目: maousama/Assets
    public int CompareTo(object obj)
    {
        MapVecAndPriority mapVecAndCost = obj as MapVecAndPriority;

        return(Priority.CompareTo(mapVecAndCost.Priority));
    }
示例#3
0
 public void Enqueue(MapVecAndPriority item)
 {
     list.Add(item);
     list.Sort();
 }
示例#4
0
    public Stack <Vector3> SumPathFromChunk(MapChunk[,] roomChunk, Vector3 curVec, Vector3 goalVec, float debugLineStayTime = 0)
    {
        MapChunk startMapVec = SolveMapChunk(roomChunk, curVec);
        MapChunk goalMapVec  = SolveMapChunk(roomChunk, goalVec);
        //MapChunk startMapVec = roomChunk[(int)(Mathf.Round(curVec.x * 0.4f)), (int)(Mathf.Round(curVec.z * 0.4f))];
        //MapChunk goalMapVec = roomChunk[(int)(Mathf.Round(goalVec.x * 0.4f)), (int)(Mathf.Round(goalVec.z * 0.4f))];
        //Debug.Log("startMapVec:" + startMapVec.MapVector.X + "," + startMapVec.MapVector.Y + "  goalMapVec" + goalMapVec.MapVector.X + "," + goalMapVec.MapVector.Y);
        //bool inRange = JudgeMapVecInRoom(roomChunk, startMapVec.MapVector.X, startMapVec.MapVector.Y) && JudgeMapVecInRoom(roomChunk, goalMapVec.MapVector.X, goalMapVec.MapVector.Y);
        bool inRange = startMapVec != null && goalMapVec != null;

        //如果两个点都没有越界,并且两点都是可走空地则继续
        if (inRange)
        {
            bool canMove = (startMapVec.Weight != IBuilding.CannotWalkThrough && goalMapVec.Weight != IBuilding.CannotWalkThrough);
            if (canMove)
            {
                PriorityQueue mapVecAndCostPriorityQueue     = new PriorityQueue();
                Dictionary <MapVector, MapChunk> comeFromDic = new Dictionary <MapVector, MapChunk>();
                Dictionary <MapVector, float>    costDic     = new Dictionary <MapVector, float>();
                //起始点入队
                mapVecAndCostPriorityQueue.Enqueue(new MapVecAndPriority(startMapVec, 0));
                //初始化花费
                costDic.Add(startMapVec.MapVector, 1);
                //当优先队列不为空时候
                while (mapVecAndCostPriorityQueue.Count > 0)
                {
                    //取出第一个点(花费最小的点)
                    MapVecAndPriority curMapVecAndCost = mapVecAndCostPriorityQueue.Dequeue();
                    //遍历其周围没有越界的可走的点
                    for (int i = curMapVecAndCost.MapChunk.MapVector.X - 1; i < curMapVecAndCost.MapChunk.MapVector.X + 2; i++)
                    {
                        for (int j = curMapVecAndCost.MapChunk.MapVector.Y - 1; j < curMapVecAndCost.MapChunk.MapVector.Y + 2; j++)
                        {
                            if (JudgeMapVecInRoom(roomChunk, i, j) && !(curMapVecAndCost.MapChunk.MapVector.X == i && curMapVecAndCost.MapChunk.MapVector.Y == j))
                            {
                                if (roomChunk[i, j].Weight != IBuilding.CannotWalkThrough)
                                {
                                    //根据体型判断是否能斜向通过
                                    if (scaleType == Small)
                                    {
                                        //计算当前花费
                                        float newCost  = costDic[curMapVecAndCost.MapChunk.MapVector] + roomChunk[i, j].Weight;
                                        float priority = ManhattanAlgorithm(newCost, roomChunk[i, j], goalMapVec);
                                        if (i == goalMapVec.MapVector.X && j == goalMapVec.MapVector.Y)
                                        {
                                            //设置终点的来源
                                            comeFromDic.Add(roomChunk[i, j].MapVector, curMapVecAndCost.MapChunk);
                                            //资源释放
                                            mapVecAndCostPriorityQueue.Dispose();
                                            costDic = null;

                                            return(GetPathsFromDic(comeFromDic, startMapVec, goalMapVec, debugLineStayTime));
                                        }
                                        //如果邻居已经在花费列表
                                        if (costDic.ContainsKey(roomChunk[i, j].MapVector))
                                        {
                                            //如果花费小于列表中的则更新Cost及ComeFrom
                                            if (newCost < costDic[roomChunk[i, j].MapVector])
                                            {
                                                costDic[roomChunk[i, j].MapVector]     = newCost;
                                                comeFromDic[roomChunk[i, j].MapVector] = curMapVecAndCost.MapChunk;
                                                mapVecAndCostPriorityQueue.Enqueue(new MapVecAndPriority(roomChunk[i, j], priority));
                                            }
                                        }
                                        //如果不包含
                                        else
                                        {
                                            costDic.Add(roomChunk[i, j].MapVector, newCost);
                                            comeFromDic.Add(roomChunk[i, j].MapVector, curMapVecAndCost.MapChunk);
                                            mapVecAndCostPriorityQueue.Enqueue(new MapVecAndPriority(roomChunk[i, j], priority));
                                        }
                                    }
                                    //如果体型大
                                    else
                                    {
                                        //通过if过滤掉
                                        if (i == curMapVecAndCost.MapChunk.MapVector.X || j == curMapVecAndCost.MapChunk.MapVector.Y)
                                        {
                                            //计算当前花费
                                            float newCost  = costDic[curMapVecAndCost.MapChunk.MapVector] + roomChunk[i, j].Weight;
                                            float priority = ManhattanAlgorithm(newCost, roomChunk[i, j], goalMapVec);
                                            if (i == goalMapVec.MapVector.X && j == goalMapVec.MapVector.Y)
                                            {
                                                //设置终点的来源
                                                comeFromDic.Add(roomChunk[i, j].MapVector, curMapVecAndCost.MapChunk);
                                                //资源释放
                                                mapVecAndCostPriorityQueue.Dispose();
                                                costDic = null;

                                                return(GetPathsFromDic(comeFromDic, startMapVec, goalMapVec, debugLineStayTime));
                                            }
                                            //如果邻居已经在花费列表
                                            if (costDic.ContainsKey(roomChunk[i, j].MapVector))
                                            {
                                                //如果花费小于列表中的则更新Cost及ComeFrom
                                                if (newCost < costDic[roomChunk[i, j].MapVector])
                                                {
                                                    costDic[roomChunk[i, j].MapVector]     = newCost;
                                                    comeFromDic[roomChunk[i, j].MapVector] = curMapVecAndCost.MapChunk;
                                                    mapVecAndCostPriorityQueue.Enqueue(new MapVecAndPriority(roomChunk[i, j], priority));
                                                }
                                            }
                                            //如果不包含
                                            else
                                            {
                                                costDic.Add(roomChunk[i, j].MapVector, newCost);
                                                comeFromDic.Add(roomChunk[i, j].MapVector, curMapVecAndCost.MapChunk);
                                                mapVecAndCostPriorityQueue.Enqueue(new MapVecAndPriority(roomChunk[i, j], priority));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return(null);
            }
            else
            {
                Debug.Log("起始点或终点不可走");
                return(null);
            }
        }
        //如果越界则直接跳出并打印信息
        else
        {
            if (!inRange)
            {
                Debug.Log("起始点或终点产生越界");
            }
            return(null);
        }
    }