Guest User

Untitled

a guest
Jul 7th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. extends Node
  2.  
  3. class_name Rect
  4. var x1
  5. var y1
  6. var x2
  7. var y2
  8.  
  9. func init(x, y, w, h):
  10.     self.x1 = x
  11.     self.y1 = y
  12.     self.x2 = x + w
  13.     self.y2 = y + h
  14.  
  15. func center():
  16.     var center_x = int((x1 + x2) / 2)
  17.     var center_y = int((y1 + y2) / 2)
  18.     return ([center_x, center_y])
  19.  
  20. func intersect(other):
  21.     # returns true if this rectangle intersects with another one
  22.     return (self.x1 <= other.x2 and self.x2 >= other.x1 and
  23.         self.y1 <= other.y2 and self.y2 >= other.y1)
  24.        
  25.  
  26.  
  27.  
  28. _make_map(DungeonSize.max_rooms, DungeonSize.room_min_size, DungeonSize.room_max_size,
  29.         DungeonSize.width, DungeonSize.height)
  30.  
  31.  
  32. func _make_map(max_rooms, room_min_size, room_max_size, map_width, map_height) -> void:
  33.     var rng = RandomNumberGenerator.new()
  34.     randomize()
  35.    
  36.     var num_rooms = 0
  37.    
  38.     for r in range(0,max_rooms):
  39.         rng.randomize()
  40.         #rand height and wdith
  41.         var w = rng.randi_range(room_min_size, room_max_size)
  42.         var h = rng.randi_range(room_min_size, room_max_size)
  43.         #random position without going out of the boundaries of the map
  44.         var x = rng.randi_range(0, map_width - w - 1)
  45.         var y = rng.randi_range(0, map_height - h - 1)
  46.        
  47.         #print("new rect: x-"+str(x) + " y-"+str(y) + " w-" + str(w))
  48.        
  49.         # "Rect" class makes rectangles easier to work with
  50.         var new_room: Rect = Rect.new()
  51.         new_room.init(x, y, w, h)
  52.        
  53.         if num_rooms == 0:
  54.             #this is first room so add manually
  55.             _create_room(new_room)
  56.             rooms.append(new_room)
  57.             num_rooms+=1
  58.             continue
  59.         # run through the other rooms and see if they intersect with this one
  60.         for other_room in rooms:
  61.             if new_room.intersect(other_room) or num_rooms >= max_rooms:
  62.                 #print("new center: " + str(new_room.center()))
  63.                 #print("other center: " + str(other_room.center()))
  64.                 #print("intersect!")
  65.                 break
  66.             else:
  67.                 #no intersections
  68.                 # "paint it"
  69.                 _create_room(new_room)
  70.                
  71.                 #center coords of new room will be useful later
  72.                 var new_room_coords = new_room.center()
  73.                 if num_rooms == 0:
  74.                     #this is first room, where we can spawn player
  75.                     continue
  76.                 else:
  77.                     #all rooms after first
  78.                     #connect it to previous room with tunnel
  79.                    
  80.                     #cent coords from previous
  81.                     var old_room_coords = rooms[num_rooms -1].center()
  82.                    
  83.                     #flip a coin 0-1
  84.                     var flip = randi()%1
  85.                     if flip == 1:
  86.                         #first move horizon then vert
  87.                         create_h_tunnel(old_room_coords[0], new_room_coords[0], old_room_coords[1])
  88.                         create_v_tunnel(old_room_coords[1], new_room_coords[1], new_room_coords[0])
  89.                     else:
  90.                         #first more vert than horizon
  91.                         create_v_tunnel(old_room_coords[1], new_room_coords[1], old_room_coords[0])
  92.                         create_h_tunnel(old_room_coords[0], new_room_coords[0], new_room_coords[1])
  93.            
  94.                        
  95.         #finally append room
  96.             rooms.append(new_room)
  97.             num_rooms+=1
Advertisement
Add Comment
Please, Sign In to add comment