Cleanup of Code

This commit is contained in:
Snoweuph 2023-04-07 19:52:50 +02:00
parent 080401b9a3
commit e833b82dd4

View file

@ -1,46 +1,32 @@
extends CharacterBody2D extends CharacterBody2D
# Serialized Variables
@export var speed = 20 @export var speed = 20
@export_range(0, 1) var dampning_function = 0.6 @export_range(0, 1) var damping_factor = 0.6
@export var animation_player : AnimationPlayer @export var animation_player : AnimationPlayer
# Engine Callbacks
func _physics_process(delta : float): func _physics_process(delta : float):
move_player(delta) update_player_movement(delta)
pass pass
func get_move_input_vector() -> Vector2:
# Getting player input into a vector
func get_move_vector():
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized() var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized()
return input_direction return input_direction
func update_player_movement(delta : float):
var input = get_move_input_vector()
# Calculates the players velocity based on input and moves it
func move_player(delta : float):
# Damp Players Velocity if no Input
if get_move_vector() == Vector2.ZERO:
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 # Damp Movement if not moving, Accelerate if Moving
else: var is_moving = input != Vector2.ZERO
self.velocity = get_move_vector().normalized() * speed * delta * 60 self.velocity = input * speed * delta * 60 if is_moving else self.velocity * (1 - min(1, damping_factor * 60 * delta))
# Animate the player update_player_animation()
animate_player_movement()
# Finally, update the Player's physics calculations # Update Objects Physics calculations
self.move_and_slide() self.move_and_slide()
pass pass
func update_player_animation():
var move_vector = get_move_input_vector()
func animate_player_movement():
var move_vector = get_move_vector()
match move_vector: match move_vector:
Vector2.ZERO: animation_player.play("Idle") Vector2.ZERO: animation_player.play("Idle")
Vector2.LEFT: animation_player.play("MoveLeft") Vector2.LEFT: animation_player.play("MoveLeft")
@ -48,3 +34,4 @@ func animate_player_movement():
Vector2.UP: animation_player.play("MoveUp") Vector2.UP: animation_player.play("MoveUp")
Vector2.DOWN: animation_player.play("MoveDown") Vector2.DOWN: animation_player.play("MoveDown")
_: animation_player.play("Idle") _: animation_player.play("Idle")
pass