mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-06 14:10:55 +03:00
This leads to code that is easier to understand and runs faster thanks to GDScript's typed instructions. The untyped declaration warning is now enabled on all projects where type hints were added. All projects currently run without any untyped declration warnings. Dodge the Creeps and Squash the Creeps demos intentionally don't use type hints to match the documentation, where type hints haven't been adopted yet (given its beginner focus).
52 lines
1.2 KiB
GDScript
52 lines
1.2 KiB
GDScript
extends Pawn
|
|
|
|
var lost = false
|
|
@onready var Grid = get_parent()
|
|
|
|
|
|
func _ready():
|
|
update_look_direction(Vector2.RIGHT)
|
|
|
|
|
|
func _process(delta):
|
|
var input_direction = get_input_direction()
|
|
if not input_direction:
|
|
return
|
|
update_look_direction(input_direction)
|
|
|
|
var target_position = Grid.request_move(self, input_direction)
|
|
if target_position:
|
|
move_to(target_position)
|
|
else:
|
|
bump()
|
|
|
|
|
|
func get_input_direction():
|
|
return Vector2(
|
|
Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"),
|
|
Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
|
|
)
|
|
|
|
|
|
func update_look_direction(direction):
|
|
$Pivot/Sprite2D.rotation = direction.angle()
|
|
|
|
|
|
func move_to(target_position):
|
|
set_process(false)
|
|
$AnimationPlayer.play("walk")
|
|
var move_direction = (position - target_position).normalized()
|
|
var tween := create_tween()
|
|
tween.set_ease(Tween.EASE_IN)
|
|
tween.tween_property($Pivot, "position", $Pivot.position + move_direction * 32, $AnimationPlayer.current_animation_length)
|
|
$Pivot/Sprite2D.position = position - target_position
|
|
position = target_position
|
|
|
|
await $AnimationPlayer.animation_finished
|
|
|
|
set_process(true)
|
|
|
|
|
|
func bump():
|
|
$AnimationPlayer.play("bump")
|