//Once the tile habe been generated, creates the world, by instantiating gameObjects (LAND, ROAD, STOPS or CROSS) void intantiateWorld() { GameObject tile = null; for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { switch (worldTable[i, j].whichType) { case tileTypes.BUILDING: tile = (GameObject)Instantiate(buildingTile, new Vector3((i - (WIDTH / 2)) * 2, 0, ((j - (HEIGHT / 2)) * -1) * 2), Quaternion.identity); tile.transform.localScale = new Vector3(2, 2, 2); break; case tileTypes.ROAD: roadTypes typeRoad = shouldIPutCrossOrStop(i, j); GameObject instantiatedObject = null; int rotation = 0; switch (typeRoad) { case roadTypes.NORMAL: instantiatedObject = roadTile; worldTable[i, j].whichRoadType = roadTypes.NORMAL; break; case roadTypes.CROSS_HORIZONTAL: instantiatedObject = crossTile; worldTable[i, j].whichRoadType = roadTypes.CROSS_HORIZONTAL; rotation = 0; break; case roadTypes.CROSS_VERTICAL: instantiatedObject = crossTile; worldTable[i, j].whichRoadType = roadTypes.CROSS_VERTICAL; rotation = 90; break; case roadTypes.STOP_TOP_RIGHT: instantiatedObject = stopTile; worldTable[i, j].whichRoadType = roadTypes.STOP_TOP_RIGHT; rotation = 180; break; case roadTypes.STOP_BOTTOM_RIGHT: instantiatedObject = stopTile; worldTable[i, j].whichRoadType = roadTypes.STOP_BOTTOM_RIGHT; rotation = 270; break; case roadTypes.STOP_BOTTOM_LEFT: instantiatedObject = stopTile; worldTable[i, j].whichRoadType = roadTypes.STOP_BOTTOM_LEFT; rotation = 360; break; case roadTypes.STOP_TOP_LEFT: instantiatedObject = stopTile; worldTable[i, j].whichRoadType = roadTypes.STOP_TOP_LEFT; rotation = 90; break; } tile = (GameObject)Instantiate(instantiatedObject, new Vector3((i - (WIDTH / 2)) * 2, 0, ((j - (HEIGHT / 2)) * -1) * 2), Quaternion.identity); tile.transform.localScale = new Vector3(2, 2, 2); tile.transform.Rotate(new Vector3(0, 1, 0), rotation); break; } tile.transform.SetParent(this.gameObject.transform); } } }
roadTypes shouldIPutCrossOrStop(int x, int y) { roadTypes ret = roadTypes.NORMAL; bool veticalLineFound = true; for (int i = y - 2; i < y + 2; i++) { if ((i >= 0) && (i < HEIGHT) && (x > 0) && (x < WIDTH - 1)) { if ((worldTable[x - 1, i].whichType != tileTypes.BUILDING) || (worldTable[x, i].whichType != tileTypes.ROAD) || (worldTable[x, i].whichRoadType != roadTypes.NORMAL) || (worldTable[x + 1, i].whichType != tileTypes.BUILDING)) { veticalLineFound = false; break; } } else { veticalLineFound = false; break; } } if (veticalLineFound == true) { ret = roadTypes.CROSS_VERTICAL; } else { bool horizontalLineFound = true; for (int i = x - 2; i < x + 2; i++) { if ((i >= 0) && (i < WIDTH) && (y > 0) && (y < HEIGHT - 1)) { if ((worldTable[i, y - 1].whichType != tileTypes.BUILDING) || (worldTable[i, y].whichType != tileTypes.ROAD) || (worldTable[i, y].whichRoadType != roadTypes.NORMAL) || (worldTable[i, y + 1].whichType != tileTypes.BUILDING)) { horizontalLineFound = false; break; } } else { horizontalLineFound = false; break; } } if (horizontalLineFound == true) { ret = roadTypes.CROSS_HORIZONTAL; } else//check the possibility of placing a "stop" { if ((y > 0) && (x < WIDTH - 1) && (worldTable[x, y].whichType == tileTypes.ROAD) && (worldTable[x + 1, y].whichType == tileTypes.BUILDING) && (worldTable[x, y - 1].whichType == tileTypes.ROAD) && (worldTable[x + 1, y - 1].whichType == tileTypes.ROAD)) { ret = roadTypes.STOP_TOP_RIGHT; } else if ((x < WIDTH - 1) && (y < HEIGHT - 1) && (worldTable[x, y].whichType == tileTypes.ROAD) && (worldTable[x + 1, y].whichType == tileTypes.ROAD) && (worldTable[x, y + 1].whichType == tileTypes.BUILDING) && (worldTable[x + 1, y + 1].whichType == tileTypes.ROAD)) { ret = roadTypes.STOP_BOTTOM_RIGHT; } else if ((x > 0) && (y < HEIGHT - 1) && (worldTable[x, y].whichType == tileTypes.ROAD) && (worldTable[x - 1, y].whichType == tileTypes.BUILDING) && (worldTable[x, y + 1].whichType == tileTypes.ROAD) && (worldTable[x - 1, y + 1].whichType == tileTypes.ROAD)) { ret = roadTypes.STOP_BOTTOM_LEFT; } else if ((x > 0) && (y > 0) && (worldTable[x, y].whichType == tileTypes.ROAD) && (worldTable[x, y - 1].whichType == tileTypes.BUILDING) && (worldTable[x - 1, y].whichType == tileTypes.ROAD) && (worldTable[x - 1, y - 1].whichType == tileTypes.ROAD)) { ret = roadTypes.STOP_TOP_LEFT; } } } if ((ret != roadTypes.NORMAL) && (Random.Range(0, 2) > 0)) { return(roadTypes.NORMAL); } return(ret); }