last update
This commit is contained in:
@@ -23,20 +23,46 @@ var grid_data: Array = [] # 3D array [floor][row][column]
|
||||
var astar_by_floor = {} # Dictionary of AStar2D instances per floor
|
||||
var path = []
|
||||
|
||||
# Item states
|
||||
enum ItemState {NORMAL, HOVER, START, END, NON_WALKABLE}
|
||||
# Direction and movement systems
|
||||
enum Direction {
|
||||
NORTHWEST, NORTH, NORTHEAST,
|
||||
WEST, CENTER, EAST,
|
||||
SOUTHWEST, SOUTH, SOUTHEAST
|
||||
}
|
||||
|
||||
# Add this to the class variables
|
||||
var diagonal_movement: bool = false
|
||||
|
||||
class NeighborInfo:
|
||||
var position: Vector2i
|
||||
var direction: Direction
|
||||
var is_walkable: bool
|
||||
|
||||
func _init(pos: Vector2i, dir: Direction, walkable: bool):
|
||||
position = pos
|
||||
direction = dir
|
||||
is_walkable = walkable
|
||||
|
||||
func _ready():
|
||||
mesh_library_changed.connect(_on_mesh_library_changed)
|
||||
if not Engine.is_editor_hint() and auto_generate:
|
||||
generate_grid()
|
||||
|
||||
# Validate item indices
|
||||
validate_item_indices()
|
||||
|
||||
# Core grid management functions
|
||||
func set_columns(value: int):
|
||||
columns = value
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
else:
|
||||
update_grid_data()
|
||||
|
||||
func set_rows(value: int):
|
||||
rows = value
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
else:
|
||||
update_grid_data()
|
||||
|
||||
func set_floors(value: int):
|
||||
floors = value
|
||||
if auto_generate:
|
||||
@@ -44,6 +70,12 @@ func set_floors(value: int):
|
||||
else:
|
||||
update_grid_data()
|
||||
|
||||
func set_auto_generate(value: bool):
|
||||
auto_generate = value
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
|
||||
# Item validation
|
||||
func validate_item_indices():
|
||||
if not mesh_library:
|
||||
print("Warning: No MeshLibrary assigned to GridMap")
|
||||
@@ -63,34 +95,13 @@ func validate_item_indices():
|
||||
if non_walkable_items.is_empty():
|
||||
non_walkable_items = [max_index]
|
||||
|
||||
func set_columns(value: int):
|
||||
columns = value
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
else:
|
||||
update_grid_data()
|
||||
|
||||
func set_rows(value: int):
|
||||
rows = value
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
else:
|
||||
update_grid_data()
|
||||
|
||||
func set_auto_generate(value: bool):
|
||||
auto_generate = value
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
|
||||
# Modified generate_grid to support floors
|
||||
# Grid generation and management
|
||||
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)
|
||||
|
||||
@@ -114,14 +125,13 @@ func generate_floor(floor_index: int):
|
||||
for z in range(rows):
|
||||
set_cell_item(Vector3i(x, floor_index, z), normal_items[0])
|
||||
|
||||
# Clear specific floor
|
||||
# Grid operations
|
||||
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)
|
||||
set_cell_item(Vector3i(x, floor_index, z), -1)
|
||||
update_grid_data()
|
||||
|
||||
# Modified clear_grid to support floors
|
||||
func clear_grid(floor_index: int = -1):
|
||||
if floor_index == -1:
|
||||
clear()
|
||||
@@ -129,7 +139,36 @@ func clear_grid(floor_index: int = -1):
|
||||
clear_floor(floor_index)
|
||||
update_grid_data()
|
||||
|
||||
# Modified randomize_grid to support floors
|
||||
func fill_grid(item_index: int, floor_index: int = -1):
|
||||
if not mesh_library:
|
||||
print("No MeshLibrary assigned to GridMap")
|
||||
return
|
||||
|
||||
if item_index < 0 or item_index >= mesh_library.get_item_list().size():
|
||||
print("Invalid item index")
|
||||
return
|
||||
|
||||
if floor_index == -1:
|
||||
for y in range(floors):
|
||||
fill_floor(item_index, y)
|
||||
else:
|
||||
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)
|
||||
var current_orientation = get_cell_item_orientation(cell_pos)
|
||||
set_cell_item(cell_pos, item_index, current_orientation)
|
||||
|
||||
# Randomization functions
|
||||
func randomize_grid(floor_index: int = -1):
|
||||
if floor_index == -1:
|
||||
for y in range(floors):
|
||||
@@ -153,7 +192,7 @@ func randomize_floor(floor_index: int):
|
||||
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var random_value = rng.randi() % 100 # Generate a random number between 0 and 99
|
||||
var random_value = rng.randi() % 100
|
||||
var item_index
|
||||
if random_value < 80:
|
||||
item_index = normal_items[rng.randi() % normal_items.size()]
|
||||
@@ -161,7 +200,6 @@ func randomize_floor(floor_index: int):
|
||||
item_index = non_walkable_items[rng.randi() % non_walkable_items.size()]
|
||||
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")
|
||||
@@ -171,7 +209,6 @@ func randomize_grid_custom(randomize_states: Array, floor_index: int = -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:
|
||||
@@ -203,13 +240,11 @@ func randomize_floor_custom(randomize_states: Array, floor_index: int):
|
||||
selected_state = state
|
||||
break
|
||||
|
||||
# Get current cell orientation
|
||||
var current_orientation = get_cell_item_orientation(cell_pos)
|
||||
|
||||
if selected_state:
|
||||
set_cell_item(cell_pos, selected_state.id, current_orientation)
|
||||
else:
|
||||
# Find first enabled state or use normal item as fallback
|
||||
var fallback_state = null
|
||||
for state in randomize_states:
|
||||
if state.include_in_randomize:
|
||||
@@ -221,117 +256,102 @@ func randomize_floor_custom(randomize_states: Array, floor_index: int):
|
||||
else:
|
||||
set_cell_item(cell_pos, normal_items[0], current_orientation)
|
||||
|
||||
# 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
|
||||
# Improved neighbor checking system
|
||||
func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInfo]:
|
||||
var neighbors: Array[NeighborInfo] = []
|
||||
|
||||
if item_index < 0 or item_index >= mesh_library.get_item_list().size():
|
||||
print("Invalid item index")
|
||||
return
|
||||
var directions = {
|
||||
Direction.NORTHWEST: Vector2i(-1, -1),
|
||||
Direction.NORTH: Vector2i(0, -1),
|
||||
Direction.NORTHEAST: Vector2i(1, -1),
|
||||
Direction.WEST: Vector2i(-1, 0),
|
||||
Direction.EAST: Vector2i(1, 0),
|
||||
Direction.SOUTHWEST: Vector2i(-1, 1),
|
||||
Direction.SOUTH: Vector2i(0, 1),
|
||||
Direction.SOUTHEAST: Vector2i(1, 1)
|
||||
}
|
||||
|
||||
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")
|
||||
for dir in directions:
|
||||
var offset = directions[dir]
|
||||
var neighbor_pos = current_pos + offset
|
||||
|
||||
if is_position_valid(neighbor_pos):
|
||||
var is_walkable = is_cell_walkable(neighbor_pos, floor_index)
|
||||
|
||||
if is_diagonal_direction(dir):
|
||||
var adjacent1: Vector2i
|
||||
var adjacent2: Vector2i
|
||||
|
||||
match dir:
|
||||
Direction.NORTHWEST:
|
||||
adjacent1 = current_pos + Vector2i(-1, 0)
|
||||
adjacent2 = current_pos + Vector2i(0, -1)
|
||||
Direction.NORTHEAST:
|
||||
adjacent1 = current_pos + Vector2i(1, 0)
|
||||
adjacent2 = current_pos + Vector2i(0, -1)
|
||||
Direction.SOUTHWEST:
|
||||
adjacent1 = current_pos + Vector2i(-1, 0)
|
||||
adjacent2 = current_pos + Vector2i(0, 1)
|
||||
Direction.SOUTHEAST:
|
||||
adjacent1 = current_pos + Vector2i(1, 0)
|
||||
adjacent2 = current_pos + Vector2i(0, 1)
|
||||
|
||||
is_walkable = is_walkable and \
|
||||
is_position_valid(adjacent1) and is_cell_walkable(adjacent1, floor_index) and \
|
||||
is_position_valid(adjacent2) and is_cell_walkable(adjacent2, floor_index)
|
||||
|
||||
if diagonal_movement or not is_diagonal_direction(dir):
|
||||
neighbors.append(NeighborInfo.new(neighbor_pos, dir, is_walkable))
|
||||
|
||||
update_grid_data()
|
||||
initialize_astar()
|
||||
update_astar_costs()
|
||||
return neighbors
|
||||
|
||||
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)
|
||||
# Helper functions for neighbor checking
|
||||
func is_diagonal_direction(direction: Direction) -> bool:
|
||||
return direction in [Direction.NORTHWEST, Direction.NORTHEAST,
|
||||
Direction.SOUTHWEST, Direction.SOUTHEAST]
|
||||
|
||||
func _on_mesh_library_changed():
|
||||
validate_item_indices()
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
func is_position_valid(pos: Vector2i) -> bool:
|
||||
return pos.x >= 0 and pos.x < columns and pos.y >= 0 and pos.y < rows
|
||||
|
||||
func _set(property, value):
|
||||
if property == "mesh_library":
|
||||
mesh_library = value
|
||||
_on_mesh_library_changed()
|
||||
return true
|
||||
return false
|
||||
func is_cell_walkable(pos: Vector2i, floor_index: int) -> bool:
|
||||
var cell_item = get_cell_item(Vector3i(pos.x, floor_index, pos.y))
|
||||
return cell_item != -1 and not (cell_item in non_walkable_items)
|
||||
|
||||
# Modified update_grid_data for 3D array
|
||||
func update_grid_data():
|
||||
grid_data.clear()
|
||||
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")
|
||||
|
||||
# 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
|
||||
# Improved A* pathfinding
|
||||
func initialize_astar():
|
||||
astar_by_floor.clear()
|
||||
for y in range(floors):
|
||||
var astar = AStar2D.new()
|
||||
|
||||
# Add all points
|
||||
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 points based on neighbors
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
var current_pos = Vector2i(x, z)
|
||||
var current_point_id = z * columns + x
|
||||
|
||||
if x > 0:
|
||||
astar.connect_points(point_id, point_id - 1)
|
||||
if z > 0:
|
||||
astar.connect_points(point_id, point_id - columns)
|
||||
if not is_cell_walkable(current_pos, y):
|
||||
continue
|
||||
|
||||
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
|
||||
var neighbors = get_neighbors(current_pos, y)
|
||||
for neighbor in neighbors:
|
||||
if neighbor.is_walkable:
|
||||
var neighbor_id = neighbor.position.y * columns + neighbor.position.x
|
||||
|
||||
if not astar.are_points_connected(current_point_id, neighbor_id):
|
||||
var weight = 1.0 if not is_diagonal_direction(neighbor.direction) else 1.4142
|
||||
astar.connect_points(current_point_id, neighbor_id, true)
|
||||
astar.set_point_weight_scale(neighbor_id, weight)
|
||||
|
||||
astar_by_floor[y] = astar
|
||||
|
||||
update_astar_costs()
|
||||
|
||||
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, 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)
|
||||
|
||||
# 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:
|
||||
@@ -350,7 +370,7 @@ func find_path(start: Vector2, end: Vector2, floor_index: int = 0) -> Array:
|
||||
|
||||
return path
|
||||
|
||||
# Modified path visualization clearing for specific floor
|
||||
# Path visualization
|
||||
func clear_path_visualization(floor_index: int = 0):
|
||||
for x in range(columns):
|
||||
for z in range(rows):
|
||||
@@ -358,6 +378,7 @@ func clear_path_visualization(floor_index: int = 0):
|
||||
if cell_item == hover_item or cell_item == start_item or cell_item == end_item:
|
||||
set_cell_item(Vector3i(x, floor_index, z), normal_items[0])
|
||||
|
||||
# Cost calculation and updates
|
||||
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:
|
||||
@@ -368,7 +389,6 @@ func get_cell_cost(x: int, z: int, floor_index: int = 0) -> float:
|
||||
return 0.0
|
||||
return 1.0
|
||||
|
||||
# Modified update_astar_costs for multiple floors
|
||||
func update_astar_costs():
|
||||
for floor_index in range(floors):
|
||||
var astar = astar_by_floor.get(floor_index)
|
||||
@@ -383,11 +403,42 @@ func update_astar_costs():
|
||||
astar.set_point_disabled(point_id, false)
|
||||
astar.set_point_weight_scale(point_id, cost)
|
||||
|
||||
# Grid data management
|
||||
func update_grid_data():
|
||||
grid_data.clear()
|
||||
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")
|
||||
|
||||
# Cell rotation handling
|
||||
func get_cell_rotation(position: Vector3i) -> int:
|
||||
return get_cell_item_orientation(position)
|
||||
|
||||
func set_cell_rotation(position: Vector3i, mode: int):
|
||||
var item = get_cell_item(position)
|
||||
if item != INVALID_CELL_ITEM:
|
||||
var orientation = int(mode)
|
||||
set_cell_item(position, item, orientation)
|
||||
if item != -1:
|
||||
set_cell_item(position, item, mode)
|
||||
|
||||
# Mesh library handling
|
||||
func _on_mesh_library_changed():
|
||||
validate_item_indices()
|
||||
if auto_generate:
|
||||
generate_grid()
|
||||
|
||||
func _set(property, value):
|
||||
if property == "mesh_library":
|
||||
mesh_library = value
|
||||
_on_mesh_library_changed()
|
||||
return true
|
||||
return false
|
||||
|
||||
# Toggle diagonal movement
|
||||
func set_diagonal_movement(enable: bool):
|
||||
diagonal_movement = enable
|
||||
initialize_astar()
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
[ext_resource type="ArrayMesh" uid="uid://bgvropltcot0q" path="res://assets/models/meshes/normal.res" id="5_san4u"]
|
||||
[ext_resource type="ArrayMesh" uid="uid://36tgon3b60db" path="res://assets/models/tiles/tile_heart.tres" id="6_r6sve"]
|
||||
[ext_resource type="ArrayMesh" uid="uid://cv4bedhida00g" path="res://assets/models/tiles/tile_star.tres" id="7_v40i1"]
|
||||
[ext_resource type="ArrayMesh" uid="uid://bhlvvdtiykgnn" path="res://assets/models/meshes/tiles.res" id="8_ul3lk"]
|
||||
[ext_resource type="ArrayMesh" uid="uid://b5ta7tcw0iscd" path="res://assets/models/tiles/tile_coin.tres" id="9_44311"]
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2vf4e"]
|
||||
resource_name = "boost"
|
||||
@@ -17,7 +17,7 @@ cull_mode = 2
|
||||
albedo_color = Color(0.91, 0.91, 0.91, 0.45098)
|
||||
albedo_texture = ExtResource("3_re0fd")
|
||||
|
||||
[sub_resource type="ArrayMesh" id="ArrayMesh_0evu4"]
|
||||
[sub_resource type="ArrayMesh" id="ArrayMesh_ff8mg"]
|
||||
_surfaces = [{
|
||||
"aabb": AABB(-0.282176, -0.000324821, -0.282176, 0.564351, 0.050792, 0.564351),
|
||||
"format": 34896613377,
|
||||
@@ -31,7 +31,7 @@ _surfaces = [{
|
||||
}]
|
||||
blend_shape_mode = 0
|
||||
|
||||
[sub_resource type="ArrayMesh" id="ArrayMesh_pte2c"]
|
||||
[sub_resource type="ArrayMesh" id="ArrayMesh_64nap"]
|
||||
resource_name = "tile_diamond"
|
||||
_surfaces = [{
|
||||
"aabb": AABB(-0.282176, -0.000324821, -0.282176, 0.564351, 0.050792, 0.564351),
|
||||
@@ -47,7 +47,7 @@ _surfaces = [{
|
||||
"vertex_data": PackedByteArray("AAD//////78AAP//////vwAA/////6oqAAD+/wAAAAAAAP7/AAD/vwAA/v8AAKoq/////////7//////////v//////////////+/wAAAAD///7/AAD/v////v8AAP////8AAP///7///wAA////P///AAD///////8AAAAAAAD//wAAAAD/P///AAAAAP//AAAAAP///78AAAAA////PwAAAAD//6oqAAAAAAAAAAAAAAAAAAD/PwAAAAAAAKoq/////////39U1VTV/7//v////39U1VTV/////////3//v/9//7//v////3//v/9//////wAA/3//v/9//7//vwAA/3//v/9//////wAA/39U1VTV/7//vwAA/39U1VTV")
|
||||
}]
|
||||
blend_shape_mode = 0
|
||||
shadow_mesh = SubResource("ArrayMesh_0evu4")
|
||||
shadow_mesh = SubResource("ArrayMesh_ff8mg")
|
||||
|
||||
[sub_resource type="PlaneMesh" id="PlaneMesh_ti6kf"]
|
||||
material = ExtResource("3_qi66w")
|
||||
@@ -103,13 +103,13 @@ item/8/shapes = []
|
||||
item/8/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
|
||||
item/8/navigation_layers = 1
|
||||
item/9/name = "tile_coin"
|
||||
item/9/mesh = ExtResource("8_ul3lk")
|
||||
item/9/mesh = ExtResource("9_44311")
|
||||
item/9/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.9, 0)
|
||||
item/9/shapes = []
|
||||
item/9/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
|
||||
item/9/navigation_layers = 1
|
||||
item/10/name = "tile_diamond"
|
||||
item/10/mesh = SubResource("ArrayMesh_pte2c")
|
||||
item/10/mesh = SubResource("ArrayMesh_64nap")
|
||||
item/10/mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.9, 0)
|
||||
item/10/shapes = []
|
||||
item/10/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
|
||||
|
||||
Reference in New Issue
Block a user