@@ -10,6 +10,11 @@ const GROUP: = "_PLAYER_CONTROLLER_GROUP"
1010# It is reset on cancelling the move path or continuing movement via arrows/gamepad directions.
1111var _target : Interaction = null
1212
13+ # Also keep track of the most recently pressed move key (e.g. WASD keys). This makes keyboard input
14+ # feel more intuitive, since the gamepiece will move towards the most recently pressed key rather
15+ # than prefering an arbitrary axis.
16+ var _last_input_direction : = Vector2 .ZERO
17+
1318
1419func _ready () -> void :
1520 super ._ready ()
@@ -57,6 +62,51 @@ func _ready() -> void:
5762func _unhandled_input (event : InputEvent ) -> void :
5863 if event .is_action_released ("select" ):
5964 stop_moving ()
65+
66+ elif event is InputEventKey :
67+ if event .is_action_pressed ("ui_up" ):
68+ _last_input_direction = Vector2 .UP
69+ if _gamepiece .is_moving (): stop_moving ()
70+ else : move_to_pressed_key (Vector2 .UP )
71+
72+ elif event .is_action_pressed ("ui_down" ):
73+ _last_input_direction = Vector2 .DOWN
74+ if _gamepiece .is_moving (): stop_moving ()
75+ else : move_to_pressed_key (Vector2 .DOWN )
76+
77+ elif event .is_action_pressed ("ui_left" ):
78+ _last_input_direction = Vector2 .LEFT
79+ if _gamepiece .is_moving (): stop_moving ()
80+ else : move_to_pressed_key (Vector2 .LEFT )
81+
82+ elif event .is_action_pressed ("ui_right" ):
83+ _last_input_direction = Vector2 .RIGHT
84+ if _gamepiece .is_moving (): stop_moving ()
85+ else : move_to_pressed_key (Vector2 .RIGHT )
86+
87+ # print(Gameboard.pathfinder.get_path_to_cell())
88+ # If path is valid, move.
89+
90+
91+ func move_to_pressed_key (input_direction : Vector2 ) -> void :
92+ if is_active :
93+ var source_cell : = GamepieceRegistry .get_cell (_gamepiece )
94+ var target_cell : = Vector2i .ZERO
95+
96+ # Unless using 8-direction movement, one movement axis must be preferred.
97+ # Default to the x-axis.
98+ target_cell = source_cell + Vector2i (input_direction )
99+
100+ # Try to get a path to destination (will fail if cell is occupied)
101+ var new_move_path : = Gameboard .pathfinder .get_path_to_cell (source_cell , target_cell )
102+
103+ # Path is invalid. Bump animation?
104+ if new_move_path .size () < 1 :
105+ _gamepiece .direction = Directions .angle_to_direction (input_direction .angle ())
106+
107+ else :
108+ move_path = new_move_path .duplicate ()
109+ FieldEvents .player_path_set .emit (_gamepiece , new_move_path .back ())
60110
61111
62112func stop_moving () -> void :
@@ -136,6 +186,16 @@ func _on_interaction_selected(interaction: Interaction) -> void:
136186 stop_moving ()
137187
138188
189+ func _on_gamepiece_arriving (excess_distance : float ) -> void :
190+ super ._on_gamepiece_arriving (excess_distance )
191+
192+ # It may be that the player is holding the keys down. In that case, continue moving the
193+ # gamepiece towards the pressed direction.
194+ var input_direction : = Input .get_vector ("ui_left" , "ui_right" , "ui_up" , "ui_down" )
195+ if not input_direction .is_equal_approx (Vector2 .ZERO ):
196+ move_to_pressed_key (_last_input_direction )
197+
198+
139199func _on_gamepiece_arrived () -> void :
140200 super ._on_gamepiece_arrived ()
141201 if _target :
@@ -144,3 +204,10 @@ func _on_gamepiece_arrived() -> void:
144204 = Directions .vector_to_direction (_target .position - _gamepiece .position )
145205 _target .run ()
146206 _target = null
207+
208+ # No target, but check to see if the player is holding a key down and face in the direction of
209+ # the last pressed key.
210+ else :
211+ var input_direction : = Input .get_vector ("ui_left" , "ui_right" , "ui_up" , "ui_down" )
212+ if not input_direction .is_equal_approx (Vector2 .ZERO ):
213+ _gamepiece .direction = Directions .vector_to_direction (_last_input_direction )
0 commit comments