SHARE
TWEET

Untitled

a guest Sep 14th, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Reference
  2.  
  3. class_name Leaf # used for the BSP tree algorithm
  4. #func __init__(x, y, width, height):
  5.    
  6. var x
  7. var y
  8. var width
  9. var height
  10. export var MIN_LEAF_SIZE = 10
  11. var child_1 = null
  12. var child_2 = null
  13. var room = null
  14. var hall = null
  15.  
  16. func splitLeaf():
  17.     # begin splitting the leaf into two children
  18.     if (child_1 != null) or (child_2 != null):
  19.         return false # This leaf has already been split
  20.  
  21.     """
  22.     ==== Determine the direction of the split ====
  23.     If the width of the leaf is >25% larger than the height,
  24.     split the leaf vertically.
  25.     If the height of the leaf is >25 larger than the width,
  26.     split the leaf horizontally.
  27.     Otherwise, choose the direction at random.
  28.     """
  29.     var splitHorizontally = randi() % 2
  30.     if (width/height >= 1.25):
  31.         splitHorizontally = false
  32.     elif (height/width >= 1.25):
  33.         splitHorizontally = true
  34.  
  35.     var maxSize
  36.     if (splitHorizontally):
  37.         maxSize = height - MIN_LEAF_SIZE
  38.     else:
  39.         maxSize = width - MIN_LEAF_SIZE
  40.  
  41.     if (maxSize <= MIN_LEAF_SIZE):
  42.         return false # the leaf is too small to split further
  43.  
  44.     var leafRange = range(MIN_LEAF_SIZE,maxSize)
  45.     var split = leafRange[randi() % leafRange.size()] #determine where to split the leaf
  46.  
  47.     if (splitHorizontally):
  48.         child_1 = Leaf.new(x, y, width, split)
  49.         child_2 = Leaf.new(x, y+split, width, height-split)
  50.     else:
  51.         child_1 = Leaf.new(x, y,split, height)
  52.         child_2 = Leaf.new(x + split, y, width-split, height)
  53.  
  54.     return true
  55.  
  56. func createRooms(bspTree):
  57.     if (child_1) or (child_2):
  58.         # recursively search for children until you hit the end of the branch
  59.         if (child_1):
  60.             child_1.createRooms(bspTree)
  61.         if (child_2):
  62.             child_2.createRooms(bspTree)
  63.  
  64.         if (child_1 and child_2):
  65.             bspTree.createHall(child_1.getRoom(),
  66.                 child_2.getRoom())
  67.  
  68.     else:
  69.     # Create rooms in the end branches of the bsp tree
  70.         var wRange = range(bspTree.ROOM_MIN_SIZE, min(bspTree.ROOM_MAX_SIZE,width-1))
  71.         var hRange = range(bspTree.ROOM_MIN_SIZE, min(bspTree.ROOM_MAX_SIZE,height-1))
  72.         var w = wRange[randi() & wRange.size()]
  73.         var h = hRange[randi() & hRange.size()]
  74.         var xRange = range(x, x+(width-1)-w)
  75.         var yRange = range(y, y+(height-1)-h)
  76.         var x = xRange[randi() & xRange.size()]
  77.         var y = yRange[randi() & yRange.size()]
  78.         var room = Rect2(x,y,w,h)
  79.         bspTree.createRoom(room)
  80.  
  81. func getRoom():
  82.     var room_1
  83.     var room_2
  84.     if (room):
  85.         return room
  86.     else:
  87.         if (child_1):
  88.             room_1 = child_1.getRoom()
  89.         if (child_2):
  90.             room_2 = child_2.getRoom()
  91.  
  92.         if (not child_1 and not child_2):
  93.             # neither room_1 nor room_2
  94.             return null
  95.  
  96.         elif (not room_2):
  97.             # room_1 and !room_2
  98.             return room_1
  99.  
  100.         elif (not room_1):
  101.             # room_2 and !room_1
  102.             return room_2
  103.  
  104.         # If both room_1 and room_2 exist, pick one
  105.         elif (randf() < 0.5):
  106.             return room_1
  107.         else:
  108.             return room_2
  109.  
  110. func _init(newX, newY, newWidth, newHeight) -> void:
  111.     x = newX
  112.     y = newY
  113.     width = newWidth
  114.     height = newHeight
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top