44 lines
1.4 KiB
GDScript3
44 lines
1.4 KiB
GDScript3
|
extends CharacterBody2D
|
||
|
|
||
|
@export var speed = 20
|
||
|
@export_range(0, 1) var damping_factor = 0.6
|
||
|
@export var animation_player : AnimationPlayer
|
||
|
|
||
|
func _physics_process(delta : float):
|
||
|
update_player_movement(delta)
|
||
|
pass
|
||
|
|
||
|
func get_move_input_vector() -> Vector2:
|
||
|
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized()
|
||
|
return input_direction
|
||
|
|
||
|
func update_player_movement(delta : float):
|
||
|
var input = get_move_input_vector()
|
||
|
|
||
|
# Damp Movement if not moving, Accelerate if Moving
|
||
|
var is_moving = input != Vector2.ZERO
|
||
|
self.velocity = input * speed * delta * 60 if is_moving else self.velocity * (1 - min(1, damping_factor * 60 * delta))
|
||
|
|
||
|
update_player_animation()
|
||
|
|
||
|
# Update Objects Physics calculations
|
||
|
self.move_and_slide()
|
||
|
pass
|
||
|
|
||
|
func update_player_animation():
|
||
|
var move_vector = get_move_input_vector()
|
||
|
match move_vector:
|
||
|
Vector2.ZERO: animation_player.play("Idle")
|
||
|
Vector2.LEFT: animation_player.play("MoveLeft")
|
||
|
Vector2.RIGHT: animation_player.play("MoveRight")
|
||
|
Vector2.UP: animation_player.play("MoveUp")
|
||
|
Vector2.DOWN: animation_player.play("MoveDown")
|
||
|
_: handle_diagonal_animations(move_vector)
|
||
|
pass
|
||
|
|
||
|
func handle_diagonal_animations(dir : Vector2):
|
||
|
if (dir.y > 0 and dir.x < 0) or (dir.y < 0 and dir.x < 0): animation_player.play("MoveLeft")
|
||
|
elif (dir.y > 0 and dir.x > 0) or (dir.y < 0 and dir.x > 0): animation_player.play("MoveRight")
|
||
|
else: animation_player.play("Idle")
|
||
|
pass
|