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); } }
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); }
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)); } } }