Skip to content

Commit 60f8e83

Browse files
food-pleaseNathanLovato
authored andcommitted
Add directional movement (e.g. WASD) for PlayerController
1 parent a7010d0 commit 60f8e83

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Main2.tscn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ gameboard_properties = ExtResource("7_7v0vs")
7777
[node name="Gamepiece" parent="Gamepieces" instance=ExtResource("2_nevdf")]
7878
position = Vector2(87.6, 55.8)
7979
animation_scene = ExtResource("3_08ad2")
80-
move_speed = 640.0
8180

8281
[node name="Camera2D" type="Camera2D" parent="Gamepieces/Gamepiece"]
8382
metadata/_edit_lock_ = true

src/field/gamepieces/player_controller.gd

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1111
var _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

1419
func _ready() -> void:
1520
super._ready()
@@ -57,6 +62,51 @@ func _ready() -> void:
5762
func _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

62112
func 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+
139199
func _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

Comments
 (0)