Esempio n. 1
0
        /// <summary>
        /// Finds a path from own position to given position in maze.
        /// </summary>
        /// <returns>The path.</returns>
        /// <param name="map">Map.</param>
        /// <param name="start">Start.</param>
        /// <param name="end">End.</param>
        /// <param name="solution">Solution.</param>
        /// <param name="allowDiagonals">If set to <c>true</c> allow diagonals.</param>
        public AStarSearch.SearchState FindPath(Vec3di end, List <Vec3di> solution, bool allowDiagonals)
        {
            solution.Clear();

            AStarSearch astarsearch = new AStarSearch(map, ownPosition, end, allowDiagonals);

            AStarSearch.SearchState SearchState;

            do
            {
                SearchState = astarsearch.SearchStep();
            }while(SearchState == AStarSearch.SearchState.SEARCHING);

            if (SearchState == AStarSearch.SearchState.SUCCEEDED)
            {
                GridPosition mapPos = astarsearch.GetSolutionStart();

                while (mapPos != null)
                {
                    solution.Add(mapPos.Position);

                    mapPos = astarsearch.GetSolutionNext();
                }
                ;

                // Once you're done with the solution you can free the nodes up
                astarsearch.Cleanup();
            }
            else if (SearchState == AStarSearch.SearchState.FAILED)
            {
                //Zentronic.Debug.LogError("search terminated. Goal state not found!");
            }

            return(SearchState);
        }