/// <summary> /// Generates the room tiles as children of the GameObject passed as parameter. /// </summary> /// <param name="parent">Parent.</param> /// <param name="roomSideX">Room side x.</param> /// <param name="roomSideY">Room side y.</param> /// <param name="roomSideZ">Room side z.</param> public void GenerateRectangularRoomIn(GameObject parent, int roomSideX, int roomSideY, int roomSideZ, RoomCoordinatesSystem coordinatesSystem) { // First, the side length of a tile is calculated. float tileSize = tileFloor.GetComponent <Renderer> ().bounds.size.x; // Computing position based on RoomCoordinatesSystem Vector3 originPosition = parent.transform.position; switch (coordinatesSystem) { case RoomCoordinatesSystem.VertexCentered: // No translations break; case RoomCoordinatesSystem.BaseCentered: originPosition += -new Vector3(roomSideX / 2.0f, 0, roomSideZ / 2.0f) * tileSize; break; case RoomCoordinatesSystem.ShapeCentered: originPosition += -new Vector3(roomSideX / 2.0f, roomSideY / 2.0f, roomSideZ / 2.0f) * tileSize; break; default: throw new SwitchCaseUnhandledException("Unhandled case in RoomCoordinatesSystem switch."); } originPosition += new Vector3(tileSize / 2, 0, tileSize / 2); // Creating "horizontal" tiles for (int x = 0; x < roomSideX; x++) { for (int z = 0; z < roomSideZ; z++) { // Creating floor tiles Transform floor = Instantiate( tileFloor, new Vector3(x, 0, z) * tileSize + originPosition, Quaternion.Euler(-90, 0, 0)); floor.SetParent(parent.transform); // Creating Ceiling tiles Transform ceiling = Instantiate( tileCeiling, new Vector3(x, roomSideY, z) * tileSize + originPosition, Quaternion.Euler(-90, 0, 0)); ceiling.SetParent(parent.transform); // TODO: Correct the problem with rotation (even if models were remade from zero, the problem is still there) } } // Creating vertical walls for (int y = 0; y <= roomSideY; y++) { // Selecting the vertical wall based on the "floor" // Exmp: Ground floor -> BottomWall // Exmp: Last floor -> TopWall Transform current_wall; if (y == 0) { //current_wall = (Random.value < 0.4) ? tileBottomWallDoor : tileBottomWallPlain; current_wall = tileBottomWallPlain; } else if (y == roomSideY) { current_wall = tileTopWall; } else { current_wall = tileMiddleWall; } // Creating X-facing walls for (int z = 0; z < roomSideZ; z++) { // Creating (x = 0) tiles Transform middle_wall_A = Instantiate( current_wall, new Vector3(0, y, z) * tileSize + originPosition, Quaternion.Euler(-90, 90, 0)); middle_wall_A.SetParent(parent.transform); // Creating (x = roomSideX - 1) tiles Transform middle_wall_B = Instantiate( current_wall, new Vector3(roomSideX - 1, y, z) * tileSize + originPosition, Quaternion.Euler(-90, -90, 0)); middle_wall_B.SetParent(parent.transform); } // Creating Z-facing walls. for (int x = 0; x < roomSideX; x++) { // Creating (z = 0) tiles Transform middle_wall_A = Instantiate( current_wall, new Vector3(x, y, 0) * tileSize + originPosition, Quaternion.Euler(-90, 0, 0)); middle_wall_A.SetParent(parent.transform); // Creating (z = roomSideZ - 1) tiles Transform middle_wall_B = Instantiate( current_wall, new Vector3(x, y, roomSideZ - 1) * tileSize + originPosition, Quaternion.Euler(-90, 180, 0)); middle_wall_B.SetParent(parent.transform); } } }
/// <summary> /// Instantiates the Empty Parent Object and creates the room tiles. /// </summary> /// <returns>The reference to the empty parent object.</returns> /// <param name="position">Position.</param> /// <param name="roomSideX">Room side x.</param> /// <param name="roomSideY">Room side y.</param> /// <param name="roomSideZ">Room side z.</param> public GameObject GenerateRectangularRoom(Vector3 position, int roomSideX, int roomSideY, int roomSideZ, RoomCoordinatesSystem coordinatesSystem) { // Instantiating empty parent object GameObject emptyParent = new GameObject(); // Setting its position as the defined position emptyParent.transform.position = position; // Creating room tiles GenerateRectangularRoomIn(emptyParent, roomSideX, roomSideY, roomSideZ, coordinatesSystem); // Returning parent reference return(emptyParent); }