25 lines
1.0 KiB
GDScript
25 lines
1.0 KiB
GDScript
class_name RoomGraph
|
|
extends RefCounted
|
|
|
|
# A room graph is intentionally limited to the generator's tree topology. Doors,
|
|
# loops, and route semantics are added by later world-generation slices.
|
|
var edges: Array[Dictionary] = []
|
|
|
|
func connect_each_room_to_nearest_prior(rooms: Array[RoomState]) -> void:
|
|
edges.clear()
|
|
for room_index in range(1, rooms.size()):
|
|
var nearest_index := 0
|
|
var nearest_distance := _manhattan_distance(_room_center(rooms[room_index].bounds), _room_center(rooms[0].bounds))
|
|
for prior_index in range(1, room_index):
|
|
var distance := _manhattan_distance(_room_center(rooms[room_index].bounds), _room_center(rooms[prior_index].bounds))
|
|
if distance < nearest_distance:
|
|
nearest_index = prior_index
|
|
nearest_distance = distance
|
|
edges.append({"from": nearest_index, "to": room_index})
|
|
|
|
func _room_center(bounds: Rect2i) -> Vector2i:
|
|
return bounds.position + Vector2i(bounds.size.x / 2, bounds.size.y / 2)
|
|
|
|
func _manhattan_distance(from: Vector2i, to: Vector2i) -> int:
|
|
return abs(from.x - to.x) + abs(from.y - to.y)
|