extends CharacterBody2D

var speed = 16*16; # godot transformation per second
# Called when the node enters the scene tree for the first time.
var texts = [];
var lastChange = 0;
var hit = 0;


func _ready() -> void:
	RenderingServer.set_default_clear_color(Color.BLACK)
	var root = get_node("../")
	var texturespace = root.get_meta("resourcepack");
	for text in ["player-w1","player-w2"]:
		texts.append(load("res://"+texturespace+"/text/"+text+".png"))




# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
	var movedelta = Vector2.ZERO
	if not self.get_meta("can_walk"):
		return
	
	if Input.is_action_pressed("movedown"):
		movedelta.y += 1
	if Input.is_action_pressed("moveup"):
		movedelta.y -= 1
	if Input.is_action_pressed("moveleft"):
		movedelta.x -= 1
	if Input.is_action_pressed("moveright"):
		movedelta.x += 1

	if movedelta.length() > 0:
		movedelta = movedelta.normalized() * speed
		move_and_collide(Vector2((movedelta * delta).x,0))
		move_and_collide(Vector2(0,(movedelta * delta).y))
		lastChange += delta
		if lastChange > 0.4:
			lastChange = 0
			hit += 1
			if hit > texts.size()-1:
				hit = 0
			get_node("Sprite2D").texture = texts[hit]
			get_node("Walk").play()
func _process(delta: float) -> void:	
	var camera = get_node("../cam")
	var campos: Vector2 = camera.position
	camera.position = campos.lerp(self.position, 1.0 - exp(-8 * delta))
