extends Node
enum Tiles {EMPTY, SOLID}
class Room:
var position:Vector2
var dimensions:Vector2
var centerpoint:Vector2
var dugRooms =
var rng = RandomNumberGenerator.new()
func _ready():
rng.randomize()
func generate(map:TileMap, map_Width, map_Height, minRoomSize, maxRoomSize):
var potentialRooms:int = (map_Width / maxRoomSize) * (map_Height / maxRoomSize)#figuring out max number of rooms by checking how many max size rooms can fit
var rooms:Dictionary
var room:Room
var position:Vector2
var start:Vector2
var end:Vector2
var modifier:int
var key:String
# Fill map with solid tiles, adding a border of solid tiles around the map
for r in range(-1, map_Height + 1):
for c in range (-1, map_Width + 1):
map.set_cell(0, Vector2i(c, r), 0, Vector2i(Tiles.SOLID, 0))
#making the rooms
for r in potentialRooms:
position = Vector2(rng.randi_range(1, map_Width - maxRoomSize), rng.randi_range(1, map_Height - maxRoomSize))
key = str(position.x) + " " + str(position.y)
if (!rooms.has(key)):
room = Room.new()
room.position = position
room.dimensions = Vector2(rng.randi_range(minRoomSize, maxRoomSize), rng.randi_range(minRoomSize, maxRoomSize))
room.centerpoint = room.position + Vector2(room.dimensions.x / 2, room.dimensions.y / 2)
print("room center point:"+str(room.centerpoint))
print("room left edge:"+str(room.centerpoint-Vector2(room.dimensions.x / 2 , 0)))
var leftSide = Vector2(room.centerpoint-Vector2(room.dimensions.x / 2, 0))
rooms[key] = room
digRoom(map, room, map_Width, map_Height, leftSide)
func digRoom(map, room, map_Width, map_Height, leftSide):
for x in range(room.position.x, room.position.x + room.dimensions.x - 1):
for y in range(room.position.y, room.position.y + room.dimensions.y - 1):
digCell(map, Vector2(x, y), map_Width, map_Height, leftSide)
#undigCell(map, Vector2(x, y), map_Width, map_Height, leftSide)
func digCell(map, pos, map_Width, map_Height, leftSide):
if ((pos.x < map_Width) and (pos.y < map_Height)):
map.set_cell(0, Vector2i(pos.x, pos.y), 0, Vector2i(Tiles.EMPTY, 0))
print("left side x value: "+str(leftSide.x))
print("left side y value: "+str(leftSide.y))
map.set_cell(0, Vector2i(leftSide.x, leftSide.y), 0, Vector2i(Tiles.SOLID, 0))
func undigCell(map, pos, map_Width, map_Height, leftSide):
if ((pos.x < map_Width) and (pos.y < map_Height)):
map.set_cell(0, Vector2i(leftSide.x, leftSide.y), 0, Vector2i(Tiles.SOLID, 0))