player.gd refactor progress
This commit is contained in:
+136
-589
@@ -1,19 +1,36 @@
|
||||
extends Node3D
|
||||
|
||||
# Managers
|
||||
var movement_manager
|
||||
var race_manager
|
||||
var input_manager
|
||||
var playerboard_manager
|
||||
var action_manager
|
||||
|
||||
@export var is_bot: bool = false
|
||||
|
||||
@export var enhanced_gridmap_path: NodePath = "/root/Main/EnhancedGridMap"
|
||||
var enhanced_gridmap: EnhancedGridMap
|
||||
@export var current_position: Vector2i
|
||||
var is_player_moving: bool = false
|
||||
var is_player_moving: bool = false:
|
||||
get: return movement_manager.is_moving if movement_manager else false
|
||||
set(value): if movement_manager: movement_manager.is_moving = value
|
||||
|
||||
var _verify_timer: float = 0.0
|
||||
var can_finish
|
||||
var can_finish: bool:
|
||||
get: return race_manager.can_finish if race_manager else false
|
||||
set(value): if race_manager: race_manager.can_finish = value
|
||||
|
||||
@export var cell_size: Vector3 = Vector3(2, 2, 2)
|
||||
@export var cell_offset: Vector3 = Vector3(0, 0, 0)
|
||||
|
||||
@export var goals: Array[int] = [0,0,0,0,0,0,0,0,0]
|
||||
@export var playerboard: Array[int] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
|
||||
@export var goals: Array[int]:
|
||||
get: return race_manager.goals if race_manager else Array([], TYPE_INT, "", null) as Array[int]
|
||||
set(value): if race_manager: race_manager.goals = value
|
||||
|
||||
@export var playerboard: Array[int]:
|
||||
get: return race_manager.playerboard if race_manager else Array([], TYPE_INT, "", null) as Array[int]
|
||||
set(value): if race_manager: race_manager.playerboard = value
|
||||
|
||||
# Modifier for Turn based
|
||||
var has_performed_action: bool = false
|
||||
@@ -23,7 +40,6 @@ var targeted_playerboard_slot = -1
|
||||
var action_points: int = 2
|
||||
|
||||
# Modifier for player models
|
||||
var target_rotation: float = 0.0
|
||||
var rotation_speed: float = 10.0
|
||||
|
||||
var spawn_locations = [
|
||||
@@ -44,22 +60,8 @@ var spawn_locations = [
|
||||
]
|
||||
|
||||
# Add these as class variables at the top of the file
|
||||
var finish_locations = [
|
||||
Vector2i(13, 0), # (13,0,0)
|
||||
Vector2i(13, 1), # (13,0,1)
|
||||
Vector2i(13, 2), # (13,0,2)
|
||||
Vector2i(13, 3), # (13,0,3)
|
||||
Vector2i(13, 4), # (13,0,4)
|
||||
Vector2i(13, 5), # (13,0,5)
|
||||
Vector2i(13, 6),
|
||||
Vector2i(13, 7),
|
||||
Vector2i(13, 8),
|
||||
Vector2i(13, 9),
|
||||
Vector2i(13, 10),
|
||||
Vector2i(13, 11),
|
||||
Vector2i(13, 12),
|
||||
Vector2i(13, 13)
|
||||
]
|
||||
var finish_locations: Array:
|
||||
get: return race_manager.finish_locations if race_manager else []
|
||||
|
||||
var spawn_point_selected = false
|
||||
|
||||
@@ -69,12 +71,17 @@ var highlighted_cells = []
|
||||
var _is_processing_action = false
|
||||
var _is_highlighting = false
|
||||
|
||||
@export var movement_range: int = 1
|
||||
@export var movement_range: int = 1:
|
||||
set(value):
|
||||
movement_range = value
|
||||
if movement_manager: movement_manager.movement_range = value
|
||||
|
||||
@export var use_diagonal_movement: bool = false:
|
||||
set(value):
|
||||
use_diagonal_movement = value
|
||||
if enhanced_gridmap:
|
||||
enhanced_gridmap.set_diagonal_movement(value)
|
||||
if movement_manager: movement_manager.use_diagonal_movement = value
|
||||
|
||||
@export var is_my_turn: bool = false:
|
||||
set(value):
|
||||
@@ -85,21 +92,7 @@ var _is_highlighting = false
|
||||
@export var has_moved_this_turn = false
|
||||
|
||||
# Track, Lap, Position
|
||||
var current_lap: int = 0
|
||||
var first_lap_goals: Array[int] = [] # Current goals (partial 3x3)
|
||||
var second_lap_goals: Array[int] = [] # Full 3x3 goals
|
||||
var race_position: int = 0 # Track finish position
|
||||
static var lap1_finishers: int = 0 # Static to track across all players
|
||||
static var lap2_finishers: int = 0
|
||||
|
||||
# Function to get ordinal string (1st, 2nd, 3rd, 4th)
|
||||
func get_ordinal_string(number: int) -> String:
|
||||
match number:
|
||||
1: return "1st"
|
||||
2: return "2nd"
|
||||
3: return "3rd"
|
||||
4: return "4th"
|
||||
_: return str(number) + "th"
|
||||
# Delegated to RaceManager
|
||||
|
||||
func _ready():
|
||||
# Ensure name is set first
|
||||
@@ -115,17 +108,12 @@ func _ready():
|
||||
push_error("Main scene not found")
|
||||
return
|
||||
|
||||
# Modifier to handle the lap
|
||||
if is_multiplayer_authority():
|
||||
# Generate the first_lap_goals
|
||||
first_lap_goals = goals.duplicate()
|
||||
# Generate the second_lap_goals
|
||||
generate_second_lap_goals()
|
||||
|
||||
# Ensure proper initialization order
|
||||
enhanced_gridmap = get_node(enhanced_gridmap_path)
|
||||
if main_scene:
|
||||
enhanced_gridmap = main_scene.get_node("EnhancedGridMap")
|
||||
|
||||
_init_managers()
|
||||
|
||||
# Early setup for bots
|
||||
if is_bot == true or is_in_group("Bots"):
|
||||
@@ -200,102 +188,50 @@ func _ready():
|
||||
if is_multiplayer_authority():
|
||||
rpc("sync_position", current_position)
|
||||
|
||||
func _init_managers():
|
||||
movement_manager = load("res://scripts/managers/player_movement_manager.gd").new()
|
||||
movement_manager.name = "MovementManager"
|
||||
add_child(movement_manager)
|
||||
movement_manager.initialize(self, enhanced_gridmap)
|
||||
|
||||
race_manager = load("res://scripts/managers/player_race_manager.gd").new()
|
||||
race_manager.name = "RaceManager"
|
||||
add_child(race_manager)
|
||||
race_manager.initialize(self, enhanced_gridmap)
|
||||
|
||||
input_manager = load("res://scripts/managers/player_input_manager.gd").new()
|
||||
input_manager.name = "InputManager"
|
||||
add_child(input_manager)
|
||||
input_manager.initialize(self, movement_manager, race_manager)
|
||||
|
||||
playerboard_manager = load("res://scripts/managers/playerboard_manager.gd").new()
|
||||
playerboard_manager.name = "PlayerboardManager"
|
||||
add_child(playerboard_manager)
|
||||
playerboard_manager.initialize(self, enhanced_gridmap)
|
||||
|
||||
action_manager = load("res://scripts/managers/player_action_manager.gd").new()
|
||||
action_manager.name = "ActionManager"
|
||||
add_child(action_manager)
|
||||
action_manager.initialize(self, enhanced_gridmap)
|
||||
|
||||
# Add function to check if position is at finish line
|
||||
func is_at_finish_line() -> bool:
|
||||
return current_position in finish_locations
|
||||
return race_manager.is_at_finish_line()
|
||||
|
||||
# Helper function to check if a 3x3 section matches the goals pattern
|
||||
func check_3x3_section(board: Array, goals: Array, start_row: int, start_col: int) -> bool:
|
||||
# First check if any required positions are empty (-1)
|
||||
for i in range(3):
|
||||
for j in range(3):
|
||||
if goals[i][j] != -1: # Only check positions that are required by the goals
|
||||
var board_value = board[start_row + i][start_col + j]
|
||||
if board_value == -1: # If required position is empty
|
||||
return false
|
||||
|
||||
# Then check if the pattern matches
|
||||
for i in range(3):
|
||||
for j in range(3):
|
||||
if goals[i][j] != -1: # Only check positions that are required by the goals
|
||||
if board[start_row + i][start_col + j] != goals[i][j]:
|
||||
return false
|
||||
|
||||
return true
|
||||
# Delegated to RaceManager
|
||||
|
||||
# Generate full 3x3 goals for second lap
|
||||
func generate_second_lap_goals():
|
||||
second_lap_goals.clear()
|
||||
# Generate a complete 3x3 pattern (no empty spaces)
|
||||
for i in range(9):
|
||||
var val = (randi() % 4) + 7 # Values between 7 and 10
|
||||
second_lap_goals.append(val)
|
||||
|
||||
if is_multiplayer_authority():
|
||||
rpc("sync_second_lap_goals", second_lap_goals)
|
||||
|
||||
@rpc("any_peer", "reliable")
|
||||
func sync_second_lap_goals(new_goals: Array):
|
||||
second_lap_goals = new_goals
|
||||
# Delegated to RaceManager
|
||||
|
||||
# Modify finish_race to handle lap completion
|
||||
@rpc("any_peer", "reliable")
|
||||
func finish_race():
|
||||
if current_lap == 0: # Finishing first lap
|
||||
lap1_finishers += 1
|
||||
race_position = lap1_finishers
|
||||
|
||||
# Display first lap completion message
|
||||
var message = "Finish 1st lap on " + get_ordinal_string(race_position)
|
||||
if is_multiplayer_authority():
|
||||
rpc("display_message", message)
|
||||
|
||||
# Start second lap
|
||||
current_lap += 1
|
||||
rpc("start_new_lap")
|
||||
|
||||
elif current_lap == 1: # Finishing second lap
|
||||
lap2_finishers += 1
|
||||
race_position = lap2_finishers
|
||||
|
||||
# Display second lap completion message
|
||||
var message = "Finish 2nd lap on " + get_ordinal_string(race_position)
|
||||
if is_multiplayer_authority():
|
||||
rpc("display_message", message)
|
||||
race_manager.finish_race()
|
||||
|
||||
# Add function to check 3x3 pattern matching anywhere in 5x5 playerboard
|
||||
func check_pattern_match() -> bool:
|
||||
# Early return if playerboard or goals are not properly sized
|
||||
if playerboard.size() != 25 or goals.size() != 9:
|
||||
return false
|
||||
|
||||
var current_goals = goals
|
||||
|
||||
# Convert 1D arrays to 2D for easier pattern matching
|
||||
var board_2d = []
|
||||
var goals_2d = []
|
||||
|
||||
# Convert playerboard to 2D (5x5)
|
||||
for i in range(5):
|
||||
var row = []
|
||||
for j in range(5):
|
||||
row.append(playerboard[i * 5 + j])
|
||||
board_2d.append(row)
|
||||
|
||||
# Convert goals to 2D (3x3)
|
||||
for i in range(3):
|
||||
var row = []
|
||||
for j in range(3):
|
||||
row.append(goals[i * 3 + j])
|
||||
goals_2d.append(row)
|
||||
|
||||
# Check each possible 3x3 section in the 5x5 board
|
||||
for start_row in range(3): # 5-3+1 possible starting rows
|
||||
for start_col in range(3): # 5-3+1 possible starting columns
|
||||
if check_3x3_section(board_2d, goals_2d, start_row, start_col):
|
||||
return true
|
||||
|
||||
return false
|
||||
return race_manager.check_pattern_match()
|
||||
|
||||
## Add function to handle new lap
|
||||
#@rpc("any_peer", "reliable")
|
||||
@@ -318,68 +254,21 @@ func check_pattern_match() -> bool:
|
||||
# Modify start_new_lap to handle different lap goals and starting positions
|
||||
@rpc("any_peer", "reliable")
|
||||
func start_new_lap():
|
||||
if current_lap == 1: # Moving to second lap
|
||||
# Start from first lap finish line
|
||||
var valid_finish_pos = find_valid_position_in_finish_line()
|
||||
if valid_finish_pos != Vector2i(-1, -1):
|
||||
current_position = valid_finish_pos
|
||||
update_player_position(current_position)
|
||||
|
||||
# Set new goals (full 3x3)
|
||||
goals = second_lap_goals.duplicate()
|
||||
|
||||
# Reset playerboard
|
||||
#playerboard.fill(-1)
|
||||
|
||||
# Reset can_finish flag
|
||||
can_finish = false
|
||||
|
||||
# Sync with other clients
|
||||
if is_multiplayer_authority():
|
||||
rpc("sync_position", current_position)
|
||||
rpc("sync_playerboard", playerboard)
|
||||
rpc("sync_goals", goals)
|
||||
race_manager.start_new_lap()
|
||||
|
||||
# Function to find valid position in finish line
|
||||
func find_valid_position_in_finish_line() -> Vector2i:
|
||||
for pos in finish_locations:
|
||||
if not is_position_occupied(pos):
|
||||
return pos
|
||||
return Vector2i(-1, -1)
|
||||
# Delegated to RaceManager
|
||||
|
||||
# Add function to check if player can reach finish
|
||||
func update_finish_availability():
|
||||
can_finish = check_pattern_match()
|
||||
|
||||
# Update visual feedback if needed
|
||||
if is_multiplayer_authority():
|
||||
if can_finish:
|
||||
highlight_finish_line()
|
||||
else:
|
||||
unhighlight_finish_line()
|
||||
race_manager.update_finish_availability()
|
||||
|
||||
func unhighlight_finish_line():
|
||||
if not is_multiplayer_authority() or is_bot:
|
||||
return
|
||||
|
||||
for finish_pos in finish_locations:
|
||||
if enhanced_gridmap:
|
||||
enhanced_gridmap.set_cell_item(
|
||||
Vector3i(finish_pos.x, 0, finish_pos.y),
|
||||
enhanced_gridmap.normal_items[0]
|
||||
)
|
||||
race_manager.unhighlight_finish_line()
|
||||
|
||||
# Add functions to handle finish line visualization
|
||||
func highlight_finish_line():
|
||||
if not is_multiplayer_authority() or is_bot:
|
||||
return
|
||||
|
||||
for finish_pos in finish_locations:
|
||||
if enhanced_gridmap:
|
||||
enhanced_gridmap.set_cell_item(
|
||||
Vector3i(finish_pos.x, 0, finish_pos.y),
|
||||
enhanced_gridmap.hover_item
|
||||
)
|
||||
race_manager.highlight_finish_line()
|
||||
|
||||
func request_spawn_positions_update():
|
||||
if multiplayer.is_server():
|
||||
@@ -449,6 +338,10 @@ func _process(delta):
|
||||
if _verify_timer >= 3.0:
|
||||
_verify_timer = 0.0
|
||||
rpc("ping_existence")
|
||||
|
||||
# Delegate rotation to movement manager
|
||||
if movement_manager:
|
||||
movement_manager._process(delta)
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func ping_existence():
|
||||
@@ -461,7 +354,7 @@ func _physics_process(delta):
|
||||
rpc("remote_set_position", global_position)
|
||||
|
||||
# Add continuous finish line check
|
||||
if current_position in finish_locations and can_finish and not is_player_moving:
|
||||
if race_manager and current_position in race_manager.finish_locations and can_finish and not is_player_moving:
|
||||
start_new_lap()
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
@@ -469,126 +362,17 @@ func _physics_process(delta):
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
func _unhandled_input(event):
|
||||
# Early return if not authorized human player
|
||||
if not is_multiplayer_authority() or is_bot or is_in_group("Bots"):
|
||||
set_process_unhandled_input(false)
|
||||
return
|
||||
|
||||
var main = get_node("/root/Main")
|
||||
|
||||
# --- Real-time Keyboard/Touch Input ---
|
||||
if not main.turn_based_mode and not is_player_moving:
|
||||
var target_position = current_position
|
||||
var input_handled = true
|
||||
|
||||
if Input.is_action_just_pressed("move_north"):
|
||||
target_position += Vector2i(0, -1)
|
||||
elif Input.is_action_just_pressed("move_northeast"):
|
||||
target_position += Vector2i(1, -1)
|
||||
elif Input.is_action_just_pressed("move_east"):
|
||||
target_position += Vector2i(1, 0)
|
||||
elif Input.is_action_just_pressed("move_southeast"):
|
||||
target_position += Vector2i(1, 1)
|
||||
elif Input.is_action_just_pressed("move_south"):
|
||||
target_position += Vector2i(0, 1)
|
||||
elif Input.is_action_just_pressed("move_southwest"):
|
||||
target_position += Vector2i(-1, 1)
|
||||
elif Input.is_action_just_pressed("move_west"):
|
||||
target_position += Vector2i(-1, 0)
|
||||
elif Input.is_action_just_pressed("move_northwest"):
|
||||
target_position += Vector2i(-1, -1)
|
||||
elif Input.is_action_just_pressed("action_grab"):
|
||||
grab_item(current_position) # Try to grab from current position [cite: 59]
|
||||
elif Input.is_action_just_pressed("action_put"):
|
||||
auto_put_item()
|
||||
else:
|
||||
input_handled = false # No relevant key was pressed
|
||||
|
||||
if target_position != current_position:
|
||||
simple_move_to(target_position)
|
||||
|
||||
if input_handled:
|
||||
get_viewport().set_input_as_handled() # Consume the event
|
||||
return # Don't process turn-based or spawn logic
|
||||
# --- End Real-time Input ---
|
||||
|
||||
# Handle spawn point selection if not yet selected
|
||||
if not spawn_point_selected and highlighted_spawn_points.size() > 0:
|
||||
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
var camera = get_viewport().get_camera_3d()
|
||||
var from = camera.project_ray_origin(event.position)
|
||||
var to = from + camera.project_ray_normal(event.position) * 1000
|
||||
|
||||
var click_position = raycast_to_grid(from, to)
|
||||
if click_position in highlighted_spawn_points:
|
||||
if select_spawn_point(click_position):
|
||||
return # Spawn point selected successfully
|
||||
|
||||
# Use get_node_or_null for safer node access
|
||||
if not main:
|
||||
return
|
||||
|
||||
# Turn-based mouse input
|
||||
if not is_multiplayer_authority() or (main.turn_based_mode and (not is_my_turn or is_player_moving)):
|
||||
return
|
||||
|
||||
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
if is_bot == true or is_in_group("Bots"):
|
||||
set_process_unhandled_input(false)
|
||||
set_process_input(false)
|
||||
return
|
||||
var camera = get_viewport().get_camera_3d()
|
||||
var from = camera.project_ray_origin(event.position)
|
||||
var to = from + camera.project_ray_normal(event.position) * 1000
|
||||
|
||||
var click_position = raycast_to_grid(from, to)
|
||||
if click_position != Vector2i(-1, -1):
|
||||
handle_grid_click(click_position)
|
||||
if input_manager:
|
||||
input_manager.handle_unhandled_input(event)
|
||||
|
||||
func _on_slot_gui_input(event, slot_index, slot_ui) -> int:
|
||||
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
|
||||
if main.current_action_state == main.ActionState.ARRANGING:
|
||||
if selected_playerboard_slot == -1:
|
||||
select_playerboard_slot(slot_index)
|
||||
return slot_index
|
||||
else:
|
||||
if selected_playerboard_slot == slot_index:
|
||||
deselect_playerboard_slot()
|
||||
return slot_index
|
||||
elif can_move_to_target_playerboard_slot():
|
||||
target_playerboard_slot(slot_index)
|
||||
main.emit_signal("can_move_item", true)
|
||||
return slot_index
|
||||
else:
|
||||
return -1
|
||||
if input_manager:
|
||||
return input_manager.handle_slot_gui_input(event, slot_index, slot_ui)
|
||||
return -1
|
||||
|
||||
func handle_grid_click(grid_position: Vector2i):
|
||||
if is_bot == true or is_in_group("Bots"):
|
||||
return
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main:
|
||||
push_error("Main node not found")
|
||||
return
|
||||
|
||||
match main.current_action_state:
|
||||
main.ActionState.MOVING:
|
||||
if grid_position in highlighted_cells:
|
||||
move_player_to_clicked_position(grid_position)
|
||||
main.ActionState.GRABBING:
|
||||
if grid_position in highlighted_cells or grid_position == current_position:
|
||||
grab_item(grid_position)
|
||||
#main.ActionState.PUTTING:
|
||||
#if grid_position in highlighted_cells and selected_playerboard_slot != -1:
|
||||
#put_item(grid_position)
|
||||
main.ActionState.RANDOMIZING:
|
||||
if grid_position in highlighted_cells:
|
||||
main.randomize_item_at_position(grid_position)
|
||||
main.ActionState.PLACING_OBSTACLE:
|
||||
if grid_position in highlighted_cells:
|
||||
main.place_obstacle(grid_position)
|
||||
if input_manager:
|
||||
input_manager.handle_grid_click(grid_position)
|
||||
|
||||
# Modify is_position_occupied to check for selected spawn points
|
||||
func is_position_occupied(pos: Vector2i) -> bool:
|
||||
@@ -730,119 +514,17 @@ func raycast_to_grid(from: Vector3, to: Vector3) -> Vector2i:
|
||||
return Vector2i(-1, -1)
|
||||
|
||||
func is_within_movement_range(target_position: Vector2i) -> bool:
|
||||
var distance: int
|
||||
if use_diagonal_movement:
|
||||
distance = max(abs(target_position.x - current_position.x), abs(target_position.y - current_position.y))
|
||||
else:
|
||||
distance = abs(target_position.x - current_position.x) + abs(target_position.y - current_position.y)
|
||||
return distance <= movement_range
|
||||
return movement_manager.is_within_movement_range(target_position)
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Movement
|
||||
# -----------------------------------------------------------------
|
||||
|
||||
func simple_move_to(grid_position: Vector2i):
|
||||
if not is_multiplayer_authority() or is_player_moving:
|
||||
return
|
||||
|
||||
# Check if target is within 1-tile range
|
||||
var distance: int
|
||||
if use_diagonal_movement:
|
||||
distance = max(abs(grid_position.x - current_position.x), abs(grid_position.y - current_position.y))
|
||||
else:
|
||||
distance = abs(grid_position.x - current_position.x) + abs(grid_position.y - current_position.y)
|
||||
|
||||
if distance != 1:
|
||||
return # Only single-step moves allowed
|
||||
|
||||
# Check for finish line
|
||||
if grid_position in finish_locations:
|
||||
if not can_finish:
|
||||
can_finish = check_pattern_match()
|
||||
if not can_finish:
|
||||
return # Cannot move to finish line
|
||||
|
||||
var is_valid_finish = grid_position in finish_locations if current_lap == 0 else grid_position in spawn_locations
|
||||
|
||||
# Check walkability and obstacles
|
||||
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
|
||||
|
||||
if enhanced_gridmap.is_blocked_by_obstacle(current_position, grid_position, 3):
|
||||
return
|
||||
|
||||
# All checks passed, perform move
|
||||
rotate_towards_target(grid_position)
|
||||
|
||||
var path = [Vector2(current_position.x, current_position.y), Vector2(grid_position.x, grid_position.y)]
|
||||
path.pop_front()
|
||||
|
||||
# Use the existing RPC to move
|
||||
rpc("start_movement_along_path", path, not (is_bot or is_in_group("Bots")))
|
||||
|
||||
# Handle finish line crossing
|
||||
if is_valid_finish and can_finish:
|
||||
rpc("finish_race")
|
||||
movement_manager.simple_move_to(grid_position)
|
||||
|
||||
func move_player_to_clicked_position(grid_position: Vector2i):
|
||||
|
||||
if not is_multiplayer_authority() or is_player_moving or action_points <= 0:
|
||||
return
|
||||
|
||||
# Check if trying to move to finish line
|
||||
if grid_position in finish_locations:
|
||||
if not can_finish:
|
||||
can_finish = check_pattern_match()
|
||||
if not can_finish:
|
||||
return # Cannot move to finish line if pattern doesn't match
|
||||
|
||||
var is_valid_finish = false
|
||||
# Make scenario for match checking laps, for handle lap count
|
||||
if current_lap == 0: # first lap
|
||||
is_valid_finish = grid_position in finish_locations
|
||||
else: # second lap
|
||||
is_valid_finish = grid_position in spawn_locations
|
||||
|
||||
if not is_within_movement_range(grid_position):
|
||||
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
|
||||
|
||||
# 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)
|
||||
|
||||
# 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()
|
||||
|
||||
# Handle finish line crossing
|
||||
if is_valid_finish and can_finish:
|
||||
rpc("finish_race")
|
||||
movement_manager.move_to_clicked_position(grid_position)
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func start_movement_along_path(path: Array, clear_visual: bool = true):
|
||||
@@ -870,16 +552,16 @@ func start_movement_along_path(path: Array, clear_visual: bool = true):
|
||||
enhanced_gridmap.clear_path_visualization()
|
||||
|
||||
# Restore movement range highlights if it was the player's turn
|
||||
if main and main.current_action_state == main.ActionState.MOVING and is_my_turn:
|
||||
if main and main.ui_manager.current_action_state == main.ui_manager.ActionState.MOVING and is_my_turn:
|
||||
highlight_movement_range()
|
||||
|
||||
has_moved_this_turn = path.size() <= movement_range
|
||||
|
||||
if main:
|
||||
if not (is_bot or is_in_group("Bots")):
|
||||
main.set_action_state(main.ActionState.NONE)
|
||||
main.ui_manager.current_action_state = main.ui_manager.ActionState.NONE
|
||||
|
||||
if main and main.turn_based_mode:
|
||||
if TurnManager.turn_based_mode:
|
||||
end_turn()
|
||||
_after_action_completed()
|
||||
)
|
||||
@@ -944,19 +626,22 @@ func end_turn():
|
||||
get_tree().get_root().get_node_or_null("Main").request_next_turn()
|
||||
|
||||
func reset_race():
|
||||
current_lap = 0
|
||||
race_position = 0
|
||||
can_finish = false
|
||||
goals = first_lap_goals.duplicate()
|
||||
playerboard.fill(-1)
|
||||
if is_multiplayer_authority():
|
||||
rpc("sync_goals", goals)
|
||||
rpc("sync_playerboard", playerboard)
|
||||
if race_manager:
|
||||
race_manager.current_lap = 0
|
||||
race_manager.race_position = 0
|
||||
race_manager.can_finish = false
|
||||
race_manager.goals = race_manager.first_lap_goals.duplicate()
|
||||
race_manager.playerboard.fill(-1)
|
||||
if is_multiplayer_authority():
|
||||
rpc("sync_goals", race_manager.goals)
|
||||
rpc("sync_playerboard", race_manager.playerboard)
|
||||
|
||||
# Add a static reset for new games
|
||||
static func reset_race_stats():
|
||||
lap1_finishers = 0
|
||||
lap2_finishers = 0
|
||||
var race_mgr = load("res://scripts/managers/player_race_manager.gd")
|
||||
if race_mgr:
|
||||
race_mgr.lap1_finishers = 0
|
||||
race_mgr.lap2_finishers = 0
|
||||
|
||||
@rpc("any_peer", "call_local", "unreliable")
|
||||
func remote_set_position(authority_position):
|
||||
@@ -1445,12 +1130,12 @@ func handle_playerboard_slot_selected(slot_index: int):
|
||||
if not main:
|
||||
return
|
||||
|
||||
if main.current_action_state == main.ActionState.PUTTING:
|
||||
if main.ui_manager.current_action_state == main.ui_manager.ActionState.PUTTING:
|
||||
if playerboard[slot_index] != -1: # If slot has an item
|
||||
selected_playerboard_slot = slot_index
|
||||
clear_highlights()
|
||||
highlight_empty_adjacent_cells() # Highlight valid put locations
|
||||
elif main.current_action_state == main.ActionState.GRABBING:
|
||||
elif main.ui_manager.current_action_state == main.ui_manager.ActionState.GRABBING:
|
||||
if slot_index in highlighted_cells and playerboard[slot_index] == -1:
|
||||
var cell = Vector3i(selected_gridmap_position.x, 1, selected_gridmap_position.y)
|
||||
var item = enhanced_gridmap.get_cell_item(cell)
|
||||
@@ -1467,13 +1152,13 @@ func handle_playerboard_slot_selected(slot_index: int):
|
||||
clear_highlights()
|
||||
clear_playerboard_highlights()
|
||||
selected_gridmap_position = Vector2i(-1, -1)
|
||||
main.set_action_state(main.ActionState.NONE)
|
||||
main.ui_manager.current_action_state = main.ui_manager.ActionState.NONE
|
||||
_after_action_completed()
|
||||
|
||||
# We also need to add handle_put_slot_selected:
|
||||
func handle_put_slot_selected(slot_index: int):
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or main.current_action_state != main.ActionState.PUTTING:
|
||||
if not main or main.ui_manager.current_action_state != main.ui_manager.ActionState.PUTTING:
|
||||
return
|
||||
|
||||
print("PUT slot selected: ", slot_index, ", item: ", playerboard[slot_index])
|
||||
@@ -1506,28 +1191,28 @@ func arrange_playerboard_item(slot_index: int):
|
||||
var adjacent_slots = get_adjacent_playerboard_slots(slot_index)
|
||||
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or not main.playerboard_ui:
|
||||
if not main or not main.ui_manager.playerboard_ui:
|
||||
return
|
||||
|
||||
# Store the selected slot
|
||||
selected_playerboard_slot = slot_index
|
||||
|
||||
# Highlight selected slot
|
||||
var selected_slot_ui = main.playerboard_ui.get_child(slot_index)
|
||||
var selected_slot_ui = main.ui_manager.playerboard_ui.get_child(slot_index)
|
||||
if selected_slot_ui.get_child_count() > 1:
|
||||
selected_slot_ui.get_child(1).show()
|
||||
|
||||
# Highlight valid adjacent slots
|
||||
for adj_slot in adjacent_slots:
|
||||
if playerboard[adj_slot] == -1: # Only highlight empty adjacent slots
|
||||
var adj_slot_ui = main.playerboard_ui.get_child(adj_slot)
|
||||
var adj_slot_ui = main.ui_manager.playerboard_ui.get_child(adj_slot)
|
||||
if adj_slot_ui.get_child_count() > 2:
|
||||
adj_slot_ui.get_child(2).show()
|
||||
highlighted_cells.append(adj_slot)
|
||||
|
||||
# Connect to slot click signals
|
||||
for i in range(playerboard.size()):
|
||||
var slot = main.playerboard_ui.get_child(i)
|
||||
var slot = main.ui_manager.playerboard_ui.get_child(i)
|
||||
if not slot.gui_input.is_connected(_on_slot_clicked):
|
||||
slot.gui_input.connect(_on_slot_clicked.bind(i))
|
||||
|
||||
@@ -1536,7 +1221,7 @@ func _on_slot_clicked(event: InputEvent, slot_index: int):
|
||||
return
|
||||
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or main.current_action_state != main.ActionState.ARRANGING:
|
||||
if not main or main.ui_manager.current_action_state != main.ui_manager.ActionState.ARRANGING:
|
||||
return
|
||||
|
||||
if selected_playerboard_slot == -1 or slot_index == selected_playerboard_slot:
|
||||
@@ -1562,8 +1247,8 @@ func _on_slot_clicked(event: InputEvent, slot_index: int):
|
||||
selected_playerboard_slot = -1
|
||||
|
||||
# Update the visual representation
|
||||
main.update_playerboard_ui()
|
||||
main.set_action_state(main.ActionState.NONE)
|
||||
main.ui_manager.update_playerboard_ui()
|
||||
main.ui_manager.current_action_state = main.ui_manager.ActionState.NONE
|
||||
|
||||
func is_valid_arrangement_slot(from_slot: int, to_slot: int) -> bool:
|
||||
var from_row = from_slot / 5
|
||||
@@ -1727,152 +1412,16 @@ func highlight_cells_if_authorized(cells_to_highlight: Array):
|
||||
|
||||
# Update highlight_movement_range to respect the expanded obstacle blocking
|
||||
func highlight_movement_range():
|
||||
if not is_multiplayer_authority() or is_bot or is_in_group("Bots"):
|
||||
return
|
||||
|
||||
# Prevent recursive highlighting
|
||||
if _is_highlighting:
|
||||
return
|
||||
_is_highlighting = true
|
||||
|
||||
clear_highlights()
|
||||
var cells_to_highlight = []
|
||||
|
||||
# First, identify all cells that are blocked by obstacles
|
||||
var blocked_cells = []
|
||||
|
||||
# Check all cells for obstacles and get their blocked cells
|
||||
for x in range(enhanced_gridmap.columns):
|
||||
for z in range(enhanced_gridmap.rows):
|
||||
var cell_pos = Vector2i(x, z)
|
||||
var cell_pos3d = Vector3i(x, 3, z)
|
||||
|
||||
if enhanced_gridmap.has_obstacle_at(cell_pos3d):
|
||||
var orientation = enhanced_gridmap.get_obstacle_orientation(cell_pos3d)
|
||||
blocked_cells.append_array(enhanced_gridmap.get_cells_blocked_by_obstacle(cell_pos, orientation, 3))
|
||||
|
||||
# Now highlight all cells within movement range that aren't blocked
|
||||
for x in range(max(0, current_position.x - movement_range),
|
||||
min(enhanced_gridmap.columns, current_position.x + movement_range + 1)):
|
||||
for z in range(max(0, current_position.y - movement_range),
|
||||
min(enhanced_gridmap.rows, current_position.y + movement_range + 1)):
|
||||
var test_pos = Vector2i(x, z)
|
||||
|
||||
# Skip current position
|
||||
if test_pos == current_position:
|
||||
continue
|
||||
|
||||
# Check if within movement range
|
||||
if is_within_movement_range(test_pos):
|
||||
# Skip if blocked by obstacle
|
||||
if test_pos in blocked_cells:
|
||||
continue
|
||||
|
||||
# Check basic walkability
|
||||
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
|
||||
if cell_item == -1 or cell_item in enhanced_gridmap.non_walkable_items or is_position_occupied(test_pos):
|
||||
continue
|
||||
|
||||
# Check if there's a valid path to this cell
|
||||
if can_reach_cell(test_pos, blocked_cells):
|
||||
cells_to_highlight.append(test_pos)
|
||||
|
||||
# At the end of the function:
|
||||
highlight_cells_if_authorized(cells_to_highlight)
|
||||
_is_highlighting = false
|
||||
movement_manager.highlight_movement_range()
|
||||
|
||||
# Helper function to check if a cell can be reached given the blocked cells
|
||||
func can_reach_cell(target_pos: Vector2i, blocked_cells: Array) -> bool:
|
||||
# Simple BFS to find if there's a path
|
||||
var queue = [current_position]
|
||||
var visited = {current_position: true}
|
||||
var steps = {current_position: 0}
|
||||
|
||||
while not queue.is_empty():
|
||||
var current = queue.pop_front()
|
||||
|
||||
# If we've found the target, check if it's within movement range
|
||||
if current == target_pos:
|
||||
return steps[current] <= movement_range
|
||||
|
||||
# If we've used all movement, don't explore further
|
||||
if steps[current] >= movement_range:
|
||||
continue
|
||||
|
||||
# Try all adjacent cells
|
||||
var directions = [
|
||||
Vector2i(0, -1), # North
|
||||
Vector2i(1, 0), # East
|
||||
Vector2i(0, 1), # South
|
||||
Vector2i(-1, 0), # West
|
||||
]
|
||||
|
||||
# Add diagonal directions if enabled
|
||||
if enhanced_gridmap.diagonal_movement:
|
||||
directions.append(Vector2i(-1, -1)) # Northwest
|
||||
directions.append(Vector2i(1, -1)) # Northeast
|
||||
directions.append(Vector2i(-1, 1)) # Southwest
|
||||
directions.append(Vector2i(1, 1)) # Southeast
|
||||
|
||||
for dir in directions:
|
||||
var next_pos = current + dir
|
||||
|
||||
# Skip if already visited, blocked, or not valid
|
||||
if visited.has(next_pos) or next_pos in blocked_cells:
|
||||
continue
|
||||
|
||||
if not enhanced_gridmap.is_position_valid(next_pos) or not enhanced_gridmap.is_cell_walkable(next_pos, 0):
|
||||
continue
|
||||
|
||||
if is_position_occupied(next_pos) and next_pos != target_pos:
|
||||
continue
|
||||
|
||||
# Check if movement between cells is blocked by an obstacle
|
||||
if not is_diagonal_direction(dir) and enhanced_gridmap.is_movement_blocked(current, next_pos, 3):
|
||||
continue
|
||||
|
||||
# For diagonal movement, check if both orthogonal paths are blocked
|
||||
if is_diagonal_direction(dir):
|
||||
var mid1 = Vector2i(next_pos.x, current.y)
|
||||
var mid2 = Vector2i(current.x, next_pos.y)
|
||||
|
||||
var path1_blocked = mid1 in blocked_cells or enhanced_gridmap.is_movement_blocked(current, mid1, 3)
|
||||
var path2_blocked = mid2 in blocked_cells or enhanced_gridmap.is_movement_blocked(current, mid2, 3)
|
||||
|
||||
if path1_blocked and path2_blocked:
|
||||
continue
|
||||
|
||||
# Add to queue
|
||||
queue.append(next_pos)
|
||||
visited[next_pos] = true
|
||||
steps[next_pos] = steps[current] + 1
|
||||
|
||||
return false
|
||||
|
||||
|
||||
# Helper function to check if a direction is diagonal
|
||||
func is_diagonal_direction(direction: Vector2i) -> bool:
|
||||
return direction.x != 0 and direction.y != 0
|
||||
|
||||
|
||||
func highlight_adjacent_cells():
|
||||
if not is_multiplayer_authority() or is_bot or is_in_group("Bots"):
|
||||
return
|
||||
|
||||
var cells_to_highlight = []
|
||||
|
||||
# Add current position if item exists
|
||||
var current_cell = Vector3i(current_position.x, 1, current_position.y)
|
||||
if enhanced_gridmap.get_cell_item(current_cell) != -1:
|
||||
cells_to_highlight.append(current_position)
|
||||
|
||||
# Add valid neighbors
|
||||
var neighbors = enhanced_gridmap.get_neighbors(current_position, 0)
|
||||
for neighbor in neighbors:
|
||||
if neighbor.is_walkable:
|
||||
var cell_pos = neighbor.position
|
||||
if enhanced_gridmap.get_cell_item(Vector3i(cell_pos.x, 1, cell_pos.y)) != -1:
|
||||
cells_to_highlight.append(cell_pos)
|
||||
|
||||
highlight_cells_if_authorized(cells_to_highlight)
|
||||
movement_manager.highlight_adjacent_cells()
|
||||
|
||||
func highlight_empty_adjacent_cells():
|
||||
if is_bot == true or is_in_group("Bots"):
|
||||
@@ -1938,25 +1487,25 @@ func highlight_occupied_playerboard_slots():
|
||||
return
|
||||
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or not main.playerboard_ui:
|
||||
if not main or not main.ui_manager.playerboard_ui:
|
||||
return
|
||||
|
||||
# First reset all slots to normal
|
||||
for i in range(playerboard.size()):
|
||||
var slot = main.playerboard_ui.get_child(i)
|
||||
var slot = main.ui_manager.playerboard_ui.get_child(i)
|
||||
for child in slot.get_children():
|
||||
child.hide()
|
||||
|
||||
# Highlight occupied slots that match goals
|
||||
for i in range(playerboard.size()):
|
||||
if playerboard[i] in goals:
|
||||
var slot = main.playerboard_ui.get_child(i)
|
||||
var slot = main.ui_manager.playerboard_ui.get_child(i)
|
||||
if slot.get_child_count() > 0:
|
||||
slot.get_child(0).show() # Show highlight for matching items
|
||||
highlighted_cells.append(i) # Add to highlighted cells for tracking
|
||||
|
||||
# Update the UI to reflect changes
|
||||
main.update_playerboard_ui()
|
||||
main.ui_manager.update_playerboard_ui()
|
||||
|
||||
func clear_highlights():
|
||||
# Never allow bots to clear highlights for human players
|
||||
@@ -1968,7 +1517,7 @@ func clear_highlights():
|
||||
|
||||
# Store the current action state before clearing
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
var current_state = main.current_action_state if main else null
|
||||
var current_state = main.ui_manager.current_action_state if main else null
|
||||
|
||||
for cell in highlighted_cells:
|
||||
if cell is Vector2i:
|
||||
@@ -1976,14 +1525,14 @@ func clear_highlights():
|
||||
|
||||
highlighted_cells.clear()
|
||||
|
||||
if main and main.playerboard_ui:
|
||||
for i in range(main.playerboard_ui.get_child_count()):
|
||||
var slot = main.playerboard_ui.get_child(i)
|
||||
if main and main.ui_manager.playerboard_ui:
|
||||
for i in range(main.ui_manager.playerboard_ui.get_child_count()):
|
||||
var slot = main.ui_manager.playerboard_ui.get_child(i)
|
||||
for child in slot.get_children():
|
||||
child.hide()
|
||||
|
||||
# Restore highlights based on current action state
|
||||
if main and current_state == main.ActionState.MOVING and is_my_turn and current_state != main.ActionState.PLACING_OBSTACLE:
|
||||
if main and current_state == main.ui_manager.ActionState.MOVING and is_my_turn and current_state != main.ui_manager.ActionState.PLACING_OBSTACLE:
|
||||
highlight_movement_range()
|
||||
|
||||
func clear_playerboard_highlights():
|
||||
@@ -1995,9 +1544,9 @@ func clear_playerboard_highlights():
|
||||
return
|
||||
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if main and main.playerboard_ui:
|
||||
for i in range(main.playerboard_ui.get_child_count()):
|
||||
var slot = main.playerboard_ui.get_child(i)
|
||||
if main and main.ui_manager.playerboard_ui:
|
||||
for i in range(main.ui_manager.playerboard_ui.get_child_count()):
|
||||
var slot = main.ui_manager.playerboard_ui.get_child(i)
|
||||
if slot.get_child_count() > 0: slot.get_child(0).hide()
|
||||
if slot.get_child_count() > 1: slot.get_child(1).hide()
|
||||
if slot.get_child_count() > 2: slot.get_child(2).hide()
|
||||
@@ -2005,14 +1554,7 @@ func clear_playerboard_highlights():
|
||||
highlighted_cells.clear()
|
||||
|
||||
func rotate_towards_target(target_pos: Vector2i):
|
||||
var direction = Vector2(target_pos.x - current_position.x, target_pos.y - current_position.y).normalized()
|
||||
target_rotation = atan2(direction.x, direction.y)
|
||||
|
||||
if is_multiplayer_authority():
|
||||
rpc("sync_rotation", target_rotation)
|
||||
|
||||
var tween = create_tween()
|
||||
tween.tween_property(self, "rotation:y", target_rotation, 0.2)
|
||||
movement_manager.rotate_towards_target(target_pos)
|
||||
|
||||
# We also need to add these supporting functions:
|
||||
func select_playerboard_slot(slot_index: int):
|
||||
@@ -2105,6 +1647,11 @@ func sync_grid_item(x: int, y: int, z: int, item: int):
|
||||
func sync_goals(new_goals: Array):
|
||||
goals = new_goals.duplicate() # Make sure to duplicate the array
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func sync_second_lap_goals(new_goals: Array):
|
||||
if race_manager:
|
||||
race_manager.second_lap_goals = new_goals.duplicate()
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func sync_playerboard(new_playerboard: Array):
|
||||
playerboard = new_playerboard.duplicate()
|
||||
@@ -2129,13 +1676,13 @@ func _after_action_completed():
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if main:
|
||||
# Add this condition for bots
|
||||
if not main.turn_based_mode and (action_points <= 0 or is_bot):
|
||||
if not TurnManager.turn_based_mode and (action_points <= 0 or is_bot):
|
||||
action_points = 20 # For bots in non-turn-based mode, this will keep refreshing
|
||||
has_performed_action = false
|
||||
has_moved_this_turn = false
|
||||
|
||||
main.update_button_states()
|
||||
main.update_playerboard_ui()
|
||||
main.ui_manager.update_button_states()
|
||||
main.ui_manager.update_playerboard_ui()
|
||||
|
||||
# Add this line to sync all boards
|
||||
main.update_all_players_boards()
|
||||
@@ -2158,14 +1705,14 @@ func consume_action_points(points: int):
|
||||
return
|
||||
|
||||
# Don't consume points for bots in non-turn-based mode
|
||||
if is_bot == true and not main.turn_based_mode:
|
||||
if is_bot == true and not TurnManager.turn_based_mode:
|
||||
_after_action_completed()
|
||||
return
|
||||
|
||||
action_points -= points
|
||||
|
||||
if action_points <= 0:
|
||||
if main.turn_based_mode:
|
||||
if TurnManager.turn_based_mode:
|
||||
main.request_next_turn()
|
||||
else:
|
||||
action_points = 2
|
||||
|
||||
Reference in New Issue
Block a user