126 lines
3.9 KiB
GDScript
126 lines
3.9 KiB
GDScript
extends Node3D
|
|
|
|
@export_group("Prefabs")
|
|
@export var floor_scene: PackedScene
|
|
@export var wall_scene: PackedScene
|
|
|
|
@export_group("Grid Settings")
|
|
@export var tile_size: float = 2.0
|
|
@export var num_rooms: int = 15
|
|
|
|
# Our digital chessboard. Key: Vector2i (x, z), Value: String ("floor", "wall_N", etc.)
|
|
var grid: Dictionary = {}
|
|
|
|
func _ready():
|
|
generate_dungeon()
|
|
|
|
func generate_dungeon():
|
|
var room_bounds: Array[AABB] = []
|
|
|
|
# ==========================================
|
|
# 1. GENERATE THE MATH (Same as before)
|
|
# ==========================================
|
|
for i in range(num_rooms):
|
|
# We snap random sizes and positions to our tile_size to ensure perfect grid alignment
|
|
var r_width = snappedf(randf_range(4.0, 10.0), tile_size)
|
|
var r_depth = snappedf(randf_range(4.0, 10.0), tile_size)
|
|
|
|
var pos_x = snappedf(randf_range(-20.0, 20.0), tile_size)
|
|
var pos_z = snappedf(randf_range(-20.0, 20.0), tile_size)
|
|
|
|
var new_room = AABB(Vector3(pos_x, 0, pos_z), Vector3(r_width, 0, r_depth))
|
|
|
|
var overlaps = false
|
|
for existing in room_bounds:
|
|
# Grow by tile_size so rooms don't share walls directly
|
|
if existing.grow(tile_size).intersects(new_room):
|
|
overlaps = true
|
|
break
|
|
|
|
if not overlaps:
|
|
room_bounds.append(new_room)
|
|
carve_aabb_into_grid(new_room) # Turn math box into floor tiles
|
|
|
|
# Connect the rooms with corridors
|
|
for i in range(room_bounds.size() - 1):
|
|
connect_rooms(room_bounds[i], room_bounds[i + 1])
|
|
|
|
# ==========================================
|
|
# 2. BUILD THE WALLS
|
|
# ==========================================
|
|
calculate_walls()
|
|
|
|
# ==========================================
|
|
# 3. SPAWN THE PREFABS
|
|
# ==========================================
|
|
spawn_prefabs()
|
|
|
|
|
|
func carve_aabb_into_grid(aabb: AABB):
|
|
# Convert 3D world coordinates into 2D grid coordinates
|
|
var start_x = int(aabb.position.x / tile_size)
|
|
var start_z = int(aabb.position.z / tile_size)
|
|
var end_x = int(aabb.end.x / tile_size)
|
|
var end_z = int(aabb.end.z / tile_size)
|
|
|
|
# Fill every grid cell inside this box with a floor
|
|
for x in range(start_x, end_x):
|
|
for z in range(start_z, end_z):
|
|
grid[Vector2i(x, z)] = "floor"
|
|
|
|
func connect_rooms(room_a: AABB, room_b: AABB):
|
|
var center_a = room_a.get_center()
|
|
var center_b = room_b.get_center()
|
|
|
|
# Create mathematical L-shape corridors
|
|
var x_corridor = AABB(
|
|
Vector3(min(center_a.x, center_b.x), 0, center_a.z - (tile_size/2.0)),
|
|
Vector3(abs(center_b.x - center_a.x) + tile_size, 0, tile_size)
|
|
)
|
|
var z_corridor = AABB(
|
|
Vector3(center_b.x - (tile_size/2.0), 0, min(center_a.z, center_b.z)),
|
|
Vector3(tile_size, 0, abs(center_b.z - center_a.z) + tile_size)
|
|
)
|
|
|
|
carve_aabb_into_grid(x_corridor)
|
|
carve_aabb_into_grid(z_corridor)
|
|
|
|
func calculate_walls():
|
|
# The 4 cardinal directions to check around every floor tile
|
|
# Dictionary maps Vector2i direction to a rotation angle in radians
|
|
var directions = {
|
|
Vector2i(0, -1): 0.0, # North
|
|
Vector2i(0, 1): PI, # South
|
|
Vector2i(-1, 0): PI / 2.0, # West
|
|
Vector2i(1, 0): -PI / 2.0 # East
|
|
}
|
|
|
|
# We duplicate the keys because we can't add to the grid while looping over it
|
|
var floor_cells = grid.keys()
|
|
|
|
for cell in floor_cells:
|
|
if grid[cell] == "floor":
|
|
for dir in directions:
|
|
var neighbor = cell + dir
|
|
# If the space next to a floor is totally empty, it becomes a wall!
|
|
if not grid.has(neighbor):
|
|
# Store the wall and the angle it needs to face to look at the floor
|
|
grid[neighbor] = {"type": "wall", "rotation": directions[dir]}
|
|
|
|
func spawn_prefabs():
|
|
for cell in grid:
|
|
var world_pos = Vector3(cell.x * tile_size, 0, cell.y * tile_size)
|
|
var cell_data = grid[cell]
|
|
|
|
if cell_data is String and cell_data == "floor":
|
|
if floor_scene:
|
|
var f = floor_scene.instantiate()
|
|
f.position = world_pos
|
|
add_child(f)
|
|
|
|
elif cell_data is Dictionary and cell_data["type"] == "wall":
|
|
if wall_scene:
|
|
var w = wall_scene.instantiate()
|
|
w.position = world_pos
|
|
w.rotation.y = cell_data["rotation"]
|
|
add_child(w)
|