public void AddTrees(GridCreator grid, float scale) { List <List <GameObject> > trees = new List <List <GameObject> > (); trees.Add(CreateListOfPrefabs(grid.conetreePrefab, 0, -1, scale)); trees.Add(CreateListOfPrefabs(grid.woodtreePrefab, 1, -1, scale)); FillWithTrees(grid, trees, scale); }
private void OnEnable() { grid = (GridCreator)target; grid.GenerateMaps(true); redoStack = new LinkedList <GridCreator.CellType[, ]>(); undoStack = new LinkedList <GridCreator.CellType[, ]>(); current = (GridCreator.CellType[, ])grid.worldMap.Clone(); undoStack.AddLast(new LinkedListNode <GridCreator.CellType[, ]>(grid.worldMap)); }
public void Populate(GridCreator grid) { min = grid.startingPoint; max = grid.endingPoint; gridSize = new Vector3(max.x - min.x, 1f, max.z - min.z); List <List <Vector3> > sampledStreet = SampleStreets(grid.streets); if (sampledStreet.Count > 0) { ComputeStreetIntersections(sampledStreet); ComputeIntersectionClusters(); List <GameObject> streetWeb = CreateStreets(sampledStreet); //offsetting from polyline CreateSideWalks(streetWeb, sampledStreet); //offsetting from polyline AddBuildings(grid, sampledStreet, 0.4f); } AddTrees(grid, 0.1f); }
public void AddBuildings(GridCreator grid, List <List <Vector3> > streets, float scale) { List <List <GameObject> > buildings = new List <List <GameObject> >(); buildings.Add(CreateListOfPrefabs(grid.housePrefab, -1, 0, scale)); buildings.Add(CreateListOfPrefabs(grid.skyscraperPrefab, -1, 1, scale)); int buildingIndex = 1; for (int i = 0; i < streets.Count; i++) { List <Vector2> side1 = new List <Vector2>(); List <Vector2> side2 = new List <Vector2>(); ComputeContourPoints(out side1, out side2, streets[i], 4f); for (int j = 0; j < side1.Count - 1; j++) { Vector3 pointFrom = new Vector3(side1[j].x, 0f, side1[j].y); Vector3 pointTo = new Vector3(side1[j + 1].x, 0f, side1[j + 1].y); buildingIndex = FillWithBuildings(grid, scale, pointFrom, pointTo, buildingIndex, streets[i][j], streets[i][j + 1], buildings); pointFrom = new Vector3(side2[j].x, 0f, side2[j].y); pointTo = new Vector3(side2[j + 1].x, 0f, side2[j + 1].y); buildingIndex = FillWithBuildings(grid, scale, pointFrom, pointTo, buildingIndex, streets[i][j], streets[i][j + 1], buildings); } } foreach (GameObject house in buildings[0]) { if (house.GetComponent <Building>() != null) { DestroyImmediate(house.gameObject); } } foreach (GameObject skyscraper in buildings[1]) { if (skyscraper.GetComponent <Building>() != null) { DestroyImmediate(skyscraper.gameObject); } } }
public void Populate(GridCreator grid) { ClearCity(); TerrainData terrainData = GetComponent <TerrainData> (); min = grid.startingPoint; max = grid.endingPoint; gridSize = new Vector3(max.x - min.x, 1f, max.z - min.z); citysize = grid.terrainData.size = gridSize; List <List <Vector3> > sampledStreet = SampleStreets(grid.streets); if (sampledStreet.Count > 0) { ComputeStreetIntersections(sampledStreet); ComputeIntersectionClusters(); List <GameObject> streetWeb = CreateStreets(sampledStreet); //offsetting from polyline CreateSideWalks(streetWeb, sampledStreet); //offsetting from polyline AddBuildings(grid, sampledStreet, buildingScale); } AddTrees(grid, treeScale); }
public GridCreator.CellType GetGridType(Vector3 point, GridCreator grid) { Vector3 gridPos = new Vector3((Mathf.Floor(point.x - grid.transform.position.x / 1) / grid.globalScale), 0.05f, (Mathf.Floor(point.z - grid.transform.position.z / 1) / grid.globalScale)); //if x or z position of mouse is out of grid //set a correct int value if (gridPos.x < 0) { gridPos.x = -1; } if (gridPos.z < 0) { gridPos.z = -1; } if (gridPos.x >= 0 && gridPos.z >= 0 && gridPos.x < grid.worldMap.GetLength(0) && gridPos.z < grid.worldMap.GetLength(1)) { return(grid.worldMap [(int)gridPos.x, (int)gridPos.z]); } return(GridCreator.CellType.Empty); }
//uncommenting the while and "currDist" makes the building density higher public void PlaceBuilding(GridCreator grid, float scale, Vector3 pointFrom, Vector3 pointTo, ref int buildingIndex, Vector3 firstTilePoint, Vector3 secondTilePoint, List <List <GameObject> > buildings) { int collisions = 0; int numOfTry = 0; GameObject building; Vector3 position = pointFrom; Vector3 streetPosition = firstTilePoint; var distance = Vector3.Distance(pointFrom, pointTo); var streetDistance = Vector3.Distance(firstTilePoint, secondTilePoint); var direction = (pointTo - pointFrom); direction.Normalize(); direction.y = 0f; var streetDirection = (secondTilePoint - firstTilePoint); streetDirection.Normalize(); streetDirection.y = 0f; var offset = 0.6f; var streetOffset = (streetDistance * offset) / distance; //while (currDist < distance) //{ //check for collision in current position collisions = 0; numOfTry = 0; var buildingType = GetGridType(position, grid); if (buildingType == GridCreator.CellType.Empty) { //return; } var residentialPrefabs = buildings [0]; var skyscraperPrefabs = buildings [1]; List <GameObject> selection; if (buildingType == GridCreator.CellType.Residential || skyscraperPrefabs.Count == 0) { selection = residentialPrefabs; } else if (buildingType == GridCreator.CellType.SkyScraper || residentialPrefabs.Count == 0) { selection = skyscraperPrefabs; } else { selection = buildings [Random.Range(0, buildings.Count)]; } var randomIndex = Random.Range(0, selection.Count); do { numOfTry += 1; building = selection [randomIndex]; Vector3 colliderSize = building.gameObject.GetComponent <BoxCollider> ().size; collisions = Physics.OverlapSphereNonAlloc(position, scale * Mathf.Max(colliderSize.x, colliderSize.y, colliderSize.z) / 2, colliders); if (collisions > 1) { // try again randomIndex = (randomIndex + 1) % selection.Count; } } while (collisions > 1 && numOfTry < selection.Count); if (collisions > 0 && collisions <= 1) { buildingIndex += 1; building = Instantiate(building); building.name = "building" + buildingIndex; building.transform.position = position; building.transform.parent = this.transform; building.transform.localScale = new Vector3(scale, scale, scale); building.transform.rotation = Quaternion.LookRotation((position - streetPosition).normalized); } position = position + (offset * direction); streetPosition = streetPosition + (streetOffset * streetDirection); }
public bool IsForestPoint(GridCreator grid, Vector3 pos) { return(grid.worldMap [(int)pos.x, (int)pos.z] == GridCreator.CellType.Forest || grid.worldMap [(int)pos.x, (int)pos.z] == GridCreator.CellType.ConeForest || grid.worldMap [(int)pos.x, (int)pos.z] == GridCreator.CellType.Woods); }
public void FillWithTrees(GridCreator grid, List <List <GameObject> > trees, float scale) { int i = 1; int numOfTry = 0; float colliderRadius = 0f; Vector3 randPoint = new Vector3(Random.Range(min.x, max.x), Random.Range(min.y, max.y), Random.Range(min.z, max.z)); randPoint.y = 0f; Vector3 gridPos = new Vector3((Mathf.Floor(randPoint.x - grid.transform.position.x / 1) / grid.globalScale), 0.05f, (Mathf.Floor(randPoint.z - grid.transform.position.z / 1) / grid.globalScale)); GameObject tree; while (numOfTry < treeDensity) { if (IsForestPoint(grid, gridPos)) { if (grid.worldMap [(int)gridPos.x, (int)gridPos.z] == GridCreator.CellType.ConeForest) { tree = trees [0] [Random.Range(0, trees [0].Count)]; } else if (grid.worldMap [(int)gridPos.x, (int)gridPos.z] == GridCreator.CellType.Woods) { tree = trees [1] [Random.Range(0, trees [1].Count)]; } else { List <GameObject> currList = trees [Random.Range(0, 2)]; tree = currList [Random.Range(0, currList.Count)]; } foreach (Transform child in tree.transform) { colliderRadius = child.GetComponent <CapsuleCollider> ().radius *(scale + 0.02f); } int collisions = Physics.OverlapSphereNonAlloc(GridToTerrain(randPoint), colliderRadius, colliders); if (collisions <= 1) { numOfTry = 0; tree = Instantiate(tree); tree.name = "tree" + i; tree.transform.position = GridToTerrain(randPoint); tree.transform.parent = this.transform; tree.transform.localScale = new Vector3(scale, scale, scale); i += 1; } else { numOfTry += 1; } } else { numOfTry += 1; } randPoint = new Vector3(Random.Range(min.x, max.x), Random.Range(min.y, max.y), Random.Range(min.z, max.z)); randPoint.y = 0f; gridPos = new Vector3((Mathf.Floor(randPoint.x - grid.transform.position.x / 1) / grid.globalScale), 0.05f, (Mathf.Floor(randPoint.z - grid.transform.position.z / 1) / grid.globalScale)); } foreach (GameObject conetree in trees[0]) { if (conetree.GetComponent <ProcTree> () != null) { DestroyImmediate(conetree.gameObject); } } foreach (GameObject woodtree in trees[1]) { if (woodtree.GetComponent <ProcTree> () != null) { DestroyImmediate(woodtree.gameObject); } } trees [0].Clear(); trees [1].Clear(); trees.Clear(); }
//uncommenting the while and "currDist" makes the building density higher public int FillWithBuildings(GridCreator grid, float scale, Vector3 pointFrom, Vector3 pointTo, int buildingIndex, Vector3 firstTilePoint, Vector3 secondTilePoint, List <List <GameObject> > buildings) { int collisions = 0; int numOfTry = 0; GameObject building; Vector3 position = pointFrom; Vector3 streetPosition = firstTilePoint; var distance = Vector3.Distance(pointFrom, pointTo); var streetDistance = Vector3.Distance(firstTilePoint, secondTilePoint); var direction = (pointTo - pointFrom); direction.Normalize(); direction.y = 0f; var streetDirection = (secondTilePoint - firstTilePoint); streetDirection.Normalize(); streetDirection.y = 0f; var offset = 0.6f; var streetOffset = (streetDistance * offset) / distance; //while (currDist < distance) //{ //check for collision in current position collisions = 0; numOfTry = 0; var buildingType = GetGridType(position, grid); do { numOfTry += 1; if (buildingType == GridCreator.CellType.Residential) { building = buildings[0][Random.Range(0, buildings[0].Count)]; } else if (buildingType == GridCreator.CellType.SkyScraper) { building = buildings[1][Random.Range(0, buildings[1].Count)]; } else { List <GameObject> currList = buildings[Random.Range(0, 2)]; building = currList[Random.Range(0, currList.Count)]; } Vector3 colliderSize = building.gameObject.GetComponent <BoxCollider>().size; collisions = Physics.OverlapSphere(position, scale * Mathf.Max(colliderSize.x, colliderSize.y, colliderSize.z) / 2).Length; }while (collisions > 1 && numOfTry < Mathf.Max(buildings[0].Count, buildings[1].Count)); if (collisions > 0 && collisions <= 1) { buildingIndex += 1; building = Instantiate(building); building.name = "building" + buildingIndex; building.transform.position = position; building.transform.parent = this.transform; building.transform.localScale = new Vector3(scale, scale, scale); building.transform.rotation = Quaternion.LookRotation((position - streetPosition).normalized); } position = position + (offset * direction); streetPosition = streetPosition + (streetOffset * streetDirection); //} return(buildingIndex); }
private void OnEnable() { grid = (GridCreator)target; grid.GenerateMaps(false); grid.showGrid = true; }
private void Reset() { grid = (GridCreator)target; grid.GenerateMaps(true); }