update
This commit is contained in:
@@ -9,6 +9,7 @@ signal grid_updated
|
||||
@export var rows: int = 10: set = set_rows
|
||||
@export var floors: int = 3: set = set_floors
|
||||
@export var auto_generate: bool = false: set = set_auto_generate
|
||||
@export var auto_randomize: bool = false # If true, randomizes grid on start
|
||||
|
||||
@export var normal_items: Array[int] = [0]
|
||||
@export var non_walkable_items: Array[int] = [4]
|
||||
@@ -46,8 +47,11 @@ class NeighborInfo:
|
||||
|
||||
func _ready():
|
||||
mesh_library_changed.connect(_on_mesh_library_changed)
|
||||
if not Engine.is_editor_hint() and auto_generate:
|
||||
generate_grid()
|
||||
if not Engine.is_editor_hint():
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
if auto_randomize:
|
||||
randomize_grid()
|
||||
validate_item_indices()
|
||||
|
||||
# Core grid management functions
|
||||
@@ -173,8 +177,10 @@ func fill_floor(item_index: int, floor_index: int):
|
||||
# Randomization functions
|
||||
func randomize_grid(floor_index: int = -1):
|
||||
if floor_index == -1:
|
||||
for y in range(floors):
|
||||
randomize_floor(y)
|
||||
# Default auto-randomize behavior:
|
||||
# Preserve Floor 0 (Ground)
|
||||
# Shuffle Floor 1 (Objects/Items)
|
||||
shuffle_floor_objects(1)
|
||||
else:
|
||||
randomize_floor(floor_index)
|
||||
|
||||
@@ -259,6 +265,44 @@ func randomize_floor_custom(randomize_states: Array, floor_index: int):
|
||||
set_cell_item(cell_pos, normal_items[0], current_orientation)
|
||||
|
||||
|
||||
func shuffle_floor_objects(floor_idx: int):
|
||||
if not mesh_library:
|
||||
print("Error: No MeshLibrary assigned to GridMap")
|
||||
return
|
||||
|
||||
# 1. Collect all existing items and their positions on this floor
|
||||
var positions: Array[Vector3i] = []
|
||||
var items: Array[int] = []
|
||||
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var cell_pos = Vector3i(x, floor_idx, z)
|
||||
var item = get_cell_item(cell_pos)
|
||||
|
||||
if item != -1:
|
||||
positions.append(cell_pos)
|
||||
items.append(item)
|
||||
|
||||
if positions.is_empty():
|
||||
return
|
||||
|
||||
# 2. Shuffle the items list
|
||||
var rng = RandomNumberGenerator.new()
|
||||
rng.randomize()
|
||||
items.shuffle()
|
||||
|
||||
# 3. Reassign items to the existing positions
|
||||
# This preserves the location of "something exists here" (layout)
|
||||
# but randomizes "what exists here" (type)
|
||||
for i in range(positions.size()):
|
||||
var pos = positions[i]
|
||||
var new_item = items[i]
|
||||
var orientation = get_cell_item_orientation(pos) # Keep orientation? Or reset? Keeping seems safer.
|
||||
set_cell_item(pos, new_item, orientation)
|
||||
|
||||
print("Shuffled %d items on Floor %d" % [items.size(), floor_idx])
|
||||
|
||||
|
||||
#func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInfo]:
|
||||
#var neighbors: Array[NeighborInfo] = []
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user