This commit is contained in:
2025-03-10 15:44:54 +08:00
parent 438eff1f7a
commit 495c9c64a5
97 changed files with 532 additions and 32 deletions
+83 -2
View File
@@ -23,11 +23,20 @@ var grid_data: Array = [] # 3D array [floor][row][column]
var astar_by_floor = {} # Dictionary of AStar2D instances per floor
var path = []
# Update the obstacle items array to use your specified item indices
@export var obstacle_items: Array[int] = [12, 13, 14, 15] # Updated mesh library indices
@export var obstacle_directions: Array[Direction] = [] # Store the directions of placed obstacles
# Direction and movement systems
enum Direction {
NORTHWEST, NORTH, NORTHEAST,
WEST, CENTER, EAST,
SOUTHWEST, SOUTH, SOUTHEAST
SOUTHWEST, SOUTH, SOUTHEAST,
BLOCKED_NORTH = 10,
BLOCKED_EAST = 11,
BLOCKED_SOUTH = 12,
BLOCKED_WEST = 13
}
var diagonal_movement: bool = false
@@ -299,7 +308,9 @@ func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInf
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)
is_position_valid(adjacent2) and is_cell_walkable(adjacent2, floor_index) and \
not is_blocked_by_obstacle(current_pos, adjacent1, floor_index) and \
not is_blocked_by_obstacle(current_pos, adjacent2, floor_index)
if diagonal_movement or not is_diagonal_direction(dir):
neighbors.append(NeighborInfo.new(neighbor_pos, dir, is_walkable))
@@ -461,3 +472,73 @@ func _set(property, value):
func set_diagonal_movement(enable: bool):
diagonal_movement = enable
initialize_astar()
# Add this function to check if a movement is blocked by an obstacle
func is_blocked_by_obstacle(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 0) -> bool:
# Check if there's a horizontal obstacle
if from_pos.y == to_pos.y: # Moving horizontally
# Check for vertical obstacles that would block horizontal movement
var min_x = min(from_pos.x, to_pos.x)
var max_x = max(from_pos.x, to_pos.x)
for x in range(min_x, max_x + 1):
var cell_index = get_cell_item(Vector3i(x, floor_index, from_pos.y))
if cell_index in obstacle_items:
var obstacle_idx = obstacle_items.find(cell_index)
if obstacle_idx != -1 and obstacle_idx < obstacle_directions.size():
var dir = obstacle_directions[obstacle_idx]
if dir == Direction.BLOCKED_NORTH or dir == Direction.BLOCKED_SOUTH:
return true
# Check if there's a vertical obstacle
if from_pos.x == to_pos.x: # Moving vertically
# Check for horizontal obstacles that would block vertical movement
var min_y = min(from_pos.y, to_pos.y)
var max_y = max(from_pos.y, to_pos.y)
for y in range(min_y, max_y + 1):
var cell_index = get_cell_item(Vector3i(from_pos.x, floor_index, y))
if cell_index in obstacle_items:
var obstacle_idx = obstacle_items.find(cell_index)
if obstacle_idx != -1 and obstacle_idx < obstacle_directions.size():
var dir = obstacle_directions[obstacle_idx]
if dir == Direction.BLOCKED_EAST or dir == Direction.BLOCKED_WEST:
return true
return false
func place_obstacle(pos: Vector3i, obstacle_item: int, direction: Direction) -> bool:
# Always place on floor 3
pos.y = 3
if get_cell_item(pos) != -1:
return false # Cell is already occupied
set_cell_item(pos, obstacle_item)
# Store the direction of the obstacle
var item_index = obstacle_items.find(obstacle_item)
if item_index == -1:
item_index = 0 # Default to first item if not found
while obstacle_directions.size() <= item_index:
obstacle_directions.append(Direction.CENTER) # Default
obstacle_directions[item_index] = direction
# Update the cell's orientation based on direction
var orientation = 0
match direction:
Direction.BLOCKED_NORTH:
orientation = 0 # Default orientation
Direction.BLOCKED_EAST:
orientation = 1 # 90 degrees clockwise
Direction.BLOCKED_SOUTH:
orientation = 2 # 180 degrees
Direction.BLOCKED_WEST:
orientation = 3 # 270 degrees clockwise
set_cell_item(pos, obstacle_item, orientation)
# Re-initialize A* pathfinding to account for the new obstacle
initialize_astar()
return true