private void RandomizeRoomContents(OGRoom room) { LayoutObjectAtRandom(room, foodTiles, foodPerRoom); LayoutObjectAtRandom(room, wallTiles, rubblePerRoom); int enemiesCountScaling = EnemiesCountScaling(); LayoutObjectAtRandom(room, enemyTiles, enemiesPerRoom, enemiesCountScaling); }
private void LayoutObjectAtRandom(OGRoom room, GameObject[] tileArray, IntRange range, int flatBonus = 0) { int objectCount = range.Random + flatBonus; for (int i = 0; i < objectCount; i++) { if (gridPositions.Count == 0) { break; } Vector3 randomPosition = RandomPosition(); if (CheckForColliders(randomPosition, 0.1f)) { i--; continue; } InstantiateFromArray(tileArray, randomPosition); } }
private void CreateRoomsAndCorridors() { // Create the rooms array with a random size. int randomNumRooms = numRooms.Random; rooms = new OGRoom[randomNumRooms]; if (exitRoom > randomNumRooms) { exitRoom = randomNumRooms; } // Create the first room and corridor. rooms[0] = new OGRoom(); // Setup the first room, there is no previous corridor so we do not use one. rooms[0].SetupRoom(roomWidth, roomHeight, columns, rows); if (startingRoom == 1) { Vector3 playerPos = new Vector3(rooms[0].xPos, rooms[0].yPos, 0); ClearSpace(playerPos, 0.1f); GameManager.instance.GetPlayer().transform.position = playerPos; } // Setup the first corridor using the first room. if (randomNumRooms >= 2) { corridors = new OGCorridor[randomNumRooms - 1]; corridors[0] = new OGCorridor(); corridors[0].SetupCorridor(rooms[0], corridorLength, roomWidth, roomHeight, columns, rows, true); } /* * if (startingRoom == 1) * { * Vector3 playerPos = new Vector3(rooms[0].xPos, rooms[0].yPos, 0); * if (ClearSpace(playerPos, 0.1f)) * playerPos = new Vector3(rooms[0].xPos, rooms[0].yPos, 0); * GameManager.instance.GetPlayer().transform.position = playerPos; * } * * if (exitRoom == 1) * { * Vector3 exitPos = new Vector3(rooms[0].xPos + rooms[0].roomWidth - 1, rooms[0].yPos + rooms[0].roomHeight - 1, 0); * if (ClearSpace(exitPos, 0.1f)) * exitPos = new Vector3(rooms[0].xPos + rooms[0].roomWidth - 1, rooms[0].yPos + rooms[0].roomHeight - 1, 0); * GameObject tileInstance = Instantiate(exit, exitPos, Quaternion.identity) as GameObject; * tileInstance.transform.parent = boardHolder.transform; * } */ for (int i = 1; i < rooms.Length; i++) { // Create a room. rooms[i] = new OGRoom(); // Setup the room based on the previous corridor. rooms[i].SetupRoom(roomWidth, roomHeight, columns, rows, corridors[i - 1]); // If we haven't reached the end of the corridors array... if (i < corridors.Length) { // ... create a corridor. corridors[i] = new OGCorridor(); // Setup the corridor based on the room that was just created. corridors[i].SetupCorridor(rooms[i], corridorLength, roomWidth, roomHeight, columns, rows, false); } /* * if (i == startingRoom - 1) * { * Vector3 playerPos = new Vector3(rooms[i].xPos, rooms[i].yPos, 0); * if (ClearSpace(playerPos, 0.1f)) * playerPos = new Vector3(rooms[i].xPos, rooms[i].yPos + 1, 0); * GameManager.instance.GetPlayer().transform.position = playerPos; * } * * if (i == exitRoom - 1) * { * Vector3 exitPos = new Vector3(rooms[i].xPos, rooms[i].yPos, 0); * if (ClearSpace(exitPos, 0.1f)) * exitPos = new Vector3(rooms[i].xPos + 1, rooms[i].yPos, 0); * GameObject tileInstance = Instantiate(exit, exitPos, Quaternion.identity) as GameObject; * tileInstance.transform.parent = boardHolder.transform; * }*/ } }
private void SetTilesValuesForRooms() { // Go through all the rooms for (int i = 0; i < rooms.Length; i++) { int xCoord, yCoord; OGRoom currentRoom = rooms[i]; gridPositions.Clear(); //Bottom wall yCoord = currentRoom.yPos - 1; for (int j = -1; j < currentRoom.roomWidth + 1; j++) { xCoord = currentRoom.xPos + j; if (tiles[xCoord][yCoord] != TileType.Room) { tiles[xCoord][yCoord] = TileType.Wall; } } //Top wall yCoord = currentRoom.yPos + currentRoom.roomHeight; for (int j = -1; j < currentRoom.roomWidth + 1; j++) { xCoord = currentRoom.xPos + j; if (tiles[xCoord][yCoord] != TileType.Room) { tiles[xCoord][yCoord] = TileType.Wall; } } //Left wall xCoord = currentRoom.xPos - 1; for (int j = 0; j < currentRoom.roomHeight + 1; j++) { yCoord = currentRoom.yPos + j; if (tiles[xCoord][yCoord] != TileType.Room) { tiles[xCoord][yCoord] = TileType.Wall; } } //Right wall xCoord = currentRoom.xPos + currentRoom.roomWidth; for (int j = 0; j < currentRoom.roomHeight + 1; j++) { yCoord = currentRoom.yPos + j; if (tiles[xCoord][yCoord] != TileType.Room) { tiles[xCoord][yCoord] = TileType.Wall; } } //Inside for (int j = 0; j < currentRoom.roomWidth; j++) { xCoord = currentRoom.xPos + j; for (int k = 0; k < currentRoom.roomHeight; k++) { yCoord = currentRoom.yPos + k; tiles[xCoord][yCoord] = TileType.Room; gridPositions.Add(new Vector3(xCoord, yCoord, 0f)); } } //RandomizeRoomContents(currentRoom); } }
public void SetupCorridor(OGRoom room, IntRange length, IntRange roomWidth, IntRange roomHeight, int columns, int rows, bool firstCorridor) { // Set a random direction (a random index from 0 to 3, cast to Direction). direction = (Direction)Random.Range(0, 4); // Find the direction opposite to the one entering the room this corridor is leaving from. // Cast the previous corridor's direction to an int between 0 and 3 and add 2 (a number between 2 and 5). // Find the remainder when dividing by 4 (if 2 then 2, if 3 then 3, if 4 then 0, if 5 then 1). // Cast this number back to a direction. // Overall effect is if the direction was South then that is 2, becomes 4, remainder is 0, which is north. Direction oppositeDirection = (Direction)(((int)room.enteringCorridor + 2) % 4); // If this is noth the first corridor and the randomly selected direction is opposite to the previous corridor's direction... if (!firstCorridor && direction == oppositeDirection) { // Rotate the direction 90 degrees clockwise (North becomes East, East becomes South, etc). // This is a more broken down version of the opposite direction operation above but instead of adding 2 we're adding 1. // This means instead of rotating 180 (the opposite direction) we're rotating 90. int directionInt = (int)direction; directionInt++; directionInt = directionInt % 4; direction = (Direction)directionInt; } // Set a random length. corridorLength = length.Random; // Create a cap for how long the length can be (this will be changed based on the direction and position). int maxLength = length.maximum; switch (direction) { // If the choosen direction is North (up)... case Direction.North: // ... the starting position in the x axis can be random but within the width of the room. startXPos = Random.Range(room.xPos, room.xPos + room.roomWidth); // The starting position in the y axis must be the top of the room. startYPos = room.yPos + room.roomHeight; // The maximum length the corridor can be is the height of the board (rows) but from the top of the room (y pos + height). maxLength = rows - startYPos - roomHeight.minimum - 1; break; case Direction.East: startXPos = room.xPos + room.roomWidth; startYPos = Random.Range(room.yPos, room.yPos + room.roomHeight); maxLength = columns - startXPos - roomWidth.minimum - 1; break; case Direction.South: startXPos = Random.Range(room.xPos, room.xPos + room.roomWidth); startYPos = room.yPos; maxLength = startYPos - roomHeight.minimum - 1; break; case Direction.West: startXPos = room.xPos; startYPos = Random.Range(room.yPos, room.yPos + room.roomHeight); maxLength = startXPos - roomWidth.minimum - 1; break; } // We clamp the length of the corridor to make sure it doesn't go off the board. corridorLength = Mathf.Clamp(corridorLength, 1, maxLength); }