예제 #1
0
 public bool IsTilePassable(Vector2Int tileCoords)
 {
     return(Tilemapper.IsGridPassableAtCoordinate(gameWorldBaseGrid, tileCoords));
 }
예제 #2
0
        public MovementPath FindShortestPath(Vector2Int source, List <Vector2Int> validDestinations, PathOptions pathOptions)
        {
            SortedList <double, Vector2Int>     toVisitList    = new SortedList <double, Vector2Int>(new DuplicateKeyComparer <double>());
            HashSet <Vector2Int>                visited        = new HashSet <Vector2Int>();
            Dictionary <Vector2Int, Vector2Int> originPosition = new Dictionary <Vector2Int, Vector2Int>();

            //instantiate the to-visit list
            toVisitList.Add(0, source);


            while ((toVisitList.Count > 0) && (!visited.Intersect(validDestinations).Any()))
            {
                var currentPosition = toVisitList.Values[0];
                var currentCost     = toVisitList.Keys[0];
                toVisitList.RemoveAt(0);
//                Debug.Log("visiting " + currentPosition);

                if (visited.Contains(currentPosition))
                {
                    continue;
                }
                visited.Add(currentPosition);
                var neighbours = GetAllNeighbours(currentPosition);
                foreach (Vector2Int neighbour in neighbours)
                {
                    if (visited.Contains(neighbour) || toVisitList.ContainsValue(neighbour))
                    {
                        continue;
                    }
                    if (!Tilemapper.IsGridPassableAtCoordinate(_gameWorld, neighbour))
                    {
                        continue;
                    }
                    originPosition.Add(neighbour, currentPosition);
                    double newCost = currentCost + CalculateCost(currentPosition, neighbour, 1f);
                    toVisitList.Add(newCost, neighbour);
                }
            }

            if (visited.Intersect(validDestinations).Any())
            {
                //build the return path
                List <Vector2Int> listOfCoords = new List <Vector2Int>();
                var currentPosition            = visited.Intersect(validDestinations).First();
                var timeout = 0;
                while (currentPosition != source || timeout > 5000)
                {
                    listOfCoords.Insert(0, currentPosition);
                    currentPosition = originPosition[currentPosition];
                    timeout++;
                }
                listOfCoords.Insert(0, currentPosition);

                if (timeout >= 5000)
                {
                    throw new TimeoutException("Finding source of path timed out -- unexpected!");
                }
                return(new MovementPath(listOfCoords.Select(it => it.ToVec3()).ToList()));
            }
            return(null);
        }