Files
godot-demo-projects/2d/role_playing_game/grid_movement/pawns/player.gd
David Briscoe aecf661c9c Fix and improve Role Playing Game: Gamepad, TileMapLayer, upgrade for 4.4.1 (#1248)
* Improve gamepad behaviour in rpg

Fix gamepad can't do dialogue.
Fix character often plays bump animation with gamepad when there's no
obstacles around.

Add gamepad A to ui_accept so gamepads can press buttons in dialogue and
combat.

Use get_vector so we get an automatic deadzone on our input.

Round our input direction so we don't pass Vector2i(0,0) to
request_move, get denied, and then do a bump animation.

* Use SpriteFrames instead of animating texture

Seems like Godot 4 doesn't allow toggling AnimationPlayer tracks by
making children editable so the texture is no longer being set and the
player is invisible. Regardless, this workflow is also not scalable to
multiple enemy types. Instead, use SpriteFrames to setup different
visuals for each slime.

Triggers animations from code alongside AnimationPlayer calls because
doing it in AnimationPlayer seemed unnecessarily complex.

* Give all slimes a common base class

Split player input handling from walker to allow both slimes to use the
same visual setup. In a real game, you'd want this kind of setup so
enemies are able to walk on the grid too.

Remove unused actor.gd -- looks like this was an early version of
walker.gd.

Setup opponent slime as type=Actor because otherwise you can't fight
them.

* Convert TileMap to TileMapLayer

Used "Extract TileMap layers as individual TileMapLayer nodes" in the
TileMap editor and removed the 0 layer argument from functions in
grid.gd

* Explain and rename facing direction node

When I first started looking into the character, I couldn't figure out
what this play button texture was for. After experimenting, it might be
an old version of the character visuals or a demo to show how to make
your character rotate.

* Fade to black between scenes

Fix blue screen when entering combat.

Our fade ColorRect was hidden so it never displayed. We removed the
exploration scene before fading so the fade started with an empty scene
with a blue skybox.

* Loop idle animation in combat

Add AnimationTree to combat character setup so we can tell when the
take_damage animation completes. This was harder than expected because
by default Godot modulates to black and sets scale to 0 unless you
specify otherwise with the RESET animation.

* Add a CanvasLayer above each clickable Control root

Fix mouse can't click buttons.

Seems like we're not able to click buttons in Godot 4 without a
CanvasLayer root. Adding that makes it work, but required some
restructuring to point at the correct node. In cases like combat+ui,
change it to emit a signal to use the "call down, signal up" pattern.

* Remove error comment that no longer occurs

Neither winning nor fleeing triggers this warning on Godot
v4.4.1.stable.official [49a5bc7b6].

* Rename parent -> grid

Make it clearer that the parent of the player and other pawns is
expected to be our Grid class.
2025-09-16 15:42:25 +02:00

22 lines
533 B
GDScript

extends Walker
func _process(_delta: float) -> void:
var input_direction := get_input_direction()
# We only move in integer increments.
input_direction = input_direction.round()
if input_direction.is_zero_approx():
return
update_look_direction(input_direction)
var target_position: Vector2 = grid.request_move(self, input_direction)
if target_position:
move_to(target_position)
elif active:
bump()
func get_input_direction() -> Vector2:
return Input.get_vector("move_left", "move_right", "move_up", "move_down")