public void ApplyRoom(Room room) { for (int _y = 0; _y < room.height; _y++) { for (int _x = 0; _x < room.width; _x++) { worldmap [room.pos.x + _x, room.pos.y + _y, room.pos.z] = '.'; } } }
public Room[] FindNearestRooms(Room room, int distance) { List<Room> selected = new List<Room> (); foreach (Room otherRoom in rooms) { if(Cord.Distance(room.pos,otherRoom.pos) <= distance) selected.Add(otherRoom); } return selected.ToArray(); }
public void Generate() { Random rand = new Random (); //Set all tiles to soild for (int z = 0; z < worldmap.GetLength(2); z++) { for (int x = 0; x < worldmap.GetLength(0); x++) { for (int y = 0; y < worldmap.GetLength(1); y++) { worldmap [x, y, z] = '#'; } } //Create rooms for (int r = 0; r < rand.Next(150,300); r++) { int width = rand.Next (3, 15), height = rand.Next (3, 10); int cx = 0, cy = 0; bool failed = true; while (failed) { while ((cx - width) < 25 || (cx + width) > 486 || (cy - height) < 25 || (cy + height) > 486) { cx = rand.Next (0, 511); cy = rand.Next (0, 511); } Room potential = new Room(new Cord (cx, cy, z), width, height); if(r == 0){failed = false; }; foreach (Room room in rooms) { if (!potential.Intersects (room)) failed = false; } } Room newRoom = new Room(new Cord (cx, cy, z), width, height); if (rooms.Count () > 1) { //Create a tunnel. Room[] selectedr = FindNearestRooms(newRoom,Consts.MAX_TUNNEL_DIST); Room oldRoom; if(selectedr.Count() > 0) { int roomid = rand.Next(1,selectedr.Count()) - 1; oldRoom = rooms[rooms.IndexOf(selectedr[roomid])]; Cord oldCord = oldRoom.getCenter(); Cord newCord = newRoom.getCenter(); int minx = Math.Min(newCord.x,oldCord.x); int maxx = Math.Max(newCord.x,oldCord.x); int miny = Math.Min(newCord.y,oldCord.y); int maxy = Math.Max(newCord.y,oldCord.y); int oldy = oldCord.y; int oldx = oldCord.x; //Horizontal tunnel. for(int x = minx; x < maxx; x++) { worldmap[x,oldy,z] = '.'; } //Vertical tunnel. for(int y = miny; y < maxy; y++) { worldmap[oldx,y,z] = '.'; } } } ApplyRoom(newRoom); rooms.Add(newRoom); } } }
public bool Intersects(Room otherRoom) { bool x_i = false,y_i = false; int r_xmin = pos.x; int r_xmax = pos.x + width; int r_ymin = pos.y; int r_ymax = pos.y + height; int or_xmin = otherRoom.pos.x; int or_xmax = otherRoom.pos.x + otherRoom.width; int or_ymin = otherRoom.pos.y; int or_ymax = otherRoom.pos.y + otherRoom.height; if (r_xmin >= or_xmin && r_xmin <= or_xmax) //Is the room along the x side. x_i = true; if(r_ymin >= or_ymin && r_ymin <= or_ymax) y_i = true; if(x_i && y_i) return true; else return false; }