extends CharacterBody2D

@export var Speed = 2500
@export var FrictionMult = 5

@onready var Sprite : Sprite2D = $Sprite2D
var SpriteTurningSpeed = 5

var ActiveContract : Node2D

var Money : int = 0

@onready var PackageSprite = $PackageSprite

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
	
	var direction = Vector2(0,0)
	
	if Input.is_action_pressed("up"):
		direction.y -= 1
	if Input.is_action_pressed("down"):
		direction.y += 1
	if Input.is_action_pressed("left"):
		direction.x -= 1
	if Input.is_action_pressed("right"):
		direction.x += 1
	
	direction = direction.normalized()
	
	velocity += direction * Speed * delta
	
	velocity = velocity.move_toward(Vector2(0,0), delta * clamp(velocity.length() * FrictionMult, 1000, 10000))
	
	if velocity.length() > 0.1:
		Sprite.rotation = rotate_toward(Sprite.rotation,velocity.angle(),delta*SpriteTurningSpeed)
	
	if ActiveContract:
		PackageSprite.show()
	else:
		PackageSprite.hide()
	
	move_and_slide()
