private bool SetNeighborEdgeConnections(KeyValuePair<Point,MapGraphicsTile> mapGraphicsTile, EdgeConnection edgeConnection) { //CheckDown if (edgeConnection.EdgePosition > 0 && edgeConnection.EdgePosition < 4) //_edgeTiles.ContainsKey()) { CheckNeighborForMatchingEdgePoint(_edgeTiles[new Point(mapGraphicsTile.Key.X, mapGraphicsTile.Key.Y + 1)], edgeConnection); return true; } else if (edgeConnection.EdgePosition > 3 && edgeConnection.EdgePosition < 7 && _edgeTiles.ContainsKey(new Point(mapGraphicsTile.Key.X + 1, mapGraphicsTile.Key.Y))) { CheckNeighborForMatchingEdgePoint(_edgeTiles[new Point(mapGraphicsTile.Key.X + 1, mapGraphicsTile.Key.Y)], edgeConnection); return true; } else if (edgeConnection.EdgePosition > 6 && edgeConnection.EdgePosition < 10 && _edgeTiles.ContainsKey(new Point(mapGraphicsTile.Key.X, mapGraphicsTile.Key.Y - 1))) { CheckNeighborForMatchingEdgePoint(_edgeTiles[new Point(mapGraphicsTile.Key.X, mapGraphicsTile.Key.Y - 1)], edgeConnection); return true; } else if (edgeConnection.EdgePosition > 6 && edgeConnection.EdgePosition < 10 && _edgeTiles.ContainsKey(new Point(mapGraphicsTile.Key.X - 1, mapGraphicsTile.Key.Y))) { CheckNeighborForMatchingEdgePoint(_edgeTiles[new Point(mapGraphicsTile.Key.X - 1, mapGraphicsTile.Key.Y)], edgeConnection); return true; } return false; }
/// <summary> /// Creates an edge connection between two neighbors. /// </summary> /// <param name="currentMapGraphicsTile"></param> /// <param name="neighborsMapGraphicsTile"></param> /// <param name="edgeDirection"></param> private void CreateEdgeConnectionBetweenNeighbors(MapGraphicsTile currentMapGraphicsTile, MapGraphicsTile neighborsMapGraphicsTile, EdgeDirection edgeDirection) { EdgeConnection edgeConnection = new EdgeConnection(CreateEdgeConnection(edgeDirection),true); currentMapGraphicsTile.ShoreEdgePoints.Add(edgeConnection); neighborsMapGraphicsTile.ShoreEdgePoints.Add(new EdgeConnection(edgeConnection.TranslateConnectionForNeighbor(), true)); }
/// <summary> /// If the two edges do not match 1 to 1, We find the closest tile on that edge. /// </summary> /// <param name="edgeConnection"></param> /// <param name="availableConnections"></param> /// <returns></returns> private byte GetClosestMatchingEdgeConnection(EdgeConnection edgeConnection, List<EdgeConnection> availableConnections) { if (availableConnections.Exists(instance => instance.TranslateConnectionForNeighbor() == edgeConnection.EdgePosition)) return edgeConnection.EdgePosition; if (availableConnections.Count == 1) return availableConnections[0].TranslateConnectionForNeighbor(); for (int i = 0; i < 3; i++) { EdgeConnection matchConnection = null; matchConnection = availableConnections.Find(instance => instance.TranslateConnectionForNeighbor() == edgeConnection.EdgePosition + i); if (matchConnection != null) return matchConnection.TranslateConnectionForNeighbor(); matchConnection = availableConnections.Find(instance => instance.TranslateConnectionForNeighbor() == edgeConnection.EdgePosition - i); if (matchConnection != null) return matchConnection.TranslateConnectionForNeighbor(); } throw new ArgumentNullException("No Match Found"); }
/// <summary> /// Determines if the Neighbord has matching EdgePoint. If it does, we set the current edgeConnection /// to the edge connection of the neighbor and set them both to connected. /// </summary> /// <param name="neighborTile"></param> /// <param name="edgeConnection"></param> /// <returns></returns> private bool CheckNeighborForMatchingEdgePoint(MapGraphicsTile neighborTile, EdgeConnection edgeConnection) { List<EdgeConnection> availableConnections = new List<EdgeConnection>(); switch (edgeConnection.EdgePosition) { case 1: case 2: case 3: //Bottom Side availableConnections.AddRange( neighborTile.ShoreEdgePoints.FindAll( instance => instance.EdgePosition > 6 && instance.EdgePosition < 10)); break; case 4: case 5: case 6: //Right Side availableConnections.AddRange( neighborTile.ShoreEdgePoints.FindAll( instance => instance.EdgePosition > 9 && instance.EdgePosition < 13)); break; case 7: case 8: case 9: //Top Side availableConnections.AddRange( neighborTile.ShoreEdgePoints.FindAll( instance => instance.EdgePosition > 0 && instance.EdgePosition < 4 && !instance.IsConnected)); break; case 10: case 11: case 12: //Left Side availableConnections.AddRange( neighborTile.ShoreEdgePoints.FindAll( instance => instance.EdgePosition > 3 && instance.EdgePosition < 7)); break; } if (availableConnections.Count > 0) { //Get the closest match byte closestMatch = GetClosestMatchingEdgeConnection(edgeConnection, availableConnections); //set the current edgeconnection to the closestMatch and it is now connected edgeConnection.EdgePosition = closestMatch; edgeConnection.IsConnected = true; //set the neighbortile edge point to connected as well. EdgeConnection neighborConnection = neighborTile.ShoreEdgePoints.Find(instance => instance.TranslateConnectionForNeighbor() == closestMatch); if (neighborConnection != null) { neighborConnection.IsConnected = true; } else { int i = 0; i++; } return true; } //No Matches were found on the adjacent tile return false; }