private void LinkRelatedTiles() { int startX, endX, startY, endY; startX = startY = endX = endY = 0; //loop through to get the max row and max col foreach (var tileData in mapTiles) { endX = tileData.XCord > endX ? tileData.XCord : endX; endY = tileData.YCord > endY ? tileData.YCord : endY; } endX++; endY++; //Scan through each tile checking the cardinal directions and if so setting the current tile in question to do stuff for (int i = 0; i < levelTiles.Count; i++) { GameObject tile = levelTiles[i]; MapTileConfigurer tileScript = tile.GetComponent <MapTileConfigurer>(); if (tileScript != null) { //First get the co-ords of the current position int centerTileX = i % endX; int centerTileY = Mathf.FloorToInt(i / endX); //bool left = IsTileTypeMatching(centerTileX - 1, centerTileY, endX, endY, tileScript._tileType); //bool right = IsTileTypeMatching(centerTileX + 1, centerTileY, endX, endY, tileScript._tileType); //bool top = IsTileTypeMatching(centerTileX, centerTileY + 1, endX, endY, tileScript._tileType); //bool bot = IsTileTypeMatching(centerTileX, centerTileY - 1, endX, endY, tileScript._tileType); bool left = IsTileTypeMatching(centerTileX, centerTileY - 1, endX, endY, tileScript._tileType); bool right = IsTileTypeMatching(centerTileX, centerTileY + 1, endX, endY, tileScript._tileType); bool top = IsTileTypeMatching(centerTileX - 1, centerTileY, endX, endY, tileScript._tileType); bool bot = IsTileTypeMatching(centerTileX + 1, centerTileY, endX, endY, tileScript._tileType); switch (tileScript._tileType) { case MapTileConfigurer.Tile.CITY_BUILDING: case MapTileConfigurer.Tile.VILLAGE_BUILDING: tileScript.InitTile(GetBuildingConfig(top, bot, left, right)); break; case MapTileConfigurer.Tile.ROAD: tileScript.InitTile(GetRoadConfig(top, bot, left, right)); break; } } } }
private bool IsTileTypeMatching(int x, int y, int xMax, int yMax, MapTileConfigurer.Tile tileType) { //safety checks first if (x >= xMax || x < 0) { return(false); } if (y >= yMax || y < 0) { return(false); } //Get the index, and compare type MapTileConfigurer tile = levelTiles[xMax * y + x].GetComponent <MapTileConfigurer>(); if (tile == null) { return(false); } return(tile._tileType == tileType); }