Exemple #1
0
 public HexCell(HexCoordinates pos, IDictionary <HexCoordinates, HexType> allCells)
 {
     Position  = pos;
     _allCells = allCells;
 }
Exemple #2
0
        public List <HexCoordinates> BuildPath(HexCoordinates startingPoint, HexCoordinates targetNode)
        {
            Dictionary <HexCoordinates?, float>           distance = new Dictionary <HexCoordinates?, float>();
            Dictionary <HexCoordinates?, HexCoordinates?> previous = new Dictionary <HexCoordinates?, HexCoordinates?>();
            List <HexCoordinates?> unvisited = new List <HexCoordinates?>();

            distance[startingPoint] = 0;
            previous[startingPoint] = null;
            // infinity for unreachable cells
            foreach (HexCoordinates coord in _fullMap._allCells.Keys)
            {
                if (!coord.Equals(startingPoint))
                {
                    distance[coord] = float.PositiveInfinity;
                    previous[coord] = null;
                }
                unvisited.Add(coord);
            }

            while (unvisited.Count > 0)
            {
                // unvisited node with shortest distance
                HexCoordinates?coord = null;
                foreach (var possibleCoord in unvisited)
                {
                    if (coord == null || distance[possibleCoord] < distance[coord])
                    {
                        coord = possibleCoord;
                    }
                }
                unvisited.Remove(coord);

                if (coord.Value.Equals(targetNode))
                {
                    break;
                }

                HexCell coordCell = _fullMap.GetCell(coord.Value);
                foreach (var neighbor in coordCell.AdjacentCells)
                {
                    float alt = distance[coord] + coordCell.DistanceTo(neighbor);
                    if (alt < distance[neighbor.Position])
                    {
                        distance[neighbor.Position] = alt;
                        previous[neighbor.Position] = coord;
                    }
                }
            }

            if (previous[targetNode] == null)
            {
                // No route to target
                return(null);
            }

            List <HexCoordinates> currentPath = new List <HexCoordinates>();

            HexCoordinates?current = targetNode;

            while (previous[current] != null)
            {
                currentPath.Add(current.Value);
                current = previous[current];
            }
            currentPath.Reverse();
            return(currentPath);
        }
Exemple #3
0
 private HexCell(HexCoordinates pos, HexType state, IDictionary <HexCoordinates, HexType> allCells)
 {
     Position  = pos;
     State     = state;
     _allCells = allCells;
 }
Exemple #4
0
 public HexCell GetCell(HexCoordinates startingPoint)
 {
     return(new HexCell(startingPoint, _allCells));
 }