226 lines
6.9 KiB
GDScript3
226 lines
6.9 KiB
GDScript3
|
|
extends Node3D
|
||
|
|
|
||
|
|
@export_group("Map Pieces")
|
||
|
|
@export var start_room_scene: PackedScene
|
||
|
|
@export var cap_scene: PackedScene
|
||
|
|
@export var room_scenes: Array[PackedScene]
|
||
|
|
@export var corridor_scenes: Array[PackedScene]
|
||
|
|
|
||
|
|
@export_group("Generation Rules")
|
||
|
|
@export_range(0.0, 1.0) var corridor_chance: float = 0.5
|
||
|
|
@export var room_threshold: int = 15
|
||
|
|
@export_range(0.0, 1.0) var stop_chance_step: float = 0.1
|
||
|
|
|
||
|
|
var generated_rooms: Array[Node3D] = []
|
||
|
|
var open_connections: Array[Dictionary] = []
|
||
|
|
var total_pieces: int = 0
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
generate_map()
|
||
|
|
|
||
|
|
func draw_connection_laser(pos1: Vector3, pos2: Vector3):
|
||
|
|
var mesh_instance = MeshInstance3D.new()
|
||
|
|
var cylinder = CylinderMesh.new()
|
||
|
|
|
||
|
|
# Make a thin, long laser beam
|
||
|
|
cylinder.top_radius = 0.05
|
||
|
|
cylinder.bottom_radius = 0.05
|
||
|
|
cylinder.height = pos1.distance_to(pos2)
|
||
|
|
|
||
|
|
var material = StandardMaterial3D.new()
|
||
|
|
material.albedo_color = Color.GREEN
|
||
|
|
material.emission_enabled = true
|
||
|
|
material.emission = Color.GREEN
|
||
|
|
cylinder.surface_set_material(0, material)
|
||
|
|
|
||
|
|
mesh_instance.mesh = cylinder
|
||
|
|
get_tree().current_scene.add_child(mesh_instance)
|
||
|
|
|
||
|
|
# Position it exactly between the two markers and make it look at the target
|
||
|
|
mesh_instance.global_position = pos1.lerp(pos2, 0.5)
|
||
|
|
mesh_instance.look_at(pos2, Vector3.UP)
|
||
|
|
|
||
|
|
# Cylinder meshes stand up (Y) by default, we need it to point forward (Z)
|
||
|
|
mesh_instance.rotate_object_local(Vector3.RIGHT, PI/2)
|
||
|
|
|
||
|
|
func generate_map():
|
||
|
|
if not start_room_scene or not cap_scene:
|
||
|
|
push_error("MapGenerator needs both a Start Room Scene and a Cap Scene!")
|
||
|
|
return
|
||
|
|
|
||
|
|
var start_room = start_room_scene.instantiate()
|
||
|
|
add_child(start_room)
|
||
|
|
generated_rooms.append(start_room)
|
||
|
|
add_open_connections(start_room, "room")
|
||
|
|
total_pieces += 1
|
||
|
|
|
||
|
|
var attempts = 0
|
||
|
|
|
||
|
|
while open_connections.size() > 0 and attempts < 2000:
|
||
|
|
attempts += 1
|
||
|
|
|
||
|
|
var target_dict = open_connections.pick_random()
|
||
|
|
var target_marker = target_dict["marker"]
|
||
|
|
var target_type = target_dict["type"]
|
||
|
|
|
||
|
|
if is_marker_blocked(target_marker):
|
||
|
|
open_connections.erase(target_dict)
|
||
|
|
continue
|
||
|
|
|
||
|
|
# 1. Decide what category we are TRYING to spawn
|
||
|
|
var spawn_category = "room"
|
||
|
|
var cap_probability = 0.0
|
||
|
|
if total_pieces > room_threshold:
|
||
|
|
cap_probability = (total_pieces - room_threshold) * stop_chance_step
|
||
|
|
|
||
|
|
if randf() < cap_probability:
|
||
|
|
spawn_category = "cap"
|
||
|
|
elif target_type == "room" and randf() < corridor_chance and corridor_scenes.size() > 0:
|
||
|
|
spawn_category = "corridor"
|
||
|
|
|
||
|
|
# 2. Gather all scenes for that category and SHUFFLE them
|
||
|
|
var scenes_to_try = []
|
||
|
|
if spawn_category == "cap":
|
||
|
|
scenes_to_try = [cap_scene]
|
||
|
|
elif spawn_category == "corridor":
|
||
|
|
scenes_to_try = corridor_scenes.duplicate()
|
||
|
|
scenes_to_try.shuffle() # Randomize the order we try the corridors
|
||
|
|
else:
|
||
|
|
scenes_to_try = room_scenes.duplicate()
|
||
|
|
scenes_to_try.shuffle() # Randomize the order we try the rooms
|
||
|
|
|
||
|
|
var placed_successfully = false
|
||
|
|
|
||
|
|
# 3. Try EVERY scene until one fits
|
||
|
|
for scene in scenes_to_try:
|
||
|
|
if placed_successfully:
|
||
|
|
break
|
||
|
|
|
||
|
|
var new_room = scene.instantiate()
|
||
|
|
add_child(new_room)
|
||
|
|
|
||
|
|
var new_room_markers = new_room.connection_markers.duplicate()
|
||
|
|
if new_room_markers.is_empty():
|
||
|
|
new_room.free()
|
||
|
|
continue
|
||
|
|
|
||
|
|
new_room_markers.shuffle() # Try the new room's doors in random order too!
|
||
|
|
|
||
|
|
# 4. Try EVERY door on the chosen scene
|
||
|
|
for incoming_marker in new_room_markers:
|
||
|
|
align_room(new_room, incoming_marker, target_marker)
|
||
|
|
|
||
|
|
var overlaps = false
|
||
|
|
for existing_room in generated_rooms:
|
||
|
|
if do_rooms_overlap(new_room, existing_room):
|
||
|
|
overlaps = true
|
||
|
|
break
|
||
|
|
|
||
|
|
if not overlaps:
|
||
|
|
# IT FITS! Lock it in.
|
||
|
|
generated_rooms.append(new_room)
|
||
|
|
|
||
|
|
# DRAW THE LASER!
|
||
|
|
draw_connection_laser(target_marker.global_position, new_room.global_position)
|
||
|
|
total_pieces += 1
|
||
|
|
|
||
|
|
open_connections.erase(target_dict)
|
||
|
|
new_room_markers.erase(incoming_marker)
|
||
|
|
|
||
|
|
for marker in new_room_markers:
|
||
|
|
open_connections.append({"marker": marker, "type": spawn_category})
|
||
|
|
|
||
|
|
placed_successfully = true
|
||
|
|
break # Break out of the door loop
|
||
|
|
|
||
|
|
if not placed_successfully:
|
||
|
|
new_room.free() # None of the doors on this scene fit, destroy it and try the next scene
|
||
|
|
|
||
|
|
# 5. Failsafe: If literally NO room and NO door fits in this spot, CAP IT instantly!
|
||
|
|
if not placed_successfully and spawn_category != "cap":
|
||
|
|
force_cap_on_marker(target_dict)
|
||
|
|
|
||
|
|
seal_remaining_connections()
|
||
|
|
print("Map Generation Complete! Total pieces: ", total_pieces)
|
||
|
|
|
||
|
|
|
||
|
|
func force_cap_on_marker(target_dict: Dictionary):
|
||
|
|
var target_marker = target_dict["marker"]
|
||
|
|
var cap = cap_scene.instantiate()
|
||
|
|
add_child(cap)
|
||
|
|
var incoming_marker = cap.connection_markers[0]
|
||
|
|
align_room(cap, incoming_marker, target_marker)
|
||
|
|
generated_rooms.append(cap)
|
||
|
|
open_connections.erase(target_dict)
|
||
|
|
|
||
|
|
|
||
|
|
func seal_remaining_connections():
|
||
|
|
for i in range(open_connections.size() - 1, -1, -1):
|
||
|
|
var target_dict = open_connections[i]
|
||
|
|
var target_marker = target_dict["marker"]
|
||
|
|
|
||
|
|
if is_marker_blocked(target_marker):
|
||
|
|
open_connections.remove_at(i)
|
||
|
|
continue
|
||
|
|
|
||
|
|
force_cap_on_marker(target_dict)
|
||
|
|
|
||
|
|
open_connections.clear()
|
||
|
|
|
||
|
|
|
||
|
|
func add_open_connections(room: Node3D, type: String):
|
||
|
|
if room.connection_markers != null:
|
||
|
|
for marker in room.connection_markers:
|
||
|
|
open_connections.append({"marker": marker, "type": type})
|
||
|
|
|
||
|
|
|
||
|
|
func is_marker_blocked(marker: Marker3D) -> bool:
|
||
|
|
for room in generated_rooms:
|
||
|
|
var aabb = get_room_global_aabb(room)
|
||
|
|
aabb.position += Vector3(0.1, 0.1, 0.1)
|
||
|
|
aabb.size -= Vector3(0.2, 0.2, 0.2)
|
||
|
|
|
||
|
|
if aabb.has_point(marker.global_position):
|
||
|
|
return true
|
||
|
|
return false
|
||
|
|
|
||
|
|
|
||
|
|
func align_room(new_room: Node3D, incoming_marker: Marker3D, target_marker: Marker3D):
|
||
|
|
var desired_incoming_transform = target_marker.global_transform.rotated_local(Vector3.UP, PI)
|
||
|
|
var room_to_marker = new_room.global_transform.affine_inverse() * incoming_marker.global_transform
|
||
|
|
new_room.global_transform = desired_incoming_transform * room_to_marker.affine_inverse()
|
||
|
|
|
||
|
|
|
||
|
|
func do_rooms_overlap(room_a: Node3D, room_b: Node3D) -> bool:
|
||
|
|
var aabb_a = get_room_global_aabb(room_a)
|
||
|
|
var aabb_b = get_room_global_aabb(room_b)
|
||
|
|
|
||
|
|
if aabb_a.size == Vector3.ZERO or aabb_b.size == Vector3.ZERO:
|
||
|
|
return false
|
||
|
|
|
||
|
|
# Increased the margin slightly to ensure corridors pass smoothly
|
||
|
|
var shrink_margin = Vector3(0.2, 0.2, 0.2)
|
||
|
|
aabb_a.position += shrink_margin / 2.0
|
||
|
|
aabb_a.size -= shrink_margin
|
||
|
|
|
||
|
|
return aabb_a.intersects(aabb_b)
|
||
|
|
|
||
|
|
|
||
|
|
func get_room_global_aabb(room: Node3D) -> AABB:
|
||
|
|
var combined_aabb := AABB()
|
||
|
|
var is_first_shape = true
|
||
|
|
|
||
|
|
for child in room.volume.get_children():
|
||
|
|
if child is CollisionShape3D and child.shape is BoxShape3D:
|
||
|
|
var shape = child.shape as BoxShape3D
|
||
|
|
var local_aabb = AABB(-shape.size / 2.0, shape.size)
|
||
|
|
var global_aabb = child.global_transform * local_aabb
|
||
|
|
|
||
|
|
if is_first_shape:
|
||
|
|
combined_aabb = global_aabb
|
||
|
|
is_first_shape = false
|
||
|
|
else:
|
||
|
|
combined_aabb = combined_aabb.merge(global_aabb)
|
||
|
|
|
||
|
|
return combined_aabb
|