Exemplo n.º 1
0
        public void CreateAstarPath(AStarAgent agent, TSVector start, TSVector target, GridMap map, int curPosIdx)
        {
            if (agent._asm == null)
            {
                AStarMachine.AStarMachine asm  = _pathPool.New();
                GridAStarGoal             goal = _goalPool.New();
                goal.Ini(agent);

                GridAStarStorage storage = _storagePool.New();
                storage.Ini();
                asm.Ini(agent, goal, storage, map);

                agent._asm = asm;
                StartFindingPath(asm, start, target, false, curPosIdx);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// three step:first step,find fast path,maybe not a valid path navigating to target position
        /// </summary>
        /// <param name="agent"></param>
        /// <param name="start"></param>
        /// <param name="target"></param>
        /// <param name="mapId"></param>
        /// <param name="isTried">is tried to find quick path and finished </param>
        /// <returns></returns>
        public EPathReturnStatus FindQuickPath(AgentBase agent, int revs, TSVector start, TSVector target, GridMap map, bool isTried)//, List<TSVector> vecPath
        {
            //GridMap map = GridMapManager.instance.GetMap(mapId);
            if (map.GetGridNodeId(start) == map.GetGridNodeId(target))
            {
                return(EPathReturnStatus.ok);
            }
            AStarAgent aAgent = ((AStarAgent)agent);

            AStarMachine.AStarMachine asm = isTried?null: aAgent._asm;
            bool bInqueue = asm != null && asm.IsWorking;

            if (asm == null)
            {
                asm = _pathPool.New();
                GridAStarGoal goal = _goalPool.New();
                goal.Ini(agent);

                GridAStarStorage storage = _storagePool.New();
                storage.Ini();
                asm.Ini(agent, goal, storage, map);
            }
            int curIdx = map.GetGridNodeId(aAgent._behaviour.position);

            StartFindingPath(asm, start, target, true, curIdx);

            if (asm.IsWorking)
            {
                int   startIdx    = map.GetGridNodeId(start);
                int   tIdx        = map.GetGridNodeId(target);
                IInt2 pos         = map.GetGridPos(tIdx);
                bool  isReachable = map.IsExistingNeighbourWalkable(pos.x, pos.y, startIdx, tIdx, curIdx);
                if (!isTried && isReachable)
                {
                    ProcessFinishedPath(asm, EPathType.quickPath, false);//quick path is valid
                }

                if (!isReachable)
                {
                    aAgent._behaviour.AddUnReachablePos(map, tIdx);
                }
                if (!isTried && isReachable)//target possible reachable
                {
                    int sIdx = map.GetGridNodeId(aAgent._vecRunningPath[0]);
                    asm.StartSearch(sIdx, tIdx, false, start, _pathId++, startIdx);//
                    //if (!bInqueue)
                    //{
                    //    _pathQueue.Enqueue(asm);//find full path
                    //}
                    aAgent._asm = asm;
                }
                else//can't reach target
                {
                    asm.IsWorking = false;
                    // asm.Reset();
                    ClearFinishedPath(asm);

                    if (!isTried)
                    {
                        return(EPathReturnStatus.fail);

#if UNITY_EDITOR && !MULTI_THREAD
                        if (PathFindingManager.DEBUG)
                        {
                            UnityEngine.Debug.LogWarning("can't reach target:" + pos.x + "," + pos.y);
                        }
#endif
                    }
                    else
                    {
                        return(EPathReturnStatus.fail);
                    }
                }
                // asm._start=
            }
            else
            {
                if (isTried)//remove working asm
                {
                    if (aAgent._asm != null)
                    {
                        aAgent._asm.IsWorking = false;
                        ClearFinishedPath(aAgent._asm);
                    }
                    aAgent._asm = asm;
                }
                ProcessFinishedPath(asm, EPathType.quickPath);//quick path is valid
            }
            if (aAgent._vecRunningPath.Count > 0)
            {
                return(EPathReturnStatus.ok);
            }
            return(EPathReturnStatus.fail);//warning
        }