Beispiel #1
0
 public HexPath GraphRoadSearch(HexCorner start, HexCorner end)
 {
     return(GraphSearch(start, end, (edgeStart, edgeEnd) =>
     {
         HexEdge edge = new HexEdge(edgeStart, edgeEnd);
         return board.Roads.ContainsKey(edge);
     }));
 }
Beispiel #2
0
        public static HexEdge GetNearestEdge(Vector3 coords)
        {
            HexCorner corner = HexCorner.GetNearestCorner(coords);

            HexCorner[] neighbors = HexCorner.GetNeighbors(corner);
            float[]     distances = new float[neighbors.Length];
            for (int i = 0; i < neighbors.Length; i++)
            {
                distances[i] = Vector3.Distance(coords, neighbors[i].ToLocalCoords());
            }
            return(new HexEdge(corner, neighbors[Array.IndexOf(distances, Mathf.Min(distances))]));
        }
Beispiel #3
0
        public HexPath GraphSearch(HexCorner start, HexCorner end, Func <HexCorner, HexCorner, bool> validEdgeFunc = null)
        {
            HexPath path = new HexPath();

            path.Start = start;
            path.End   = end;
            path.Nodes = new List <HexCorner>();
            Dictionary <HexCorner, HexCorner> parents = new Dictionary <HexCorner, HexCorner>();
            Queue <HexCorner> queue = new Queue <HexCorner>();

            queue.Enqueue(start);
            while (queue.Count != 0)
            {
                HexCorner current = queue.Dequeue();
                if (current.Equals(end))
                {
                    break;
                }
                HexCorner[] currNeighbors = Neighbors[current];
                for (int i = 0; i < currNeighbors.Length; i++)
                {
                    if (validEdgeFunc == null || validEdgeFunc(current, currNeighbors[i]) == true)
                    {
                        if (!parents.ContainsKey(currNeighbors[i]))
                        {
                            parents.Add(currNeighbors[i], current);
                            queue.Enqueue(currNeighbors[i]);
                        }
                    }
                }
            }
            //find distance
            int       distance = 0;
            HexCorner pathNode = end;

            while (!pathNode.Equals(start))
            {
                path.Nodes.Add(pathNode);
                if (parents.ContainsKey(pathNode))
                {
                    pathNode = parents[pathNode];
                }
                else
                {
                    //there is no path that gets us back to the start, return a null path
                    return(null);
                }
                distance++;
            }
            path.Length = distance;
            return(path);
        }
Beispiel #4
0
        public bool IsOutOfRange(HexCorner corner)
        {
            HexCoords origin = new HexCoords(0, 0);

            for (int i = 0; i < 3; i++)
            {
                HexCoords coords = corner.Hexes[i];
                if (HexCoords.HexDistance(coords, origin) > 3)
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #5
0
 private void Build(HexCorner corner)
 {
     //dont add duplicates
     if (Neighbors.ContainsKey(corner))
     {
         return;
     }
     HexCorner[] possibleNeighbors = HexCorner.GetNeighbors(corner);
     HexCorner[] validNeighbors    = possibleNeighbors.Where((HexCorner val) => { return(!IsOutOfRange(val)); }).ToArray();
     Neighbors.Add(corner, validNeighbors);
     for (int i = 0; i < validNeighbors.Length; i++)
     {
         Build(validNeighbors[i]);
     }
 }
Beispiel #6
0
        public static HexCorner[] GetNeighbors(HexCorner corner)
        {
            HexCorner[] neighbors  = new HexCorner[3];
            HexCoords   h1         = corner.Hexes[0];
            HexCoords   h2         = corner.Hexes[1];
            HexCoords   h3         = corner.Hexes[2];
            HexCoords   newCoords1 = h1 + (h2 - h1) + (h3 - h1);

            neighbors[0] = new HexCorner(newCoords1, h2, h3);
            HexCoords newCoords2 = h2 + (h1 - h2) + (h3 - h2);

            neighbors[1] = new HexCorner(h1, newCoords2, h3);
            HexCoords newCoords3 = h3 + (h1 - h3) + (h2 - h3);

            neighbors[2] = new HexCorner(h1, h2, newCoords3);
            return(neighbors);
        }
Beispiel #7
0
 public HexEdge(HexCorner start, HexCorner end)
 {
     Start = start;
     End   = end;
 }