Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- class_name Rect
- var x1
- var y1
- var x2
- var y2
- func init(x, y, w, h):
- self.x1 = x
- self.y1 = y
- self.x2 = x + w
- self.y2 = y + h
- func center():
- var center_x = int((x1 + x2) / 2)
- var center_y = int((y1 + y2) / 2)
- return ([center_x, center_y])
- func intersect(other):
- # returns true if this rectangle intersects with another one
- return (self.x1 <= other.x2 and self.x2 >= other.x1 and
- self.y1 <= other.y2 and self.y2 >= other.y1)
- _make_map(DungeonSize.max_rooms, DungeonSize.room_min_size, DungeonSize.room_max_size,
- DungeonSize.width, DungeonSize.height)
- func _make_map(max_rooms, room_min_size, room_max_size, map_width, map_height) -> void:
- var rng = RandomNumberGenerator.new()
- randomize()
- var num_rooms = 0
- for r in range(0,max_rooms):
- rng.randomize()
- #rand height and wdith
- var w = rng.randi_range(room_min_size, room_max_size)
- var h = rng.randi_range(room_min_size, room_max_size)
- #random position without going out of the boundaries of the map
- var x = rng.randi_range(0, map_width - w - 1)
- var y = rng.randi_range(0, map_height - h - 1)
- #print("new rect: x-"+str(x) + " y-"+str(y) + " w-" + str(w))
- # "Rect" class makes rectangles easier to work with
- var new_room: Rect = Rect.new()
- new_room.init(x, y, w, h)
- if num_rooms == 0:
- #this is first room so add manually
- _create_room(new_room)
- rooms.append(new_room)
- num_rooms+=1
- continue
- # run through the other rooms and see if they intersect with this one
- for other_room in rooms:
- if new_room.intersect(other_room) or num_rooms >= max_rooms:
- #print("new center: " + str(new_room.center()))
- #print("other center: " + str(other_room.center()))
- #print("intersect!")
- break
- else:
- #no intersections
- # "paint it"
- _create_room(new_room)
- #center coords of new room will be useful later
- var new_room_coords = new_room.center()
- if num_rooms == 0:
- #this is first room, where we can spawn player
- continue
- else:
- #all rooms after first
- #connect it to previous room with tunnel
- #cent coords from previous
- var old_room_coords = rooms[num_rooms -1].center()
- #flip a coin 0-1
- var flip = randi()%1
- if flip == 1:
- #first move horizon then vert
- create_h_tunnel(old_room_coords[0], new_room_coords[0], old_room_coords[1])
- create_v_tunnel(old_room_coords[1], new_room_coords[1], new_room_coords[0])
- else:
- #first more vert than horizon
- create_v_tunnel(old_room_coords[1], new_room_coords[1], old_room_coords[0])
- create_h_tunnel(old_room_coords[0], new_room_coords[0], new_room_coords[1])
- #finally append room
- rooms.append(new_room)
- num_rooms+=1
Advertisement
Add Comment
Please, Sign In to add comment