extends Node2D

var target
var speed
var hitpoint
var distancetaken = 0

#For the mortar, to create a nice bezier curve!
var starterpos = 0
var targetpos = 0
var midpoint = 0
var t = 0.0
var duration = 0.5

var SFX_loudness = 1.0
var master_volume = 1.0
var reached_once = false

static var NAMES = ["Forkman", "Cobold", "Ork"]

func _ready() -> void:
	starterpos = global_position
	targetpos = target.global_position
	midpoint = (targetpos + starterpos) / 2
	midpoint.y -= 200	#height of the curve!
	var config = ConfigFile.new()
	var err = config.load("user://settings.cfg")
	if err == OK:
		var master_volume_raw = config.get_value("audio", "master_volume", 100.0)
		master_volume = clamp(master_volume_raw / 100.0, 0.0, 1.0)
		var sfx_volume_raw = config.get_value("audio", "sfx_volume", 100.0)
		SFX_loudness = clamp(sfx_volume_raw / 100.0, 0.0, 1.0)
	
	get_node("AudioStreamPlayer2D").finished.connect(finished_end_sound)

func set_targe(_target : Node2D) -> void:
	target = _target

func set_speed(_speed: int) -> void:
	speed = _speed
	
func set_hitpoint(_hitpoint: int) -> void:
	hitpoint = _hitpoint
	
	
func _process(delta: float) -> void:
	if not reached_once:
		if name.contains("Rock"):
			t += delta / duration
			position = bezier(t)
			if t >= 1.0:
				get_node("CharacterBody2D/AnimatedSprite2D").play("hit")
				var killalll = get_node("Area2D").get_overlapping_bodies()
				for i in killalll:
					for j in NAMES:
						if i.get_parent().name.contains(j):
							i.get_parent().enemy_hurt(hitpoint)
				target_reached()
				set_process(false)
		elif target and speed > 0:
			# Get the direction vector from the bullet to the target
			var direction = (target.get_parent().get_parent().get_position() - position).normalized()

			# Rotate the bullet to face the target
			if name.contains("Arrow"):
				rotation = direction.angle() + deg_to_rad(90)

			# Move the bullet towards the target
			position += direction * speed * delta
			distancetaken += (direction * speed * delta).length()
			
			if position.distance_to(target.get_parent().get_parent().position) <= 5:
				target_reached()
			if distancetaken > 250:
				queue_free()
		else:
			queue_free()
		
	
func target_reached() -> void:
	if not reached_once:
		reached_once = true
		if name.contains("Arrow"):				#special effects here!
			self.get_node("CharacterBody2D").get_node("Sprite2D").hide()
			if randf() < 0.1:
				target.get_parent().set_speed(target.get_parent().get_speed() / 2)
				target.get_parent().adjust_speed_of_animation()
				pass
		if name.contains("Magic"):
			self.get_node("CharacterBody2D").get_node("Sprite2D").hide()
			if randf() < 0.1:
				#target.get_parent().set_progress(max(target.get_parent().get_progress() - 60, 0))
				target.get_parent().set_progress(0)
				target.get_parent().set_process(true)

		if target:
			target.get_parent().enemy_hurt(hitpoint)
		self.get_node("AudioStreamPlayer2D").volume_db = linear_to_db(master_volume * SFX_loudness)
		self.get_node("AudioStreamPlayer2D").playing = true



func finished_end_sound() -> void:
		queue_free()

func bezier(t: float):
	var q0 = starterpos.lerp(midpoint, t)
	var q1 = midpoint.lerp(targetpos, t)
	return q0.lerp(q1, t)



func _on_animated_sprite_2d_animation_finished() -> void:
	queue_free()
