43 lines
1.1 KiB
GDScript
43 lines
1.1 KiB
GDScript
extends CharacterBody2D
|
|
|
|
const MAX_SPEED: float = 150
|
|
|
|
var was_moving: bool
|
|
var is_moving: bool
|
|
var current_dir: String
|
|
|
|
func _ready() -> void:
|
|
was_moving = true
|
|
is_moving = false
|
|
current_dir = 'down'
|
|
|
|
func _process(delta: float) -> void:
|
|
if not is_moving and was_moving:
|
|
$AnimatedSprite2D.play('down')
|
|
$AnimatedSprite2D.frame = 1
|
|
$AnimatedSprite2D.pause()
|
|
|
|
if is_moving:
|
|
$AnimatedSprite2D.play(current_dir)
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
var movement_vector: Vector2 = get_movement_vector()
|
|
var direction: Vector2 = movement_vector.normalized()
|
|
velocity = direction * MAX_SPEED
|
|
|
|
is_moving = velocity.length() > 10
|
|
|
|
if is_moving:
|
|
if abs(velocity.x) > abs(velocity.y):
|
|
current_dir = 'right' if velocity.x > 0 else 'left'
|
|
else:
|
|
current_dir = 'down' if velocity.y > 0 else 'up'
|
|
|
|
was_moving = true
|
|
|
|
move_and_slide()
|
|
|
|
func get_movement_vector() -> Vector2:
|
|
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
|
|
var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up')
|
|
return Vector2(x_mov, y_mov)
|