update obstacles

This commit is contained in:
2025-03-11 17:40:54 +08:00
parent 495c9c64a5
commit 161f03d931
3 changed files with 225 additions and 50 deletions
+70 -23
View File
@@ -297,6 +297,45 @@ func is_within_movement_range(target_position: Vector2i) -> bool:
distance = abs(target_position.x - current_position.x) + abs(target_position.y - current_position.y)
return distance <= movement_range
#func move_player_to_clicked_position(grid_position: Vector2i):
#if not is_multiplayer_authority() or is_player_moving or action_points <= 0:
#return
#
#var main = get_tree().get_root().get_node_or_null("Main")
#if not main or main.current_action_state != main.ActionState.MOVING or not grid_position in highlighted_cells:
#return
#
#if not is_within_movement_range(grid_position):
#return
#
#var cell_item = enhanced_gridmap.get_cell_item(Vector3i(grid_position.x, 0, grid_position.y))
#if cell_item in enhanced_gridmap.non_walkable_items or is_position_occupied(grid_position):
#return
#
#rotate_towards_target(grid_position)
#
#var path = enhanced_gridmap.find_path(Vector2(current_position), Vector2(grid_position))
#if path.size() <= 1:
#return
#
#var valid_path = true
#for point in path.slice(1):
#if is_position_occupied(Vector2i(point.x, point.y)):
#valid_path = false
#break
#
#if valid_path:
#path.pop_front()
## Pass clear_visual=false for bots to preserve player highlights
#rpc("start_movement_along_path", path, not (is_bot or is_in_group("Bots")))
#action_points -= 1
#
## Only clear highlights if not a bot
#if not (is_bot or is_in_group("Bots")):
#clear_highlights()
#else:
#print("Path is blocked by other players")
func move_player_to_clicked_position(grid_position: Vector2i):
if not is_multiplayer_authority() or is_player_moving or action_points <= 0:
return
@@ -312,29 +351,25 @@ func move_player_to_clicked_position(grid_position: Vector2i):
if cell_item in enhanced_gridmap.non_walkable_items or is_position_occupied(grid_position):
return
# Check if direct movement is blocked by an obstacle
if enhanced_gridmap.is_blocked_by_obstacle(current_position, grid_position, 3):
# Do not allow movement if blocked (this should not happen if highlight logic is correct)
print("Movement blocked by obstacle")
return
rotate_towards_target(grid_position)
var path = enhanced_gridmap.find_path(Vector2(current_position), Vector2(grid_position))
if path.size() <= 1:
return
var valid_path = true
for point in path.slice(1):
if is_position_occupied(Vector2i(point.x, point.y)):
valid_path = false
break
if valid_path:
path.pop_front()
# Pass clear_visual=false for bots to preserve player highlights
rpc("start_movement_along_path", path, not (is_bot or is_in_group("Bots")))
action_points -= 1
# Only clear highlights if not a bot
if not (is_bot or is_in_group("Bots")):
clear_highlights()
else:
print("Path is blocked by other players")
# Create a direct path rather than using A* for obstacle avoidance
# This ensures the player can only move to directly accessible positions
var path = [Vector2(current_position.x, current_position.y), Vector2(grid_position.x, grid_position.y)]
path.pop_front()
rpc("start_movement_along_path", path, not (is_bot or is_in_group("Bots")))
action_points -= 1
# Clear highlights after moving
if not (is_bot or is_in_group("Bots")):
clear_highlights()
@rpc("any_peer", "call_local")
func start_movement_along_path(path: Array, clear_visual: bool = true):
@@ -815,13 +850,25 @@ func highlight_movement_range():
return
var cells_to_highlight = []
# Only highlight cells within movement range that aren't blocked by obstacles
for x in range(enhanced_gridmap.columns):
for z in range(enhanced_gridmap.rows):
var test_pos = Vector2i(x, z)
# Skip the current position
if test_pos == current_position:
continue
# Check if within movement range
if is_within_movement_range(test_pos):
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
# Check if cell is walkable and not occupied
if cell_item != -1 and not (cell_item in enhanced_gridmap.non_walkable_items) and not is_position_occupied(test_pos):
cells_to_highlight.append(test_pos)
# Most importantly, check if movement is blocked by obstacles
if not enhanced_gridmap.is_blocked_by_obstacle(current_position, test_pos, 3):
cells_to_highlight.append(test_pos)
highlight_cells_if_authorized(cells_to_highlight)
@@ -976,7 +1023,7 @@ func clear_highlights():
child.hide()
# Restore highlights based on current action state
if main and current_state == main.ActionState.MOVING and is_my_turn:
if main and current_state == main.ActionState.MOVING and is_my_turn and current_state != main.ActionState.PLACING_OBSTACLE:
highlight_movement_range()
func clear_playerboard_highlights():