extends KinematicBody2D

var screen_size
export var speed = 300
const normal_speed = 300
const sprint_speed = 600

func _ready():
	screen_size = get_viewport_rect().size

func _process(delta):
	var velocity = Vector2.ZERO
	var animation_to_play
	if Input.is_action_pressed("move_down"):
		animation_to_play = "walk_down"
		velocity.y += 1
	if Input.is_action_pressed("move_up"):
		animation_to_play = "walk_up"
		velocity.y -= 1
	if Input.is_action_pressed("move_left"):
		animation_to_play = "walk_left"
		velocity.x -= 1
	if Input.is_action_pressed("move_right"):
		animation_to_play = "walk_right"
		velocity.x += 1
	if Input.is_action_pressed('sprint'):
		speed = sprint_speed
		$AnimatedSprite.speed_scale = 4
	else:
		speed = normal_speed
		$AnimatedSprite.speed_scale = 2

	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		$AnimatedSprite.play(animation_to_play)
	else:
		$AnimatedSprite.stop()
		
	position += velocity * delta
	position.x = clamp(position.x,8,screen_size.x-8)
	position.y = clamp(position.y,16,screen_size.y-16)
