update
This commit is contained in:
@@ -7,6 +7,7 @@ signal grid_updated
|
||||
|
||||
@export var columns: int = 10 : set = set_columns
|
||||
@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 normal_items: Array[int] = [0]
|
||||
@@ -16,10 +17,10 @@ signal grid_updated
|
||||
@export var end_item: int = 3
|
||||
|
||||
var current_mesh_library: MeshLibrary
|
||||
var grid_data: Array = []
|
||||
var grid_data: Array = [] # 3D array [floor][row][column]
|
||||
|
||||
# A* Pathfinding variables
|
||||
var astar = AStar2D.new()
|
||||
# A* Pathfinding variables (per floor)
|
||||
var astar_by_floor = {} # Dictionary of AStar2D instances per floor
|
||||
var path = []
|
||||
|
||||
# Item states
|
||||
@@ -28,7 +29,6 @@ enum ItemState {NORMAL, HOVER, START, END, NON_WALKABLE}
|
||||
# Add this to the class variables
|
||||
var diagonal_movement: bool = false
|
||||
|
||||
|
||||
func _ready():
|
||||
mesh_library_changed.connect(_on_mesh_library_changed)
|
||||
if not Engine.is_editor_hint() and auto_generate:
|
||||
@@ -37,6 +37,13 @@ func _ready():
|
||||
# Validate item indices
|
||||
validate_item_indices()
|
||||
|
||||
func set_floors(value: int):
|
||||
floors = value
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
else:
|
||||
update_grid_data()
|
||||
|
||||
func validate_item_indices():
|
||||
if not mesh_library:
|
||||
print("Warning: No MeshLibrary assigned to GridMap")
|
||||
@@ -75,9 +82,23 @@ func set_auto_generate(value: bool):
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
|
||||
# Override the existing functions to update A* data
|
||||
func generate_grid():
|
||||
clear()
|
||||
# Modified generate_grid to support floors
|
||||
func generate_grid(floor_index: int = -1):
|
||||
if floor_index == -1:
|
||||
# Generate all floors
|
||||
clear()
|
||||
for y in range(floors):
|
||||
generate_floor(y)
|
||||
else:
|
||||
# Generate specific floor
|
||||
clear_floor(floor_index)
|
||||
generate_floor(floor_index)
|
||||
|
||||
update_grid_data()
|
||||
initialize_astar()
|
||||
update_astar_costs()
|
||||
|
||||
func generate_floor(floor_index: int):
|
||||
if not mesh_library:
|
||||
print("Error: No MeshLibrary assigned to GridMap")
|
||||
return
|
||||
@@ -91,17 +112,36 @@ func generate_grid():
|
||||
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
set_cell_item(Vector3i(x, 0, z), normal_items[0])
|
||||
set_cell_item(Vector3i(x, floor_index, z), normal_items[0])
|
||||
|
||||
# Clear specific floor
|
||||
func clear_floor(floor_index: int):
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
set_cell_item(Vector3i(x, floor_index, z), INVALID_CELL_ITEM)
|
||||
update_grid_data()
|
||||
|
||||
# Modified clear_grid to support floors
|
||||
func clear_grid(floor_index: int = -1):
|
||||
if floor_index == -1:
|
||||
clear()
|
||||
else:
|
||||
clear_floor(floor_index)
|
||||
update_grid_data()
|
||||
|
||||
# Modified randomize_grid to support floors
|
||||
func randomize_grid(floor_index: int = -1):
|
||||
if floor_index == -1:
|
||||
for y in range(floors):
|
||||
randomize_floor(y)
|
||||
else:
|
||||
randomize_floor(floor_index)
|
||||
|
||||
update_grid_data()
|
||||
initialize_astar()
|
||||
update_astar_costs()
|
||||
|
||||
func clear_grid():
|
||||
clear()
|
||||
update_grid_data()
|
||||
|
||||
func randomize_grid():
|
||||
func randomize_floor(floor_index: int):
|
||||
if not mesh_library:
|
||||
print("Error: No MeshLibrary assigned to GridMap")
|
||||
return
|
||||
@@ -119,16 +159,31 @@ func randomize_grid():
|
||||
item_index = normal_items[rng.randi() % normal_items.size()]
|
||||
else:
|
||||
item_index = non_walkable_items[rng.randi() % non_walkable_items.size()]
|
||||
set_cell_from_data(x, z, item_index)
|
||||
|
||||
set_cell_item(Vector3i(x, floor_index, z), item_index)
|
||||
|
||||
# Modified randomize_grid_custom to properly handle mesh library items
|
||||
func randomize_grid_custom(randomize_states: Array, floor_index: int = -1):
|
||||
if not mesh_library:
|
||||
print("Error: No MeshLibrary assigned to GridMap")
|
||||
return
|
||||
|
||||
if floor_index == -1:
|
||||
for y in range(floors):
|
||||
randomize_floor_custom(randomize_states, y)
|
||||
else:
|
||||
# Randomize specific floor
|
||||
if floor_index >= 0 and floor_index < floors:
|
||||
randomize_floor_custom(randomize_states, floor_index)
|
||||
else:
|
||||
print("Invalid floor index")
|
||||
|
||||
update_grid_data()
|
||||
initialize_astar()
|
||||
update_astar_costs()
|
||||
emit_signal("grid_updated")
|
||||
|
||||
func randomize_grid_custom(randomize_states: Array):
|
||||
if not mesh_library:
|
||||
print("Error: No MeshLibrary assigned to GridMap")
|
||||
func randomize_floor_custom(randomize_states: Array, floor_index: int):
|
||||
if randomize_states.is_empty():
|
||||
print("No randomize states provided")
|
||||
return
|
||||
|
||||
var rng = RandomNumberGenerator.new()
|
||||
@@ -136,27 +191,38 @@ func randomize_grid_custom(randomize_states: Array):
|
||||
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var random_value = rng.randf() * 100 # Generate a random number between 0 and 100
|
||||
var cell_pos = Vector3i(x, floor_index, z)
|
||||
var random_value = rng.randf() * 100
|
||||
var accumulated_percentage = 0
|
||||
var selected_state = null
|
||||
|
||||
for state in randomize_states:
|
||||
accumulated_percentage += state.randomize_percentage
|
||||
if random_value <= accumulated_percentage:
|
||||
selected_state = state
|
||||
break
|
||||
if state.include_in_randomize:
|
||||
accumulated_percentage += state.randomize_percentage
|
||||
if random_value <= accumulated_percentage:
|
||||
selected_state = state
|
||||
break
|
||||
|
||||
# Get current cell orientation
|
||||
var current_orientation = get_cell_item_orientation(cell_pos)
|
||||
|
||||
if selected_state:
|
||||
set_cell_from_data(x, z, selected_state.id)
|
||||
set_cell_item(cell_pos, selected_state.id, current_orientation)
|
||||
else:
|
||||
set_cell_from_data(x, z, randomize_states[0].id) # Default to the first state if no match
|
||||
# Find first enabled state or use normal item as fallback
|
||||
var fallback_state = null
|
||||
for state in randomize_states:
|
||||
if state.include_in_randomize:
|
||||
fallback_state = state
|
||||
break
|
||||
|
||||
if fallback_state:
|
||||
set_cell_item(cell_pos, fallback_state.id, current_orientation)
|
||||
else:
|
||||
set_cell_item(cell_pos, normal_items[0], current_orientation)
|
||||
|
||||
update_grid_data()
|
||||
initialize_astar()
|
||||
update_astar_costs()
|
||||
emit_signal("grid_updated")
|
||||
|
||||
func fill_grid(item_index: int):
|
||||
# Fill grid for specific floor
|
||||
func fill_grid(item_index: int, floor_index: int = -1):
|
||||
if not mesh_library:
|
||||
print("No MeshLibrary assigned to GridMap")
|
||||
return
|
||||
@@ -165,11 +231,27 @@ func fill_grid(item_index: int):
|
||||
print("Invalid item index")
|
||||
return
|
||||
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
set_cell_item(Vector3i(x, 0, z), item_index)
|
||||
if floor_index == -1:
|
||||
for y in range(floors):
|
||||
fill_floor(item_index, y)
|
||||
else:
|
||||
# Fill specific floor
|
||||
if floor_index >= 0 and floor_index < floors:
|
||||
fill_floor(item_index, floor_index)
|
||||
else:
|
||||
print("Invalid floor index")
|
||||
|
||||
update_grid_data()
|
||||
initialize_astar()
|
||||
update_astar_costs()
|
||||
|
||||
func fill_floor(item_index: int, floor_index: int):
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var cell_pos = Vector3i(x, floor_index, z)
|
||||
# Preserve the current cell's orientation when filling
|
||||
var current_orientation = get_cell_item_orientation(cell_pos)
|
||||
set_cell_item(cell_pos, item_index, current_orientation)
|
||||
|
||||
func _on_mesh_library_changed():
|
||||
validate_item_indices()
|
||||
@@ -183,92 +265,101 @@ func _set(property, value):
|
||||
return true
|
||||
return false
|
||||
|
||||
# Modified update_grid_data for 3D array
|
||||
func update_grid_data():
|
||||
grid_data.clear()
|
||||
for z in range(rows):
|
||||
var row = []
|
||||
for x in range(columns):
|
||||
row.append(get_cell_item(Vector3i(x, 0, z)))
|
||||
grid_data.append(row)
|
||||
for y in range(floors):
|
||||
var floor_data = []
|
||||
for z in range(rows):
|
||||
var row = []
|
||||
for x in range(columns):
|
||||
row.append(get_cell_item(Vector3i(x, y, z)))
|
||||
floor_data.append(row)
|
||||
grid_data.append(floor_data)
|
||||
emit_signal("grid_updated")
|
||||
|
||||
func set_cell_from_data(x: int, z: int, item_index: int):
|
||||
if x >= 0 and x < columns and z >= 0 and z < rows:
|
||||
if not mesh_library:
|
||||
print("Error: No MeshLibrary assigned to GridMap")
|
||||
return
|
||||
|
||||
var item_list = mesh_library.get_item_list()
|
||||
var max_index = item_list.size() - 1
|
||||
var valid_index = clamp(item_index, 0, max_index)
|
||||
|
||||
set_cell_item(Vector3i(x, 0, z), valid_index)
|
||||
grid_data[z][x] = valid_index
|
||||
var point_id = z * columns + x
|
||||
var cost = get_cell_cost(x, z)
|
||||
if cost == INF:
|
||||
astar.set_point_disabled(point_id, true)
|
||||
else:
|
||||
astar.set_point_disabled(point_id, false)
|
||||
astar.set_point_weight_scale(point_id, cost)
|
||||
|
||||
# New A* Pathfinding functions
|
||||
# Modify the initialize_astar function
|
||||
func initialize_astar():
|
||||
astar.clear()
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var point_id = z * columns + x
|
||||
astar.add_point(point_id, Vector2(x, z))
|
||||
|
||||
# Connect to neighboring points
|
||||
if x > 0:
|
||||
astar.connect_points(point_id, point_id - 1)
|
||||
if z > 0:
|
||||
astar.connect_points(point_id, point_id - columns)
|
||||
|
||||
# Add diagonal connections if diagonal movement is enabled
|
||||
if diagonal_movement:
|
||||
if x > 0 and z > 0:
|
||||
astar.connect_points(point_id, point_id - columns - 1) # Top-left
|
||||
if x < columns - 1 and z > 0:
|
||||
astar.connect_points(point_id, point_id - columns + 1) # Top-right
|
||||
# New function to swap items on a specific floor
|
||||
func swap_items(old_item: int, new_item: int, floor_index: int = -1):
|
||||
if floor_index == -1:
|
||||
for y in range(floors):
|
||||
swap_items_on_floor(old_item, new_item, y)
|
||||
else:
|
||||
swap_items_on_floor(old_item, new_item, floor_index)
|
||||
|
||||
update_grid_data()
|
||||
initialize_astar()
|
||||
update_astar_costs()
|
||||
|
||||
func swap_items_on_floor(old_item: int, new_item: int, floor_index: int):
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var cell_pos = Vector3i(x, floor_index, z)
|
||||
if get_cell_item(cell_pos) == old_item:
|
||||
set_cell_item(cell_pos, new_item)
|
||||
|
||||
# Modified A* pathfinding for multiple floors
|
||||
func initialize_astar():
|
||||
astar_by_floor.clear()
|
||||
for y in range(floors):
|
||||
var astar = AStar2D.new()
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var point_id = z * columns + x
|
||||
astar.add_point(point_id, Vector2(x, z))
|
||||
|
||||
if x > 0:
|
||||
astar.connect_points(point_id, point_id - 1)
|
||||
if z > 0:
|
||||
astar.connect_points(point_id, point_id - columns)
|
||||
|
||||
if diagonal_movement:
|
||||
if x > 0 and z > 0:
|
||||
astar.connect_points(point_id, point_id - columns - 1) # Top-left
|
||||
if x < columns - 1 and z > 0:
|
||||
astar.connect_points(point_id, point_id - columns + 1) # Top-right
|
||||
|
||||
astar_by_floor[y] = astar
|
||||
update_astar_costs()
|
||||
|
||||
# Add this function to toggle diagonal movement
|
||||
func set_diagonal_movement(enable: bool):
|
||||
diagonal_movement = enable
|
||||
initialize_astar() # Reinitialize the A* graph with new connections
|
||||
|
||||
func set_point_solid(x: int, z: int, is_solid: bool):
|
||||
var point_id = z * columns + x
|
||||
astar.set_point_disabled(point_id, is_solid)
|
||||
func set_point_solid(x: int, z: int, floor_index: int, is_solid: bool):
|
||||
var astar = astar_by_floor.get(floor_index)
|
||||
if astar:
|
||||
var point_id = z * columns + x
|
||||
astar.set_point_disabled(point_id, is_solid)
|
||||
|
||||
func find_path(start: Vector2, end: Vector2) -> Array:
|
||||
# Modified path finding for specific floor
|
||||
func find_path(start: Vector2, end: Vector2, floor_index: int = 0) -> Array:
|
||||
var astar = astar_by_floor.get(floor_index)
|
||||
if not astar:
|
||||
return []
|
||||
|
||||
var start_point = start.y * columns + start.x
|
||||
var end_point = end.y * columns + end.x
|
||||
path = astar.get_point_path(start_point, end_point)
|
||||
|
||||
# Visualize the path
|
||||
clear_path_visualization()
|
||||
set_cell_item(Vector3i(start.x, 0, start.y), start_item)
|
||||
set_cell_item(Vector3i(end.x, 0, end.y), end_item)
|
||||
clear_path_visualization(floor_index)
|
||||
set_cell_item(Vector3i(start.x, floor_index, start.y), start_item)
|
||||
set_cell_item(Vector3i(end.x, floor_index, end.y), end_item)
|
||||
for point in path:
|
||||
if point != start and point != end:
|
||||
set_cell_item(Vector3i(point.x, 0, point.y), hover_item)
|
||||
set_cell_item(Vector3i(point.x, floor_index, point.y), hover_item)
|
||||
|
||||
return path
|
||||
|
||||
func clear_path_visualization():
|
||||
# Modified path visualization clearing for specific floor
|
||||
func clear_path_visualization(floor_index: int = 0):
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var cell_item = get_cell_item(Vector3i(x, 0, z))
|
||||
var cell_item = get_cell_item(Vector3i(x, floor_index, z))
|
||||
if cell_item == hover_item or cell_item == start_item or cell_item == end_item:
|
||||
set_cell_item(Vector3i(x, 0, z), normal_items[0])
|
||||
set_cell_item(Vector3i(x, floor_index, z), normal_items[0])
|
||||
|
||||
func get_cell_cost(x: int, z: int) -> float:
|
||||
var cell_item = get_cell_item(Vector3i(x, 0, z))
|
||||
func get_cell_cost(x: int, z: int, floor_index: int = 0) -> float:
|
||||
var cell_item = get_cell_item(Vector3i(x, floor_index, z))
|
||||
if cell_item in non_walkable_items:
|
||||
return INF
|
||||
elif cell_item == hover_item:
|
||||
@@ -277,16 +368,20 @@ func get_cell_cost(x: int, z: int) -> float:
|
||||
return 0.0
|
||||
return 1.0
|
||||
|
||||
# Modified update_astar_costs for multiple floors
|
||||
func update_astar_costs():
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var point_id = z * columns + x
|
||||
var cost = get_cell_cost(x, z)
|
||||
if cost == INF:
|
||||
astar.set_point_disabled(point_id, true)
|
||||
else:
|
||||
astar.set_point_disabled(point_id, false)
|
||||
astar.set_point_weight_scale(point_id, cost)
|
||||
for floor_index in range(floors):
|
||||
var astar = astar_by_floor.get(floor_index)
|
||||
if astar:
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var point_id = z * columns + x
|
||||
var cost = get_cell_cost(x, z, floor_index)
|
||||
if cost == INF:
|
||||
astar.set_point_disabled(point_id, true)
|
||||
else:
|
||||
astar.set_point_disabled(point_id, false)
|
||||
astar.set_point_weight_scale(point_id, cost)
|
||||
|
||||
func get_cell_rotation(position: Vector3i) -> int:
|
||||
return get_cell_item_orientation(position)
|
||||
|
||||
Reference in New Issue
Block a user