public NeededSpace(NeededSpace neededSpace, Vector3Int offset) { _usedCoordinate = new Vector3Int(neededSpace.UsedCoordinate.x + offset.x, neededSpace.UsedCoordinate.y + offset.y, neededSpace.UsedCoordinate.z + offset.z); _terrainType = neededSpace.TerrainType; }
private IEnumerator GenerateCity(int seed, int maxCityProgress, Vector2Int streetLengthMinMax, float buildingProbability) { List <SimpleMapPlaceable> endpoints = new List <SimpleMapPlaceable>(); SimpleMapPlaceable mapPlaceable = GetComponentInChildren <SimpleMapPlaceable>(); endpoints.Add(mapPlaceable); System.Random random = new System.Random(seed); float waitTime = 0.0f; int progress = 0; while (progress < maxCityProgress) { progress++; if (endpoints.Count == 0) { break; } Street street = (Street)endpoints[0]; endpoints.RemoveAt(0); for (int i = 0; i < street.NeighborNodes.Length; i++) { if (street.NeighborNodes[i]) { continue; } Vector3 directionVec = Util.DirectionIntToVector(i); Vector3 directionClockwiseVec = Util.DirectionIntToVector((i + 1) % 4); Vector3 directionCounterClockwiseVec = Util.DirectionIntToVector((i + 3) % 4); int amount = random.Next(streetLengthMinMax.x, streetLengthMinMax.y); for (int j = 1; j < amount + 1; j++) { Vector3 placedPosition = street.transform.position + (directionVec * j); if (IsRasterizing(placedPosition) || !_placementController.IsPlaceable(placedPosition, street.UsedCoordinates)) { break; } SimpleMapPlaceable placeable = PlaceStreetAt(placedPosition); yield return(new WaitForSeconds(waitTime)); if (j < amount) { if (random.NextDouble() > buildingProbability) { continue; } List <NeededSpace> neededSpaces = new List <NeededSpace>() { NeededSpace.Zero(TerrainGenerator.TerrainType.Flatland) }; bool isClockwisePlaceable = _placementController.IsPlaceable(placedPosition + directionClockwiseVec, neededSpaces); bool isCounterClockwisePlaceable = _placementController.IsPlaceable(placedPosition + directionCounterClockwiseVec, neededSpaces); Vector3 buildingStartPosition; Vector3 streetDirection; if (isClockwisePlaceable) { buildingStartPosition = placedPosition + directionClockwiseVec; streetDirection = directionCounterClockwiseVec; } else if (isCounterClockwisePlaceable) { buildingStartPosition = placedPosition + directionCounterClockwiseVec; streetDirection = directionClockwiseVec; } else { continue; } GameObject building = new GameObject("Procedural Building"); building.transform.position = buildingStartPosition; SimpleMapPlaceable simpleMapPlaceable; if (!_cityPlaceable.MainBuilding) { CityMainBuilding cityMainBuilding = building.AddComponent <CityMainBuilding>(); cityMainBuilding.CityPlaceable = _cityPlaceable; cityMainBuilding.UsedCoordinates = new List <NeededSpace>() { NeededSpace.Zero(TerrainGenerator.TerrainType.Flatland) }; _cityPlaceable.MainBuilding = cityMainBuilding; simpleMapPlaceable = cityMainBuilding; } else { ProceduralCityBuilding cityBuilding = building.AddComponent <ProceduralCityBuilding>(); cityBuilding.CityPlaceable = _cityPlaceable; cityBuilding.Height = 1f; cityBuilding.Generate(random.Next(1, amount - j), directionVec, streetDirection, _placementController); simpleMapPlaceable = cityBuilding; } if (!_placementController.PlaceObject(simpleMapPlaceable)) { Destroy(simpleMapPlaceable.gameObject); } ; _cityPlaceable.ChildMapPlaceables.Add(simpleMapPlaceable); building.transform.SetParent(transform); } if (j != amount) { continue; } endpoints.Add(placeable); break; } yield return(new WaitForSeconds(waitTime)); } } }