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

51 lines
1.4 KiB
GDScript3
Raw Normal View History

2023-04-07 14:11:07 +00:00
extends CharacterBody2D
2023-04-07 17:26:18 +00:00
# Serialized Variables
@export var speed = 20
@export_range(0, 1) var dampning_function = 0.6
@export var animation_player : AnimationPlayer
2023-04-07 14:11:07 +00:00
2023-04-07 17:26:18 +00:00
# Engine Callbacks
func _physics_process(delta : float):
move_player(delta)
pass
# Getting player input into a vector
2023-04-07 14:11:07 +00:00
func get_move_vector():
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized()
return input_direction
2023-04-07 17:26:18 +00:00
# Calculates the players velocity based on input and moves it
2023-04-07 14:11:07 +00:00
func move_player(delta : float):
2023-04-07 17:26:18 +00:00
# Damp Players Velocity if no Input
2023-04-07 14:11:07 +00:00
if get_move_vector() == Vector2.ZERO:
2023-04-07 17:26:18 +00:00
self.velocity = self.velocity * (1 - min(1, dampning_function * 60 * delta))
# Set Player's velocity to the Input direction with a magnitude of Speed synced with frame rate
2023-04-07 14:11:07 +00:00
else:
2023-04-07 17:26:18 +00:00
self.velocity = get_move_vector().normalized() * speed * delta * 60
# Animate the player
animate_player_movement()
# Finally, update the Player's physics calculations
2023-04-07 14:11:07 +00:00
self.move_and_slide()
pass
2023-04-07 17:26:18 +00:00
func animate_player_movement():
var move_vector = get_move_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")
_: animation_player.play("Idle")