void SetRoomTileValues() { for (int i = 0; i < room_.Length; i++) //loop through all rooms { RoomGen currentRoom = room_[i]; for (int j = 0; j < currentRoom.roomWidth; j++) //for each room go through room width { int xCoordinate = currentRoom.xPosition + j; for (int k = 0; k < currentRoom.roomHeight; k++) //for each horizontal tile, go up vertically through room's height. { int yCoordinate = currentRoom.yPosition + k; if (i == 1) //if second room { tile[xCoordinate][yCoordinate] = TypeofTile.floorSpawner; //set tile to spawner tile } else { tile[xCoordinate][yCoordinate] = TypeofTile.Floor; //else set tile to floor. } } } } }
void MoveToNextRoom() { RoomGen currentRoom = rooms[currentRoomIdx]; if (currentRoomIdx - 1 >= 0) { rooms[currentRoomIdx - 1].spawner.ClearCurrentDrops(); } CameraTracker tracker = CameraController.instance.GetCurrentCam().GetComponent <CameraTracker>(); tracker.ChangeRooms(currentRoom.roomSize.x * currentRoom.tileSize, currentRoom.roomSize.y * currentRoom.tileSize, currentRoom.transform.position); if (enemiesEnabled) { currentRoom.spawner.minEnemies = 1 + (int)(minMaxEnemyPercent.x * currentRoom.roomSize.x * currentRoom.roomSize.y); currentRoom.spawner.maxEnemies = (int)(minMaxEnemyPercent.y * currentRoom.roomSize.x * currentRoom.roomSize.y); currentRoom.spawner.SpawnEnemies(); } if (currentRoom.prevDoor != null) { currentRoom.prevDoor.CloseDoor(); } currentRoomIdx++; }
public bool GenerateLevel(Vector3 centerPos, Vector2 size, Vector2 prevSize, int x, int y, int doorNum) { if (roomsGenerated == numRooms) { return(false); } roomsGenerated++; roomGrid[x, y] = true; if (doorNum == 0) { roomGrid[x - (int)Mathf.Sin(Mathf.Deg2Rad * yRot), y - (int)Mathf.Cos(Mathf.Deg2Rad * yRot)] = true; } RoomGen room = Instantiate(roomPrefab, centerPos, Quaternion.identity) as RoomGen; room.transform.parent = transform; room.obstaclePercent = UnityEngine.Random.Range(minMaxObstaclePercent.x, minMaxObstaclePercent.y); room.roomSize = size; room.FindDoors(); room.seed = UnityEngine.Random.Range(0, 1000); if (!GenerateRandomDoor(room, size, prevSize, x, y)) { roomsGenerated--; Destroy(room.gameObject); return(GenerateLevel(centerPos, size, prevSize, x, y, doorNum)); } else { if (doorNum != 0) { room.prevDoorCoord = room.Door[doorNum];; } else { room.isStart = true; switch (yRot) { case 90: room.prevDoorCoord = room.Door[1]; break; case -90: room.prevDoorCoord = room.Door[3]; break; case 0: room.prevDoorCoord = room.Door[2]; break; case 180: room.prevDoorCoord = room.Door[4]; break; } Vector3 startDoorPos = room.CoordToPosition(room.prevDoorCoord.x, room.prevDoorCoord.y); playerStartPos = new Vector3(startDoorPos.x, 1, startDoorPos.z); } rooms.Add(room); return(true); } }
void GenerateFloorPlan() { for (int i = 0; i < possiblePlaces.Count; i++) { possiblePlaces[i].chance = chance; } int startPoint = Random.Range(0, possiblePlaces.Count); possiblePlaces[startPoint].chance = 100; possiblePlaces[startPoint].InstantiateFloor(); currentRoom = possiblePlaces[startPoint]; int totalRooms = 0; for (int i = 0; i < possiblePlaces.Count; i++) { if (possiblePlaces[i].myFloor != null) { totalRooms++; } } if (totalRooms < roomsNeeded || totalRooms > maxRooms) { ResetDungeon(); return; } PlaceStartRoom(startPoint); for (int i = 0; i < possiblePlaces.Count; i++) { possiblePlaces[i].done = false; } possiblePlaces[startPoint].Doors();; bool reset = PlaceBossRoom(startPoint - 1, startPoint + 1, startPoint - gridSize, startPoint + gridSize, startPoint); if (reset) { return; } for (int i = 0; i < possiblePlaces.Count; i++) { if (possiblePlaces[i].myActivities != null) { possiblePlaces[i].myActivities.DisableRigidBodys(); } } EnterRoom(possiblePlaces[startPoint]); MapFloor(); AssignActivities(); BuildWalls(); PlaceDoors(); StartCoroutine(StartPathfinder(startPoint)); }
public void BuildLevel() { for (int i = 0; i < rooms.Count; i++) { RoomGen room = rooms[i]; room.GenerateRoom(); if (i + 1 < rooms.Count) { RoomGen nextRoom = rooms[i + 1]; room.nextTeleporter.SetDestination(nextRoom.CoordToPosition(nextRoom.prevDoorCoord.x, nextRoom.prevDoorCoord.y)); } } }
private void CreateLayer(int _layerIndex) { Transform _layer = Instantiate(GameManager.roomGenerationFields.layerGO, Vector3.zero, Quaternion.identity, this.transform).transform; layers.Add(_layer); List <RoomGen> _layerRooms = new List <RoomGen>(); Vector2[] roomPoints = GetPointsAroundOrigin(Vector2.zero, numberOfRooms[_layerIndex], (_layerIndex + 0.5f) * GameManager.roomGenerationFields.distanceBetweenLayers * GameManager.roomGenerationFields.roomScale, 180, Random.Range(0f, 360f)); for (int i = 0; i < numberOfRooms[_layerIndex]; i++) { RoomGen _room = Instantiate(GameManager.roomGenerationFields.roomGO, roomPoints[i], Quaternion.identity, _layer).GetComponent <RoomGen>(); //_room.transform.position = roomPoints[i]; _layerRooms.Add(_room); } rooms.Add(_layerRooms); }
void SpawnNextTeleporter() { if (currentRoomIdx > 0 && currentRoomIdx <= rooms.Count) { RoomGen currentRoom = rooms[currentRoomIdx - 1]; currentRoom.nextDoor.OpenDoor(); currentRoom.nextTeleporter.gameObject.SetActive(true); if (currentRoomIdx <= rooms.Count - 1) { RoomGen nextRoom = rooms[currentRoomIdx]; nextRoom.prevDoor.OpenDoor(); } CameraTracker tracker = CameraController.instance.GetCurrentCam().GetComponent <CameraTracker>(); tracker.doorClosed = false; tracker.StartCoroutine(tracker.MoveToPlayer()); } }
void CreateRoomsAndCorridors() { // Create the rooms array with a random size. rooms = new RoomGen[numRooms.Random]; // There should be one less corridor than there is rooms. corridors = new Corridor[rooms.Length - 1]; // Create the first room and corridor. rooms[0] = new RoomGen(); corridors[0] = new Corridor(); // Setup the first room, there is no previous corridor so we do not use one. rooms[0].SetupRoom(roomWidth, roomHeight, columns, rows); // Setup the first corridor using the first room. corridors[0].SetupCorridor(rooms[0], corridorLength, roomWidth, roomHeight, columns, rows, true); for (int i = 1; i < rooms.Length; i++) { // Create a room. rooms[i] = new RoomGen(); // 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 Corridor(); // Setup the corridor based on the room that was just created. corridors[i].SetupCorridor(rooms[i], corridorLength, roomWidth, roomHeight, columns, rows, false); } if (i == rooms.Length * .5f) { Vector3 playerPos = new Vector3(rooms[i].xPos, rooms[i].yPos, 0); var playerObject = Instantiate(player, playerPos, Quaternion.identity); GameObject.FindGameObjectWithTag("MainCamera").GetComponent <CameraController>().followTarget = playerObject; } } }
//Generates and shows the Room, if not yet Generated. Otherwise, shows the Room. public void showRoom(System.Random random) { if (!isGenerated) { objects = RoomGen.generateRoom(random.Next(10)); foreach (GameObject g in spawnedObjects) { if (g != null) { g.SetActive(true); } } foreach (GameObject g in generateEnemies(random)) { objects.Add(g); } foreach (GameObject g in generateChests()) { objects.Add(g); } isGenerated = true; isShowing = true; } else if (!isShowing) { foreach (GameObject g in objects) { g.SetActive(true); } foreach (GameObject g in spawnedObjects) { g.SetActive(true); } isShowing = true; } }
bool GenerateRandomDoor(RoomGen room, Vector2 size, Vector2 prevSize, int x, int y) { float rng; bool generated = false; //Top Bot for (int i = -1; i <= 1; i += 2) { rng = UnityEngine.Random.Range(0.0f, 1.0f); if (rng < 0.25f && CheckAvailable(x, y + i)) { Vector2 newSize = new Vector2(UnityEngine.Random.Range(minRoomSize, maxRoomSize + 1) * 2 + 1, UnityEngine.Random.Range(minRoomSize, maxRoomSize + 1) * 2 + 1); Vector3 doorPos = room.CoordToPosition(room.Door[i + 3].x, room.Door[i + 3].y); Vector3 newPos = new Vector3(doorPos.x, 0, doorPos.z + i * (newSize.y / 2 + prevSize.y + 0.5f) * room.tileSize); generated = true; GenerateLevel(newPos, newSize, size, x, y + i, -i + 3); room.nextDoorCoord = room.Door[i + 3]; return(generated); } } //Left Right for (int i = -1; i <= 1; i += 2) { rng = UnityEngine.Random.Range(0.0f, 1.0f); if (rng < 0.25f && CheckAvailable(x + i, y)) { Vector2 newSize = new Vector2(UnityEngine.Random.Range(minRoomSize, maxRoomSize + 1) * 2 + 1, UnityEngine.Random.Range(minRoomSize, maxRoomSize + 1) * 2 + 1); Vector3 doorPos = room.CoordToPosition(room.Door[i + 2].x, room.Door[i + 2].y); Vector3 newPos = new Vector3(doorPos.x + i * (newSize.x / 2 + prevSize.x + 0.5f) * room.tileSize, 0, doorPos.z); generated = true; GenerateLevel(newPos, newSize, size, x + i, y, -i + 2); room.nextDoorCoord = room.Door[i + 2]; return(generated); } } return(generated); }
void CreateRoomsCorridors() { room_ = new RoomGen[numRooms.Random]; //create room_ array with random amount of rooms. corridor_ = new CorridorGen[room_.Length - 1]; //assign corridor_ array with same amount of rooms -1 room_[0] = new RoomGen(); //map room and corridor corridor_[0] = new CorridorGen(); room_[0].SetupRoom(roomWidth, roomHeight, columns, rows); //map second room corridor_[0].CorridorSetup(room_[0], corridorSize, roomWidth, roomHeight, columns, rows, true); // map second corridor from the first room. for (int i = 1; i < room_.Length; i++) //loop through the room_ array { room_[i] = new RoomGen(); //create new room room_[i].SetupRoom(roomWidth, roomHeight, columns, rows, corridor_[i - 1]); //set up room based on prev corridor position if (i < corridor_.Length) //if current forloop iteration is less than the corridor_ value, create a corridor and set up corridor based on the room just created { corridor_[i] = new CorridorGen(); corridor_[i].CorridorSetup(room_[i], corridorSize, roomWidth, roomHeight, columns, rows, false); } } }
void SetTilesValuesForRooms() { // Go through all the rooms... for (int i = 0; i < rooms.Length; i++) { RoomGen currentRoom = rooms[i]; // ... and for each room go through it's width. for (int j = 0; j < currentRoom.roomWidth; j++) { int xCoord = currentRoom.xPos + j; // For each horizontal tile, go up vertically through the room's height. for (int k = 0; k < currentRoom.roomHeight; k++) { int yCoord = currentRoom.yPos + k; // The coordinates in the jagged array are based on the room's position and it's width and height. tiles[xCoord][yCoord] = TileType.Floor; } } } }
IEnumerator StartPathfinder(int startRoom) { yield return(new WaitForSeconds(0.1f)); LoadingScreen.instance.UpdateLoad(); GameManager.instance.grid.GridSize(roomSize, gridSize); yield return(new WaitForSeconds(0.2f)); for (int i = 0; i < possiblePlaces.Count; i++) { if (possiblePlaces[i].myActivities != null) { possiblePlaces[i].myActivities.DisableRigidBodys(); } } LoadingScreen.instance.UpdateLoad(); currentRoom = possiblePlaces[startRoom]; currentRoom.myActivities.EnableRigidBodys(); StartCoroutine(EnterRoom(possiblePlaces[startRoom])); yield return(new WaitForSeconds(0.5f)); LoadingScreen.instance.UpdateLoad(); PlacePlayer(startRoom); if (PlayerStats.instance != null) { PlayerStats.instance.LoadWeapons(); } AssignThings(); ready = true; yield return(new WaitForSeconds(0.6f)); LoadingScreen.instance.UpdateLoad(); GameManager.instance.paused = false; }
public IEnumerator EnterRoom(RoomGen entering) { yield return(new WaitForSeconds(0.01f)); if (GameManager.instance.grid.ready) { if (GameManager.instance.grid.ready && currentRoom.myActivities != null) { if (currentRoom != null) { bool firstChamber = false; if (lastRoom == null) { firstChamber = true; } lastRoom = currentRoom; currentRoom = entering; if (!firstChamber) { if (lastRoom.left != currentRoom && lastRoom.left != null && lastRoom.left.myActivities != null) { lastRoom.left.myActivities.DisableRigidBodys(); } yield return(new WaitForSeconds(0.02f)); if (lastRoom.right != currentRoom && lastRoom.right != null && lastRoom.right.myActivities != null) { lastRoom.right.myActivities.DisableRigidBodys(); } yield return(new WaitForSeconds(0.03f)); if (lastRoom.up != currentRoom && lastRoom.up != null && lastRoom.up.myActivities != null) { lastRoom.up.myActivities.DisableRigidBodys(); } yield return(new WaitForSeconds(0.04f)); if (lastRoom.down != currentRoom && lastRoom.down != null && lastRoom.down.myActivities != null) { lastRoom.down.myActivities.DisableRigidBodys(); } } yield return(new WaitForSeconds(0.05f)); if (currentRoom.up != null && currentRoom.up.myActivities != null) { currentRoom.up.myActivities.EnableRigidBodys(); } yield return(new WaitForSeconds(0.06f)); if (currentRoom.down != null && currentRoom.down.myActivities != null) { currentRoom.down.myActivities.EnableRigidBodys(); } yield return(new WaitForSeconds(0.07f)); if (currentRoom.left != null && currentRoom.left.myActivities != null) { currentRoom.left.myActivities.EnableRigidBodys(); } yield return(new WaitForSeconds(0.08f)); if (currentRoom.right != null && currentRoom.right.myActivities != null) { currentRoom.right.myActivities.EnableRigidBodys(); } } } } }
public void CorridorSetup(RoomGen room, RandomIntGen length, RandomIntGen roomWidth, RandomIntGen roomHeight, int columns, int rows, bool firstCorridor) { // Set a random direction (random index from 0-3, cast to Direction). dir = (Dir)Random.Range(0, 4); // Find direction opposite to one entering room current corridor is leaving from. // Cast prev corridor direction to int between 0 and 3 then add 2, then find remainder Dir oppositeDirection = (Dir)(((int)room.CorridorEntry + 2) % 4); // If this is not the first corridor and the randomly selected direction is opposite to the previous corridor's direction... if (!firstCorridor && dir == oppositeDirection) { // Rotate the direction 90 degrees clockwise (North becomes East, East becomes South.) int directionVal = (int)dir; directionVal++; directionVal = directionVal % 4; dir = (Dir)directionVal; } // Set random length. corridorLength = length.Random; // Create cap from randomvalue script for how long it can be.. int max_Length = length.maximumVal; switch (dir) { // If the choosen direction is North case Dir.North: // starting position in the x axis can be random but within the width of the room. startXPos = Random.Range(room.xPosition, room.xPosition + room.roomWidth - 1); // starting position in the y axis must be the top of the room. startYPos = room.yPosition + room.roomHeight; // maximum length the corridor can be is the height of the board (rows) but from the top of the room (y pos + height). max_Length = rows - startYPos - roomHeight.minimumvVal; break; case Dir.East: startXPos = room.xPosition + room.roomWidth; startYPos = Random.Range(room.yPosition, room.yPosition + room.roomHeight - 1); max_Length = columns - startXPos - roomWidth.minimumvVal; break; case Dir.South: startXPos = Random.Range(room.xPosition, room.xPosition + room.roomWidth); startYPos = room.yPosition; max_Length = startYPos - roomHeight.minimumvVal; break; case Dir.West: startXPos = room.xPosition; startYPos = Random.Range(room.yPosition, room.yPosition + room.roomHeight); max_Length = startXPos - roomWidth.minimumvVal; break; } // clamp the corridor length to make sure it doesn't go off the board. corridorLength = Mathf.Clamp(corridorLength, 1, max_Length); }
public RoomGenOption(RoomGen <MapGenContext> gridOption, RoomGen <ListMapGenContext> listOption, List <BaseRoomFilter> filters) { GridOption = gridOption; ListOption = listOption; Filters = filters; }
public override void ApplyToPath(IRandom rand, GridPlan floorPlan) { for (int xx = 0; xx < floorPlan.GridWidth - 1; xx++) { for (int yy = 0; yy < floorPlan.GridHeight - 1; yy++) { //check for room presence in all rooms (must be SINGLE and immutable) if (!roomViable(floorPlan, xx, yy)) { continue; } if (!roomViable(floorPlan, xx, yy + 1)) { continue; } if (!roomViable(floorPlan, xx + 1, yy)) { continue; } if (!roomViable(floorPlan, xx + 1, yy + 1)) { continue; } //check for hall connectivity in all constituent halls if (floorPlan.GetHall(new LocRay4(xx, yy, Dir4.Down)) == null) { continue; } if (floorPlan.GetHall(new LocRay4(xx, yy, Dir4.Right)) == null) { continue; } if (floorPlan.GetHall(new LocRay4(xx + 1, yy, Dir4.Down)) == null) { continue; } if (floorPlan.GetHall(new LocRay4(xx, yy + 1, Dir4.Right)) == null) { continue; } if (rand.Next(100) < CombineChance) { //erase the constituent rooms floorPlan.EraseRoom(new Loc(xx, yy)); floorPlan.EraseRoom(new Loc(xx + 1, yy)); floorPlan.EraseRoom(new Loc(xx, yy + 1)); floorPlan.EraseRoom(new Loc(xx + 1, yy + 1)); //erase the constituent halls floorPlan.SetHall(new LocRay4(xx, yy, Dir4.Down), null, new ComponentCollection()); floorPlan.SetHall(new LocRay4(xx, yy, Dir4.Right), null, new ComponentCollection()); floorPlan.SetHall(new LocRay4(xx + 1, yy, Dir4.Down), null, new ComponentCollection()); floorPlan.SetHall(new LocRay4(xx, yy + 1, Dir4.Right), null, new ComponentCollection()); //place the room RoomGen <T> gen = GiantRooms.Pick(rand); floorPlan.AddRoom(new Rect(xx, yy, 2, 2), gen.Copy(), this.RoomComponents.Clone(), false); } } } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here grid = new Grid(); inventory = new Inventory(); player = new Player(grid, inventory); // Whew, avoided circular dependencies! inventory.SetPlayer(player); Creature.grid_position wand_pos = new Creature.grid_position(); wand_pos.X = 8; wand_pos.Y = 8; wand = new TeleportWand(grid, wand_pos, player); //creature = new Creature(grid); wall = new Wall(grid); room_gen = new RoomGen(); room_gen.Initialize(); base.Initialize(); }
public override void ApplyToPath(IRandom rand, GridPlan floorPlan) { int gapLength = Vertical ? floorPlan.GridHeight : floorPlan.GridWidth; int sideLength = Vertical ? floorPlan.GridWidth : floorPlan.GridHeight; if (gapLength < 3 || sideLength < 2) { CreateErrorPath(rand, floorPlan); return; } //add the body int chosenTier = FromCorners ? (rand.Next(2) * gapLength - 1) : rand.Next(1, gapLength - 1); RoomGen <T> roomGen = GiantHallGen.Pick(rand); if (roomGen == null) { roomGen = GenericRooms.Pick(rand); } floorPlan.AddRoom(new Rect(Vertical ? 0 : chosenTier, Vertical ? chosenTier : 0, Vertical ? sideLength : 1, Vertical ? 1 : sideLength), roomGen, this.LargeRoomComponents.Clone()); GenContextDebug.DebugProgress("Center Room"); //add the legs for (int ii = 0; ii < sideLength; ii++) { if (chosenTier > 0) { if (rand.Next(100) < LegPercent) { int roomTier = rand.Next(0, chosenTier); floorPlan.AddRoom(new Loc(Vertical ? ii : roomTier, Vertical ? roomTier : ii), GenericRooms.Pick(rand), this.RoomComponents.Clone()); for (int jj = roomTier; jj < chosenTier; jj++) { SafeAddHall(new LocRay4(new Loc(Vertical ? ii : jj, Vertical ? jj : ii), Vertical ? Dir4.Down : Dir4.Right), floorPlan, GenericHalls.Pick(rand), GetDefaultGen(), this.RoomComponents, this.HallComponents, true); } GenContextDebug.DebugProgress("Add Leg"); int hasRoom = -1; for (int jj = ii - 1; jj >= 0; jj--) { if (floorPlan.GetRoomPlan(new Loc(Vertical ? jj : roomTier, Vertical ? roomTier : jj)) != null) { hasRoom = jj; break; } } if (ii > 0 && hasRoom > -1) { if (rand.Next(100) < ConnectPercent) { for (int jj = ii; jj > hasRoom; jj--) { SafeAddHall(new LocRay4(new Loc(Vertical ? jj : roomTier, Vertical ? roomTier : jj), Vertical ? Dir4.Left : Dir4.Up), floorPlan, GenericHalls.Pick(rand), GetDefaultGen(), this.RoomComponents, this.HallComponents, true); GenContextDebug.DebugProgress("Connect Leg"); } } } } } if (chosenTier < gapLength - 1) { if (rand.Next(100) < LegPercent) { int roomTier = rand.Next(chosenTier + 1, gapLength); floorPlan.AddRoom(new Loc(Vertical ? ii : roomTier, Vertical ? roomTier : ii), GenericRooms.Pick(rand), this.RoomComponents.Clone()); for (int jj = chosenTier; jj < roomTier; jj++) { SafeAddHall(new LocRay4(new Loc(Vertical ? ii : jj, Vertical ? jj : ii), Vertical ? Dir4.Down : Dir4.Right), floorPlan, GenericHalls.Pick(rand), GetDefaultGen(), this.RoomComponents, this.HallComponents, true); } GenContextDebug.DebugProgress("Add Leg"); int hasRoom = -1; for (int jj = ii - 1; jj >= 0; jj--) { if (floorPlan.GetRoomPlan(new Loc(Vertical ? jj : roomTier, Vertical ? roomTier : jj)) != null) { hasRoom = jj; break; } } if (ii > 0 && hasRoom > -1) { if (rand.Next(100) < ConnectPercent) { for (int jj = ii; jj > hasRoom; jj--) { SafeAddHall(new LocRay4(new Loc(Vertical ? jj : roomTier, Vertical ? roomTier : jj), Vertical ? Dir4.Left : Dir4.Up), floorPlan, GenericHalls.Pick(rand), GetDefaultGen(), this.RoomComponents, this.HallComponents, true); GenContextDebug.DebugProgress("Connect Leg"); } } } } } } }