Working map generator

This commit is contained in:
2024-01-21 00:42:57 +01:00
parent cd80ab3357
commit 405b374b9a
17 changed files with 240 additions and 38 deletions

View File

@ -18,9 +18,10 @@ var _rooms_top: Array[PackedScene] = []
var _rooms_bottom: Array[PackedScene] = []
var _rooms_blocking: Array[PackedScene] = []
var _progress_tracker: ProgressTracker = ProgressTracker.new(1)
var _progress_tracker: ProgressTracker
func _generate(map: TileMap) -> void:
self._progress_tracker = ProgressTracker.new(1)
self._sort_rooms(_progress_tracker.next_step("Sorting Rooms"))
@ -30,8 +31,9 @@ func _generate(map: TileMap) -> void:
# Create rooms
self._create_rooms(map, init_room, self.max_room_path_length, _progress_tracker.next_step("Creating Rooms"))
map.update_internals()
func _sort_rooms(step_tracker: ProgressStepTracker, validate: bool = false) -> void:
func _sort_rooms(step_tracker: ProgressStepTracker) -> void:
step_tracker.complete.call_deferred()
step_tracker.set_substeps(len(self.rooms))
@ -67,12 +69,12 @@ func _create_initial_room(map: TileMap, room: PackedScene, step_tracker: Progres
step_tracker.complete.call_deferred()
# Instantiate spawn room
var spawn_room: StandardRoom = room.instance()
var init_room: StandardRoom = room.instantiate()
# Copy spawn room into map
spawn_room.copy_to_map(map, Vector2i(0, 0))
init_room.copy_to_map(map)
return spawn_room
return init_room
# Create rooms by randomly selecting from the available rooms in every direction with openings from the current room
@ -88,6 +90,8 @@ func _create_rooms(map: TileMap, parent_room: StandardRoom, max_path_length: int
# If the maximum path length has been exceeded, stop
if max_path_length <= 0:
for exit in exits:
parent_room.seal_exit(map, parent_room.position, exit)
return
# If there are no more exits, stop
@ -106,30 +110,42 @@ func _create_rooms(map: TileMap, parent_room: StandardRoom, max_path_length: int
var room_edge_pos: Vector2i = exit_pos + Vector2i(parent_room.position)
# If there is already a room at this position, skip it
if map.get_cellv(room_edge_pos) != -1:
continue
# Get the rooms that can be added in this direction
var rooms: Array[PackedScene] = self._get_rooms(exit)
var possible_rooms: Array[PackedScene] = self._get_rooms(parent_room.flip_vector(exit)).duplicate()
# If there are no rooms that can be added in this direction, skip it
if len(rooms) == 0:
if len(possible_rooms) == 0:
continue
# Get the room to add
var room: PackedScene = rooms[randi() % len(rooms)]
var room_instance: StandardRoom = null
while len(possible_rooms) != 0 and room_instance == null:
var i = randi() % len(possible_rooms)
var possible_room = possible_rooms.pop_at(i).instantiate()
var origin_pos: Vector2i = possible_room.get_exit_pos_offset(room_edge_pos, exit)
possible_room.position = origin_pos
if possible_room.is_overlapping(map):
continue
room_instance = possible_room
# Instantiate the room
var room_instance: StandardRoom = room.instance()
room_instance.position = room_edge_pos + (room_instance.size / 2)
if room_instance == null:
parent_room.seal_exit(map, parent_room.position, exit)
continue
room_instance.queue_free.call_deferred()
# Disable direction that the room was added from
# since the parent room will already be there
room_instance.disable_room_exit_opposite(exit)
# Copy the room into the map
room_instance.copy_to_map(map, Vector2i(room_instance.position))
room_instance.copy_to_map(map)
# Create rooms from the exits of the room that was just added
self._create_rooms(map, room_instance, max_path_length - 1, step_tracker.next_step("Creating Rooms"))
step_tracker.substep("Creating Rooms")
self._create_rooms(map, room_instance, max_path_length - 1, step_tracker)
# If there are no more exits, stop
return