extends KinematicBody

class_name Player

var attackRate : float = 0.3
var lastAttackTime : int = 0

# physics
var airborneSpeed : float = 3.0
var walkSpeed : float = 5.0
var runSpeed : float = 10.0
var jumpForce : float = 10.0
var gravity : float = 15.0
 
var vel := Vector3()
 
# components
onready var camera = get_node("CameraOrbit")
onready var interactCast = get_node("InteractRayCast")
onready var anim = get_node("Casual2_Female/AnimationPlayer")

func _process(delta):
	# attack input
	if Controller.player_input_enabled and is_on_floor() and Input.is_action_just_pressed("interact"):
		try_interact()
		
	for area in $TouchableArea.get_overlapping_areas():
		try_touch(area)

# called every physics step (60 times a second)
func _physics_process (delta):
	var input := Vector3()
		
	# gravity
	vel.y -= gravity * delta
	
	if Controller.player_input_enabled :
		# movement inputs
		if Input.is_action_pressed("move_forward"):
			input.z += 1
		if Input.is_action_pressed("move_backward"):
			input.z -= 1
		if Input.is_action_pressed("move_left"):
			input.x += 1
		if Input.is_action_pressed("move_right"):
			input.x -= 1
			
		input = input.normalized()
		
		# get the relative direction
		var dir := (transform.basis.z * input.z + transform.basis.x * input.x)
		
		# apply the direction to our velocity
		if not is_on_floor() and dir.length_squared() == 0:
			vel.x = lerp(vel.x, 0, 0.02)
			vel.z = lerp(vel.z, 0, 0.02)
		elif Input.is_action_pressed("run"):
			vel.x = dir.x * runSpeed
			vel.z = dir.z * runSpeed
		else:
			vel.x = dir.x * walkSpeed
			vel.z = dir.z * walkSpeed
			
		# jump input
		if Input.is_action_pressed("jump") and is_on_floor():
			vel.y = jumpForce
	else:
		vel.x = 0
		vel.z = 0
	
	if is_on_floor():
		if anim.current_animation != "Punch":
			if input.x != 0 || input.z != 0:
				if Controller.player_input_enabled and Input.is_action_pressed("run"):
					anim.play("Run")
				else:
					anim.play("Walk")
			else:
				anim.play("Idle")
	else:
		anim.play("RecieveHit")
	
	# move along the current velocity
	var previous_vel = vel
	vel = move_and_slide(vel, Vector3.UP, true, 4, deg2rad(30))
	
	"debug for water"
	if translation.y <= -5.75:
		translation.y = -5.75
		vel.y = 0
	
	if is_on_wall():
		vel.y = min(previous_vel.y, vel.y)

# called when we collect a coin
func give_gold (amount):
	var gold = Controller.player_infos.gold
	gold += amount
	Controller.player_infos.gold = gold

# called when an enemy deals damage to us
func take_damage (damageToTake):
	Controller.player_infos.curHp -= damageToTake
	
	# if our health is 0, die
	if Controller.player_infos.curHp <= 0:
		die()

# called when our health reaches 0
func die ():
	# reload the scene
	get_tree().reload_current_scene()
	
func try_touch(area):
	if area.has_method("on_touch"):
		area.on_touch(self)

# called when we press the interact button
func try_interact():
	# is the ray cast colliding with an enemy?
	for area in $InteractorArea.get_overlapping_areas():
		# play the animation
		anim.stop()
		anim.play("Idle")
		if area.has_method("on_interact"):
			area.on_interact()
