public HexTile FindNearestCityWithConnection(CityTile tile) { List <HexTile> cityTilesByDistance = tile.GetAllCitiesByDistance(); for (int i = 0; i < cityTilesByDistance.Count; i++) { if (cityTilesByDistance[i].GetCityTile().cityAttributes.numOfRoads > 0) { if (i == cityTilesByDistance.Count) { Debug.LogError("No more possible connectionssssss"); break; } return(cityTilesByDistance[i]); } } return(null); }
public void GenerateConnections(List <HexTile> cities) { pendingCityTiles.Add(cities[0].GetCityTile()); for (int i = 0; i < pendingCityTiles.Count; i++) { CityTile currentCityTile = cities[i].GetCityTile(); List <HexTile> listOrderedByDistance = currentCityTile.GetAllCitiesByDistance(); int randomNumberOfRoads = currentCityTile.cityAttributes.GenerateNumberOfRoads(); int createdRoads = currentCityTile.cityAttributes.connectedCities.Count; if (randomNumberOfRoads < createdRoads) { currentCityTile.cityAttributes.numOfRoads = createdRoads; } for (int j = 0; createdRoads < randomNumberOfRoads; j++) { CityTile tileToConnectTo = listOrderedByDistance [j].GetCityTile(); if ((j + 1) == listOrderedByDistance.Count) { //if j has reached listOrderedByDistance's upper bound, connect to nearest city createdRoads++; if (!currentCityTile.cityAttributes.connectedCities.Contains(listOrderedByDistance [0].GetCityTile())) { ConnectCities(currentCityTile.cityAttributes.hexTile, listOrderedByDistance [0]); if (!pendingCityTiles.Contains(listOrderedByDistance [0].GetCityTile())) { pendingCityTiles.Add(listOrderedByDistance [0].GetCityTile()); } } break; } else { if (tileToConnectTo.cityAttributes.numOfRoads < 3 && !currentCityTile.cityAttributes.connectedCities.Contains(tileToConnectTo)) { createdRoads++; ConnectCities(currentCityTile.cityAttributes.hexTile, tileToConnectTo.cityAttributes.hexTile); if (!pendingCityTiles.Contains(tileToConnectTo)) { pendingCityTiles.Add(tileToConnectTo); } } } } if (pendingCityTiles.Count != CityGenerator.Instance.cityCount) { Debug.Log("MISSED SOME CITIES!"); Debug.Log("Create Lines for missed out cities"); for (int x = 0; x < cities.Count; x++) { if (!pendingCityTiles.Contains(cities [x].GetCityTile())) { Debug.Log("======Missed out city: " + cities [x].name + " ======"); CityTile missedOutCity = cities [x].GetCityTile(); HexTile possibleConnectionTile = CityGenerator.Instance.FindNearestCityWithConnection(missedOutCity); if (possibleConnectionTile != null && !missedOutCity.cityAttributes.connectedCities.Contains(possibleConnectionTile.GetCityTile())) { ConnectCities(missedOutCity.cityAttributes.hexTile, possibleConnectionTile); if (!pendingCityTiles.Contains(missedOutCity)) { pendingCityTiles.Add(missedOutCity); } if (!pendingCityTiles.Contains(possibleConnectionTile.GetCityTile())) { pendingCityTiles.Add(possibleConnectionTile.GetCityTile()); } } } } } } Debug.Log("DONE!: " + pendingCityTiles.Count); }