public MapTile GetMatchingTile(MapGraphicsTile mapGraphicsTile) { //Getting a list of matching tiles. In the future we will have several tiles //That might fit, so we will take a random one from the list. var matchingTiles = (from maptile in _mapTiles where mapGraphicsTile.Equals(maptile) select maptile).ToList(); if (matchingTiles.Count == 0) return new MapTile(_errorTile); Random rnd = new Random(); return new MapTile(matchingTiles[rnd.Next(0, matchingTiles.Count)]); }
private void SetSpecialTiles() { if (!MapTiles.Exists(instance => instance.TileType == TileType.Error)) throw new Exception("TileSet Does not have an error Tile"); if (!MapTiles.Exists(instance => instance.TileType == TileType.Land)) throw new Exception("TileSet Does not have a Land Tile"); if (!MapTiles.Exists(instance => instance.TileType == TileType.Water)) throw new Exception("TileSet Does not have a Water Tile"); if (!MapTiles.Exists(instance => instance.TileType == TileType.EdgePlaceHolder)) throw new Exception("TileSet Does not have a Blank Tile"); _errorTile = _mapTiles.First(instance => instance.TileType == TileType.Error); _landTile = _mapTiles.First(instance => instance.TileType == TileType.Land); _landTile.LeftEdgeType = EdgeType.Land; _landTile.RightEdgeType = EdgeType.Land; _landTile.TopEdgeType = EdgeType.Land; _landTile.BottomEdgeType = EdgeType.Land; _waterTile = _mapTiles.First(instance => instance.TileType == TileType.Water); _waterTile.LeftEdgeType = EdgeType.Water; _waterTile.RightEdgeType = EdgeType.Water; _waterTile.TopEdgeType = EdgeType.Water; _waterTile.BottomEdgeType = EdgeType.Water; _edgePlaceholder = _mapTiles.First(instance => instance.TileType == TileType.EdgePlaceHolder); _edgePlaceholder.LeftEdgeType = EdgeType.Undefined; _edgePlaceholder.RightEdgeType = EdgeType.Undefined; _edgePlaceholder.TopEdgeType = EdgeType.Undefined; _edgePlaceholder.BottomEdgeType = EdgeType.Undefined; }
/// <summary> /// Determines how Connections with other tiles a side has. /// </summary> /// <param name="mapGraphicsTile"></param> /// <returns></returns> private int NumberOfSidesWithConnections(MapGraphicsTile mapGraphicsTile) { int count = 0; if (mapGraphicsTile.BottomEdgeType == EdgeType.Both) count++; if (mapGraphicsTile.LeftEdgeType == EdgeType.Both) count++; if (mapGraphicsTile.RightEdgeType == EdgeType.Both) count++; if (mapGraphicsTile.TopEdgeType == EdgeType.Both) count++; return count; }
/// <summary> /// Processes all of the Tiles and determines if they are an edge or not. Edgetiles get added to the list, and the /// other tiles get water / land added to them. /// </summary> /// <param name="bmpData">The bmp data we arew processing</param> /// <param name="gameWorldX">The game's x coordinate</param> /// <param name="gameWorldY">The game's y coordinate</param> private void ProcessTile(BmpData bmpData, int gameWorldX, int gameWorldY) { bool hasWater = false; bool hasLand = false; CheckEdges(bmpData, _graphicsTileSize, out hasWater, out hasLand); if (hasLand && hasWater) { //We generate the edge connections for each tile just like they are by themselves. //After all of the tiles are processed we will do a second pass to ensure everything is connected. MapGraphicsTile mapGraphicsTile = new MapGraphicsTile(); _gameWorld.GameMap[gameWorldX, gameWorldY] = new MapTile(_mapGraphicsTileSet.EdgePlaceHolder); mapGraphicsTile.TileType = TileType.EdgePlaceHolder; _edgeTiles.Add(new Point(gameWorldX, gameWorldY), mapGraphicsTile); } else if (hasLand) _gameWorld.GameMap[gameWorldX, gameWorldY] = new MapTile(_mapGraphicsTileSet.LandTile); else _gameWorld.GameMap[gameWorldX, gameWorldY] = new MapTile(_mapGraphicsTileSet.WaterTile); //Need to release the locked bits here bmpData.Dispose(); }
/// <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> /// Sets the Neighbors Edge Connection based on /// </summary> /// <param name="currentMapGraphicsTile"></param> /// <param name="neighborsMapGraphicsTile"></param> /// <param name="edgeDirection"></param> private void GetNeighborsEdgeConnection(MapGraphicsTile currentMapGraphicsTile, MapGraphicsTile neighborsMapGraphicsTile, EdgeDirection edgeDirection) { if (edgeDirection == EdgeDirection.Top && !neighborsMapGraphicsTile.ShoreEdgePoints.Exists(x => x.EdgePosition > 0 && x.EdgePosition < 3)) { CreateEdgeConnectionBetweenNeighbors(currentMapGraphicsTile, neighborsMapGraphicsTile, edgeDirection); currentMapGraphicsTile.TopEdgeType = EdgeType.Both; } else if (edgeDirection == EdgeDirection.Bottom && !neighborsMapGraphicsTile.ShoreEdgePoints.Exists(x => x.EdgePosition > 6 && x.EdgePosition < 10)) { CreateEdgeConnectionBetweenNeighbors(currentMapGraphicsTile, neighborsMapGraphicsTile, edgeDirection); currentMapGraphicsTile.BottomEdgeType = EdgeType.Both; } else if (edgeDirection == EdgeDirection.Left && !neighborsMapGraphicsTile.ShoreEdgePoints.Exists(x => x.EdgePosition > 3 && x.EdgePosition < 7)) { CreateEdgeConnectionBetweenNeighbors(currentMapGraphicsTile, neighborsMapGraphicsTile, edgeDirection); currentMapGraphicsTile.BottomEdgeType = EdgeType.Both; } else if (edgeDirection == EdgeDirection.Right && !neighborsMapGraphicsTile.ShoreEdgePoints.Exists(x => x.EdgePosition > 9 && x.EdgePosition < 13)) { CreateEdgeConnectionBetweenNeighbors(currentMapGraphicsTile, neighborsMapGraphicsTile, edgeDirection); currentMapGraphicsTile.BottomEdgeType = EdgeType.Both; } }
/// <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; }
/// <summary> /// Detemines if a GmapTile contains a transition in it. /// </summary> /// <param name="gmapX">The google maps X coordinates</param> /// <param name="gmapY">The google maps Y coordinates</param> /// <param name="gmapBitmap">The bitmap we are processing</param> /// <returns>a value indicating if a tile has a transition in it. </returns> private bool CheckGmapTilesForTransitions(int gmapX, int gmapY, Bitmap gmapBitmap, int zoomLevel, Point currentAbsolutePosition) { int offset = getOffSet(zoomLevel); bool hasWater = false; bool hasLand = false; using (BmpData largeBmpData = new BmpData(gmapBitmap, 256)) CheckEdges(largeBmpData, 256, out hasWater, out hasLand); //if we have land and water on the tile, then we need to Process it normally, otherwise we can skip all of that. if (hasLand && hasWater) return true; MapGraphicsTile mapGraphicsTile = new MapGraphicsTile(); if (hasLand) mapGraphicsTile= _mapGraphicsTileSet.LandTile; else mapGraphicsTile= _mapGraphicsTileSet.WaterTile; MapTile mapTile = new MapTile(mapGraphicsTile); for (int x = currentAbsolutePosition.X * _graphicsTileSize; x < (currentAbsolutePosition.X * _graphicsTileSize) + (_graphicsTileSize * offset); x++) { for (int y = currentAbsolutePosition.Y * _graphicsTileSize; y < (currentAbsolutePosition.Y * _graphicsTileSize) + (_graphicsTileSize * offset); y++) { _gameWorld.GameMap[x,y] = mapTile; } } return false; }
/// <summary> /// Generates the tiles that occur along a path that has been found between two tiles that do not connect. /// </summary> /// <param name="bestPath"></param> private void BuildPath(TilePath bestPath) { TilePath currentPathTile = bestPath; EdgeConnection parentEdgeConnection = null; Dictionary<Point,MapGraphicsTile> tilesToAdd = new Dictionary<Point,MapGraphicsTile>(); while (true) { MapGraphicsTile currentMapTile; //create a new mapTile if (_noNeighborTiles.ContainsKey(currentPathTile.TileLocation)) { currentMapTile = _noNeighborTiles[currentPathTile.TileLocation]; //foreach (EdgeConnection edgeConnection in currentMapTile.ShoreEdgePoints) // SetNeighborEdgeConnections(new KeyValuePair<Point, MapGraphicsTile>(currentPathTile.TileLocation, _edgeTiles[currentPathTile.TileLocation]), edgeConnection); //get rid of all of the edgepoints that are not a match, as we cannot used them. //currentMapTile.ShoreEdgePoints.RemoveAll(i => i.IsConnected == false); } else currentMapTile = new MapGraphicsTile(); //add the previous parent translated to they connect if (parentEdgeConnection != null) currentMapTile.ShoreEdgePoints.Add(new EdgeConnection(parentEdgeConnection.TranslateConnectionForNeighbor(), true)); //Get the parentEdgeConnection parentEdgeConnection = GetParentTileEdgeConnection(currentPathTile); if (parentEdgeConnection != null) currentMapTile.ShoreEdgePoints.Add(parentEdgeConnection); tilesToAdd.Add(currentPathTile.TileLocation, currentMapTile); //if the current tile doesnt have a parentTile we are done. if (currentPathTile.ParentTile == null) break; currentPathTile = currentPathTile.ParentTile; } foreach (KeyValuePair<Point,MapGraphicsTile> mapGraphicsTile in tilesToAdd) { SetTileEdgeTypeByNeighbor(mapGraphicsTile); MapTile mapTile = _mapGraphicsTileSet.GetMatchingTile(mapGraphicsTile.Value); if (mapTile.GraphicsTile.TileType != TileType.Error) _gameWorld.GameMap[mapGraphicsTile.Key.X, mapGraphicsTile.Key.Y] = mapTile; } }
private void SaveValues() { MapGraphicsTile mapGraphicsTile = new MapGraphicsTile { ShoreEdgePoints = GetShoreEdgePoints(), TileStartPoint = _currentPoint, TileType = TileType }; mapGraphicsTile = GetTileSides(mapGraphicsTile); _graphicsTiles[_currentPoint] = mapGraphicsTile; }
private MapGraphicsTile GetTileSides(MapGraphicsTile mapGraphicsTile) { mapGraphicsTile.TopEdgeType = TopEdge; mapGraphicsTile.BottomEdgeType = BottomEdge; mapGraphicsTile.LeftEdgeType = LeftEdge; mapGraphicsTile.RightEdgeType = RightEdge; return mapGraphicsTile; }