This repository has been archived on 2024-07-02. You can view files and clone it, but cannot push or open issues or pull requests.
HoppyEaster/Scripts/PlayerController.gd

45 lines
1.7 KiB
GDScript3
Raw Normal View History

2023-04-07 14:11:07 +00:00
extends CharacterBody2D
2023-04-09 14:39:45 +00:00
@export var speed = 200
2023-04-07 17:52:50 +00:00
@export_range(0, 1) var damping_factor = 0.6
2023-04-07 17:26:18 +00:00
@export var animation_player : AnimationPlayer
2023-04-07 14:11:07 +00:00
2023-04-07 17:26:18 +00:00
func _physics_process(delta : float):
2023-04-07 17:52:50 +00:00
update_player_movement(delta)
2023-04-07 17:26:18 +00:00
pass
2023-04-07 17:52:50 +00:00
func get_move_input_vector() -> Vector2:
2023-04-07 14:11:07 +00:00
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized()
2023-04-10 20:18:16 +00:00
var input_direction_controller = Input.get_vector("move_left_controller", "move_right_controller", "move_up_controller", "move_down_controller")
return (input_direction if not input_direction_controller.length() > input_direction.length() else input_direction_controller.normalized())
2023-04-07 14:11:07 +00:00
2023-04-07 17:52:50 +00:00
func update_player_movement(delta : float):
var input = get_move_input_vector()
2023-04-07 17:26:18 +00:00
2023-04-07 17:52:50 +00:00
# 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))
2023-04-07 17:26:18 +00:00
2023-04-07 17:52:50 +00:00
update_player_animation()
2023-04-07 17:26:18 +00:00
2023-04-07 17:52:50 +00:00
# Update Objects Physics calculations
2023-04-07 14:11:07 +00:00
self.move_and_slide()
pass
2023-04-07 17:52:50 +00:00
func update_player_animation():
var move_vector = get_move_input_vector()
2023-04-07 17:26:18 +00:00
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")
2023-04-07 19:32:56 +00:00
_: 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")
2023-04-07 17:52:50 +00:00
pass