Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- var floorRect
- var _rooms = []
- var windingPercent = 40 #change this
- var _currentRegion = 0
- var directionsCardinal = {"n": Vector2(0,-1), "e": Vector2(1,0), "s": Vector2(0,1), "w": Vector2(-1,0) }
- var directionsIntercardinal = { "ne": Vector2(1,-1), "se": Vector2(1,1), "sw": Vector2(-1,1), "nw": Vector2(-1,-1)}
- var directionsAll = {"none":Vector2(0,0)}
- var cellSize = 0
- func gen(_floorRect, _cellSize):
- var _rooms = []
- for direct in directionsCardinal.keys():
- directionsAll[direct] = directionsCardinal[direct]
- for direct in directionsIntercardinal.keys():
- directionsAll[direct] = directionsIntercardinal[direct]
- floorRect = _floorRect
- cellSize = _cellSize
- tileFill(_floorRect, "wall")
- print("tileFill tile index:",get_node("/root/Main/dungeonLevel").get_cellv(Vector2(0,0)))
- roomGen(_floorRect)
- var y = 1
- while y < floorRect.end.y:
- y +=2
- var x = 1
- while x < floorRect.end.x:
- var pos = Vector2(x,y)
- if get_node("/root/Main/dungeonLevel").get_cellv(pos) != 0:
- x +=2
- continue
- elif get_node("/root/Main/dungeonLevel").get_cellv(pos + directionsCardinal["s"]) != 0 or get_node("/root/Main/dungeonLevel").get_cellv(pos + directionsCardinal["n"]) != 0 or get_node("/root/Main/dungeonLevel").get_cellv(pos + directionsCardinal["e"]) != 0 or get_node("/root/Main/dungeonLevel").get_cellv(pos + directionsCardinal["w"]) != 0:
- x +=2
- continue
- else:
- growMaze(pos)
- x +=2
- func tileFill(areaRect, tileType):
- var areaRectEnd = areaRect.end
- var x = areaRect.position.x
- var y = areaRect.position.y
- var endX = areaRect.end.x + 1
- var endY = areaRect.end.y + 1
- var tile = tileIndex(tileType)
- while x < endX :
- while y < endY :
- var currentCell = get_node("/root/Main/dungeonLevel").set_cell(x,y,tile)
- y += 1
- y = areaRect.position.y
- x += 1
- func roomGen(floorRect):
- var numRoomTries = 10
- var roomExtraSize = 2
- var i = 0
- for i in numRoomTries + 1:
- i += 1
- ## might have to change this to round
- var size = int(rand_range(1,3 + roomExtraSize)) * 2 + 1
- var rectangularity = int(rand_range(0,1 + size / 2)) * 2
- var width = size
- var height = size
- var rngPick = int(rand_range(1,2))
- if rngPick == 1:
- width += rectangularity
- else:
- height += rectangularity
- var x = int(rand_range(0, floorRect.end.x - width) / 2) * 2 + 1
- var y = int(rand_range(0, floorRect.end.y - height) / 2) * 2 + 1
- var room = Rect2(x,y,width,height)
- var overlaps = false
- for other in _rooms:
- #print(int(room.position.distance_to(other.position)))
- #print("room end",room.end)
- #print("room start",room.position)
- #if room.end.distance_to(other.end) <= 0 or room.position.distance_to(other.position) <= 0:
- if intersects(room.position, room.end, other.position, other.end):
- overlaps = true
- break
- if overlaps:
- continue
- _rooms.append(room)
- _currentRegion +=1
- tileFill(room, "floor")
- #add code for making regios here
- func intersects(r1from, r1to, r2from, r2to):
- return !(r2from.x >= r1to.x +2 || r2to.x <= r1from.x -2 || r2from.y >= r1to.y +2 || r2to.y <= r1from.y -2)
- func tileIndex(tileType):
- var tileDict = {"wall": 0, "floor": 1, "other":2, "water":3}
- if tileType in tileDict:
- return tileDict[tileType]
- else:
- print("ERROR: INVALID TILE")
- get_tree().quit()
- func _direction(direction): #might need to make this function first
- #WHEN YOU HANDLE THIS LATER YOU JUST HAVE TO ADD THE DIRECTION TO THE POS/VECTOR AND IT MOVES IT IN THAT DIRECTION
- pass
- func growMaze(start):
- var cells = [] #these are all vects
- var lastDir
- _currentRegion +=1
- _carve(start, "other")
- cells.append(start)
- while not cells.empty():
- #print("cells: ", len(cells))
- var cell = cells[-1]
- var unmadeCells = []
- for dir in directionsCardinal:
- #print(directionsCardinal[dir])
- if _canCarve(cell, directionsCardinal[dir]):
- unmadeCells.append(directionsCardinal[dir])
- print("unmade cells len",len(unmadeCells))
- if not unmadeCells.empty():
- var dir
- var rngChoice = int(randi() % 101)
- if unmadeCells.has(lastDir) and rngChoice > windingPercent:
- dir = lastDir
- else:
- dir = unmadeCells[int(randi()%len(unmadeCells))] #minus 1? plus 1??
- _carve(cell+dir, "water")
- _carve(cell+dir*2, "water")
- cells.append(cell+dir*2)
- lastDir = dir
- else:
- cells.pop_back()
- lastDir = null
- func _canCarve(pos, direction):
- var _floorRect = floorRect
- print("just pos ",pos)
- print("just direction ",direction)
- print("pos + direction ",pos + direction)
- print("pos + direction * 3 ",pos + direction * 3)
- print("pos + direction * 3 + north",pos + direction * 3 + directionsCardinal["n"])
- if not 0 < pos.x + direction.x and not pos.x + direction.x < floorRect.end.x and not 0 < pos.y + direction.y and not pos.y + direction.y < floorRect.end.y:
- return false
- if get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 2) == 0:
- if direction == directionsCardinal["n"] or direction == directionsCardinal["s"]:
- var directionTile1 = get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 3 + directionsCardinal["e"])
- var directionTile2 = get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 3 + directionsCardinal["w"])
- if directionTile1 != 0:
- return false
- elif directionTile2 != 0:
- return false
- else:
- print("can carve? intersects",intersects(pos + direction * 3,pos + direction * 3, floorRect.position,floorRect.end))
- print("direction tiles:", directionTile1, directionTile2)
- return true
- elif direction == directionsCardinal["e"] or direction == directionsCardinal["w"]:
- var directionTile1 = get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 3 + directionsCardinal["n"])
- var directionTile2 = get_node("/root/Main/dungeonLevel").get_cellv(pos + direction * 3 + directionsCardinal["s"])
- if directionTile1 != 0:
- return false
- elif directionTile2 != 0:
- return false
- else:
- print("can carve? intersects",intersects(pos + direction * 3,pos + direction * 3, floorRect.position,floorRect.end))
- print("direction tiles:", directionTile1, directionTile2)
- return true
- else:
- return false
- func _carve(pos, tileType = "floor"):
- get_node("/root/Main/dungeonLevel").set_cellv(pos,tileIndex(tileType))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement