public bool Intersects(Room r1) { Room r2 = this; return !(r2.cornerNW.X > r1.cornerSE.X - 1 || r2.cornerSE.X - 1 < r1.cornerNW.X || r2.cornerNW.Y > r1.cornerSE.Y - 1 || r2.cornerSE.Y - 1 < r1.cornerNW.Y); }
public void CheckForDoors(Room room) { Room bigRoom = new Room(controller, this, new Point(room.cornerNW.X - 1, room.cornerNW.Y - 1), room.cornerSE); Point tempPoint = new Point(0, 0); Point corner1 = new Point(bigRoom.cornerSE.X, bigRoom.cornerNW.Y); Point corner2 = new Point(bigRoom.cornerNW.X, bigRoom.cornerSE.Y); for (int ix = bigRoom.cornerNW.X; ix < bigRoom.cornerSE.X + 1; ix++) { for (int iy = bigRoom.cornerNW.Y; iy < bigRoom.cornerSE.Y + 1; iy++) { tempPoint.X = ix; tempPoint.Y = iy; if (OutOfBounds(tempPoint)) { continue; } if (mapArray[ix, iy] == (int)Element.Nothing && !room.ContainsPoint(tempPoint) && tempPoint != bigRoom.cornerNW && tempPoint != bigRoom.cornerSE && tempPoint != corner1 && tempPoint != corner2) { foreach (Room r in roomList) { if (r.ContainsPoint(tempPoint)) { Point pointLeft, pointRight, pointUp, pointDown; pointLeft = new Point(tempPoint.X - 1, tempPoint.Y); pointRight = new Point(tempPoint.X + 1, tempPoint.Y); pointUp = new Point(tempPoint.X, tempPoint.Y - 1); pointDown = new Point(tempPoint.X, tempPoint.Y + 1); //top || bottom if ((bigRoom.cornerNW.Y == room.cornerNW.Y - 1 || bigRoom.cornerSE.Y == room.cornerSE.Y) && (r.width == 1)) { if (IsElement(pointLeft, Element.Wall) && IsElement(pointRight, Element.Wall) && mapArray[pointUp.X, pointUp.Y] != (int)Element.Door && mapArray[pointDown.X, pointDown.Y] != (int)Element.Door && mapArray[pointUp.X, pointUp.Y] != (int)Element.Wall && mapArray[pointDown.X, pointDown.Y] != (int)Element.Wall) { mapArray[ix, iy] = (int)Element.Door; } } //left || right else if ((bigRoom.cornerNW.X == room.cornerNW.X - 1 || bigRoom.cornerSE.X == room.cornerSE.X) && r.height == 1) { if (IsElement(pointUp, Element.Wall) && IsElement(pointDown, Element.Wall) && mapArray[pointLeft.X, pointLeft.Y] != (int)Element.Door && mapArray[pointRight.X, pointRight.Y] != (int)Element.Door && mapArray[pointLeft.X, pointLeft.Y] != (int)Element.Wall && mapArray[pointRight.X, pointRight.Y] != (int)Element.Wall) { //SPAWN DOOR mapArray[ix, iy] = (int)Element.Door; } } } } } } } }
private void GenerateRandomIsolatedRoom(int minX, int minY, int maxX, int maxY) { int a, b, c, d; bool isIsolated; int timeOut = 10000; do { timeOut--; isIsolated = true; Room tempRoom; a = controller.random.Next(1, floorSize - 1); b = controller.random.Next(1, floorSize - 1); c = controller.random.Next(a, floorSize); d = controller.random.Next(b, floorSize); if (c - a >= minX && d - b >= minY && c - a <= maxX && d - b <= maxY) { tempRoom = new Room(controller, this, new Point(a, b), new Point(c, d)); foreach (Room r in roomList) { if (r.Intersects(tempRoom)) { isIsolated = false; } } } else { isIsolated = false; } } while (!isIsolated && timeOut > 0); if (timeOut == 0) { return; } GenerateRoom(new Point(a, b), new Point(c, d)); bigRoomList.Add(roomList[roomList.Count - 1]); }
/*public bool IsIsolated(Room room) { //DOESN'T WORK WITH ROOMS INSIDE OTHER ROOMS Room tempRoom = new Room(controller, this, new Point(room.cornerNW.X - 1, room.cornerNW.Y - 1), new Point(room.cornerSE.X, room.cornerSE.Y)); Point tempPoint = new Point(0, 0); Point corner1 = new Point(tempRoom.cornerSE.X, tempRoom.cornerNW.Y); Point corner2 = new Point(tempRoom.cornerNW.X, tempRoom.cornerSE.Y); for (int ix = tempRoom.cornerNW.X; ix < tempRoom.cornerSE.X + 1; ix++) { for (int iy = tempRoom.cornerNW.Y; iy < tempRoom.cornerSE.Y + 1; iy++) { tempPoint.X = ix; tempPoint.Y = iy; if (OutOfBounds(tempPoint)) { continue; } if (mapArray[ix, iy] == 0 && !room.ContainsPoint(tempPoint)) { if (tempPoint != tempRoom.cornerNW && tempPoint != tempRoom.cornerSE && tempPoint != corner1 && tempPoint != corner2) { return false; } } } } return true; }*/ public bool IsIsolated(Room room) { foreach (Room r in roomList) { if (room.Intersects(r) && r != room) { return false; } } return true; }
public void CreateRandomLevel(int numberOfRooms, int roomSizeX, int roomSizeY, int maxRoomSizeX, int maxRoomSizeY) { this.player = controller.player; ResetFloorVars(); while (roomList.Count == 0 || !CheckIfAllConnected(roomList)) { ResetFloorVars(); //CREATE BIG ROOMS for (int i = 0; i < numberOfRooms; i++) { GenerateRandomIsolatedRoom(roomSizeX, roomSizeY, maxRoomSizeX, maxRoomSizeY); } //CORRIDORS for (int i = 0; i < roomList.Count; i++) { Room tempRoom; int x1, x2, y1, y2; for (int i2 = 0; i2 < roomList.Count; i2++) { if (roomList[i].cornerNW.X <= roomList[i2].cornerNW.X) { x1 = roomList[i].cornerNW.X; x2 = roomList[i2].cornerSE.X; } else { x1 = roomList[i2].cornerNW.X; x2 = roomList[i].cornerSE.X; } tempRoom = new Room(controller, this, new Point(x1, roomList[i].cornerNW.Y), new Point(x2, roomList[i].cornerNW.Y + 1)); if (roomList[i2] != roomList[i]) { if (tempRoom.Intersects(roomList[i2])) { GenerateRoom(tempRoom.cornerNW, tempRoom.cornerSE); break; } } } for (int i2 = 0; i2 < roomList.Count; i2++) { if (roomList[i].cornerNW.Y <= roomList[i2].cornerNW.Y) { y1 = roomList[i].cornerNW.Y; y2 = roomList[i2].cornerSE.Y; } else { y1 = roomList[i2].cornerNW.Y; y2 = roomList[i].cornerSE.Y; } tempRoom = new Room(controller, this, new Point(roomList[i].cornerNW.X, y1), new Point(roomList[i].cornerNW.X + 1, y2)); if (tempRoom.Intersects(roomList[i2]) && roomList[i2] != roomList[i] /*&& IsIsolated(rm)*/) { GenerateRoom(tempRoom.cornerNW, tempRoom.cornerSE); break; } } } //ELIMINATE ISOLATED ROOMS for (int i = roomList.Count - 1; i > -1; i--) { if (IsIsolated(roomList[i]) && roomList.Count > 0) { for (int ix = roomList[i].cornerNW.X; ix < roomList[i].cornerSE.X; ix++) { for (int iy = roomList[i].cornerNW.Y; iy < roomList[i].cornerSE.Y; iy++) { mapArray[ix, iy] = (int)Element.Wall; } } for (int ib = bigRoomList.Count - 1; ib > -1; ib--) { if (bigRoomList[ib] == roomList[i]) { bigRoomList.RemoveAt(ib); break; } } roomList.RemoveAt(i); } } } //MAKE DOORS foreach (Room r in roomList) { CheckForDoors(r); } //STAIRS bool posInBig; Point stairs; do { posInBig = false; stairs = GenerateFreePos(); foreach (Room r in bigRoomList) { if (r.ContainsPoint(stairs)) { posInBig = true; break; } } } while (!posInBig); mapArray[stairs.X, stairs.Y] = (int)Element.Stairs; //PLAYER do { posInBig = false; player.position = GenerateFreePos(); foreach (Room r in bigRoomList) { if (r.ContainsPoint(player.position)) { posInBig = true; break; } } } while (!posInBig); //GENERATING ITEMS Point temp; bool overlap = false; for (int i = 0; i < bigRoomList.Count; i++) { overlap = false; temp = GenerateFreePos(); foreach (Item it in itemList) { if (temp == it.position) { overlap = true; } } if (temp != Point.Zero && temp != player.position && !overlap) { itemList.Add(new Item(controller, temp, (Map.Element)mapArray[temp.X, temp.Y])); mapArray[temp.X, temp.Y] = (int)Element.Item; } } //ENEMIES overlap = false; for (int i = 0; i < bigRoomList.Count + (int)controller.level / 10; i++) { overlap = false; temp = GenerateFreePos(); foreach (Enemy e in enemyList) { if (temp == e.position) { overlap = true; } } if (temp != Point.Zero && temp != player.position && !overlap) { Type t = typeof(Enemy.EnemyType); enemyList.Add(new Enemy(controller, temp, (Enemy.EnemyType)controller.random.Next(Enum.GetValues(t).Length))); } } mapString = ArrayToString(mapArray); controller.camera.ResetPosition(); GenerateFog(); GenerateMinimapFog(); }