127 lines
4.7 KiB
GDScript
127 lines
4.7 KiB
GDScript
extends Node
|
|
|
|
var player: Node3D
|
|
var movement_manager: Node
|
|
var race_manager: Node
|
|
|
|
func initialize(p_player: Node3D, p_movement_manager: Node, p_race_manager: Node):
|
|
player = p_player
|
|
movement_manager = p_movement_manager
|
|
race_manager = p_race_manager
|
|
|
|
func handle_unhandled_input(event):
|
|
# Early return if not authorized human player
|
|
if not player.is_multiplayer_authority() or player.is_bot or player.is_in_group("Bots"):
|
|
player.set_process_unhandled_input(false)
|
|
return
|
|
|
|
var main = player.get_node("/root/Main")
|
|
if not main:
|
|
return
|
|
|
|
# --- Real-time Keyboard/Touch Input ---
|
|
if not TurnManager.turn_based_mode and not movement_manager.is_moving:
|
|
var target_position = player.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"):
|
|
player.grab_item(player.current_position)
|
|
elif Input.is_action_just_pressed("action_put"):
|
|
player.auto_put_item()
|
|
else:
|
|
input_handled = false
|
|
|
|
if target_position != player.current_position:
|
|
movement_manager.simple_move_to(target_position)
|
|
|
|
if input_handled:
|
|
player.get_viewport().set_input_as_handled()
|
|
return
|
|
# --- End Real-time Input ---
|
|
|
|
# Handle spawn point selection if not yet selected
|
|
if not player.spawn_point_selected and player.highlighted_spawn_points.size() > 0:
|
|
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
var camera = player.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 = player.raycast_to_grid(from, to)
|
|
if click_position in player.highlighted_spawn_points:
|
|
if player.select_spawn_point(click_position):
|
|
return
|
|
|
|
# Turn-based mouse input
|
|
if not player.is_multiplayer_authority() or (TurnManager.turn_based_mode and (not player.is_my_turn or movement_manager.is_moving)):
|
|
return
|
|
|
|
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
if player.is_bot == true or player.is_in_group("Bots"):
|
|
player.set_process_unhandled_input(false)
|
|
player.set_process_input(false)
|
|
return
|
|
var camera = player.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 = player.raycast_to_grid(from, to)
|
|
if click_position != Vector2i(-1, -1):
|
|
handle_grid_click(click_position)
|
|
|
|
func handle_grid_click(grid_position: Vector2i):
|
|
if player.is_bot == true or player.is_in_group("Bots"):
|
|
return
|
|
var main = player.get_node("/root/Main")
|
|
if not main:
|
|
return
|
|
|
|
match main.ui_manager.current_action_state:
|
|
main.ui_manager.ActionState.MOVING:
|
|
if grid_position in player.highlighted_cells:
|
|
movement_manager.move_to_clicked_position(grid_position)
|
|
main.ui_manager.ActionState.GRABBING:
|
|
if grid_position in player.highlighted_cells or grid_position == player.current_position:
|
|
player.grab_item(grid_position)
|
|
main.ui_manager.ActionState.RANDOMIZING:
|
|
if grid_position in player.highlighted_cells:
|
|
main.randomize_item_at_position(grid_position)
|
|
main.ui_manager.ActionState.PLACING_OBSTACLE:
|
|
if grid_position in player.highlighted_cells:
|
|
main.place_obstacle(grid_position)
|
|
|
|
func handle_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 = player.get_tree().get_root().get_node_or_null("Main")
|
|
|
|
if main.ui_manager.current_action_state == main.ui_manager.ActionState.ARRANGING:
|
|
if player.selected_playerboard_slot == -1:
|
|
player.select_playerboard_slot(slot_index)
|
|
return slot_index
|
|
else:
|
|
if player.selected_playerboard_slot == slot_index:
|
|
player.deselect_playerboard_slot()
|
|
return slot_index
|
|
elif player.can_move_to_target_playerboard_slot():
|
|
player.target_playerboard_slot(slot_index)
|
|
main.emit_signal("can_move_item", true)
|
|
return slot_index
|
|
else:
|
|
return -1
|
|
return -1
|