Exemplo n.º 1
0
        /// <summary>
        /// Calculate the pathfinding for the gesture
        /// </summary>
        public static NavNode CalculatePath(Vector3 from, Gesture.Gesture gesture)
        {
            if (EventSystem.current.IsPointerOverGameObject(-1) || EventSystem.current.IsPointerOverGameObject(0))
            {
                return(null);
            }

            Ray        ray      = new Ray(from, Vector3.down);
            RaycastHit startHit = default(RaycastHit);

            if (Physics.Raycast(ray, out startHit, RAY_LENGTH, 1 << TERRAIN_LAYERMASK))
            {
                //Gets the player position on the grid
                if (startHit.collider.name == MobileAI.Instance.m_navMesh.name)
                {
                    gesture.cellsHitten.Insert(0, startHit.triangleIndex);
                }
                else
                {
                    return(null);
                }
            }

            //Store the last node path
            NavNode path = MobileAI.Instance.m_navMesh.PathFinding(gesture.cellsHitten[0], gesture.cellsHitten[1] << 1);

            for (int index = 1; index < gesture.cellsHitten.Count - 1;)
            {
                NavNode tmp      = null;
                int     tmpIndex = index;

                //Since the player may have clicked outside the bound area or over an object,
                //this code assured a path between the last valid location and the final location
                while (tmp == null)
                {
                    if (++tmpIndex >= gesture.cellsHitten.Count)
                    {
                        break;
                    }

                    tmp = MobileAI.Instance.m_navMesh.PathFinding(gesture.cellsHitten[index] << 1, gesture.cellsHitten[tmpIndex] << 1);
                }

                //If the algorithm found a possible path, then concatenate it
                if (tmp != null)
                {
                    path = NavNode.Concatenate(path, tmp);
                }

                //Skip all the invalid index.
                index = tmpIndex;
            }

            return(path);
        }