feat: generate connected dungeon rooms
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
extends GutTest
|
||||
|
||||
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
|
||||
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
|
||||
|
||||
const CARDINAL_DIRECTIONS := [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]
|
||||
|
||||
func test_one_hundred_seeds_keep_every_room_center_entrance_and_exit_reachable() -> void:
|
||||
var catalog := ContentCatalogType.new()
|
||||
|
||||
for seed_index in range(100):
|
||||
var floor := FloorGeneratorType.generate("B2-connectivity-%03d" % seed_index, 1, catalog)
|
||||
var reachable := _reachable_cells(floor.map, floor.entrance)
|
||||
var required_cells: Array[Vector2i] = [floor.entrance, floor.exit]
|
||||
for room in floor.rooms:
|
||||
required_cells.append(_room_center(room.bounds))
|
||||
|
||||
for cell in required_cells:
|
||||
assert_true(reachable.has(cell), "seed %d reaches %s" % [seed_index, cell])
|
||||
|
||||
func _reachable_cells(map: GridMapState, start: Vector2i) -> Dictionary[Vector2i, bool]:
|
||||
var reachable: Dictionary[Vector2i, bool] = {}
|
||||
var frontier: Array[Vector2i] = [start]
|
||||
reachable[start] = true
|
||||
while not frontier.is_empty():
|
||||
var current := frontier.pop_front()
|
||||
for direction in CARDINAL_DIRECTIONS:
|
||||
var next_cell: Vector2i = current + direction
|
||||
if map.is_walkable(next_cell) and not reachable.has(next_cell):
|
||||
reachable[next_cell] = true
|
||||
frontier.append(next_cell)
|
||||
return reachable
|
||||
|
||||
func _room_center(bounds: Rect2i) -> Vector2i:
|
||||
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
|
||||
@@ -0,0 +1 @@
|
||||
uid://c6dwu2fu28ggi
|
||||
@@ -0,0 +1,36 @@
|
||||
extends GutTest
|
||||
|
||||
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
|
||||
const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd")
|
||||
|
||||
const MAP_SIZE := Vector2i(80, 50)
|
||||
const ROOM_PADDING := 2
|
||||
|
||||
func test_generator_creates_deterministic_padded_rooms_inside_the_minimal_floor() -> void:
|
||||
var catalog := ContentCatalogType.new()
|
||||
var first := FloorGeneratorType.generate("B2-rooms-contract", 1, catalog)
|
||||
var second := FloorGeneratorType.generate("B2-rooms-contract", 1, catalog)
|
||||
|
||||
assert_eq(first.map.size, MAP_SIZE)
|
||||
assert_gte(first.rooms.size(), 8)
|
||||
assert_eq(first.to_dict(), second.to_dict())
|
||||
|
||||
for room in first.rooms:
|
||||
assert_gte(room.bounds.size.x, 5, "%s minimum room width" % room.id)
|
||||
assert_gte(room.bounds.size.y, 4, "%s minimum room height" % room.id)
|
||||
assert_true(_is_inside_map(room.bounds), "%s is inside the map" % room.id)
|
||||
|
||||
for first_index in range(first.rooms.size()):
|
||||
for second_index in range(first_index + 1, first.rooms.size()):
|
||||
var first_padded := first.rooms[first_index].bounds.grow(ROOM_PADDING)
|
||||
var second_padded := first.rooms[second_index].bounds.grow(ROOM_PADDING)
|
||||
assert_false(
|
||||
first_padded.intersects(second_padded),
|
||||
"%s and %s respect padding" % [first.rooms[first_index].id, first.rooms[second_index].id]
|
||||
)
|
||||
|
||||
func _is_inside_map(bounds: Rect2i) -> bool:
|
||||
return bounds.position.x >= 0 \
|
||||
and bounds.position.y >= 0 \
|
||||
and bounds.end.x <= MAP_SIZE.x \
|
||||
and bounds.end.y <= MAP_SIZE.y
|
||||
@@ -0,0 +1 @@
|
||||
uid://72yf5sfnhej1
|
||||
Reference in New Issue
Block a user