extends Node2D

var follower = false
var health = 100
static var NAMES = ["Forkman", "Cobold", "Ork"]

var SFX_loudness = 1.0
var master_volume = 1.0

func _ready() -> void:
	follower = true
	if name.contains("Mine"):
		#get_node("CharacterBody2D/AnimatedSprite2D").connect("animation_finished", Callable(self, "Remove_Mine"))
		get_node("AudioStreamPlayer2D").connect("finished", Callable(self, "explode_ended"))
	set_process_input(true)
	
	if name.contains("Wall"):
		get_node("CharacterBody2D/ProgressBar").hide()
	
	#loads the settings and sounds
	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)
	self.get_node("AudioStreamPlayer2D").volume_db = linear_to_db(master_volume * SFX_loudness)


func _process(delta: float) -> void:
	if follower:
		position = get_viewport().get_mouse_position()
		if name.contains("Mine"):
			if get_node("Area2D").get_overlapping_bodies().size() > 1 or self.get_node("CharacterBody2D") not in get_parent().get_node("../TrapArea").get_overlapping_bodies() or self.get_node("CharacterBody2D") in get_parent().get_node("../TowerArea").get_overlapping_bodies():
				get_node("CharacterBody2D/AnimatedSprite2D").modulate = Color(255, 0, 0, 0.3)
			else:
				get_node("CharacterBody2D/AnimatedSprite2D").modulate = Color(1, 1, 1, 1)
		if name.contains("StickTrap") or name.contains("Wall"):
			if get_node("Area2D").get_overlapping_bodies().size() > 1 or self.get_node("CharacterBody2D") not in get_parent().get_node("../TrapArea").get_overlapping_bodies() or self.get_node("CharacterBody2D") in get_parent().get_node("../TowerArea").get_overlapping_bodies():
				get_node("CharacterBody2D/Sprite2D").modulate = Color(255, 0, 0, 0.3)
				get_node("CharacterBody2D/Sprite2D2").modulate = Color(255, 0, 0, 0.3)
			else:
				get_node("CharacterBody2D/Sprite2D").modulate = Color(1, 1, 1, 1)
				get_node("CharacterBody2D/Sprite2D2").modulate = Color(1, 1, 1, 1)

func get_if_moving_state():
	return follower

func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
		if abs(position) <= get_viewport().get_mouse_position():
			if get_node("Area2D").get_overlapping_bodies().size() <= 1 and self.get_node("CharacterBody2D") in get_parent().get_node("../TrapArea").get_overlapping_bodies() and self.get_node("CharacterBody2D") not in get_parent().get_node("../TowerArea").get_overlapping_bodies():
				follower = false

func Explode_Mine() -> void:
	get_node("CharacterBody2D/AnimatedSprite2D").scale = Vector2(1,1)
	get_node("CharacterBody2D/AnimatedSprite2D").play("Explode")

func explode_ended() -> void:
	Remove_Mine()

func Remove_Mine() -> void:
	queue_free()


func _on_area_2d_body_shape_entered(body_rid: RID, body: Node2D, body_shape_index: int, local_shape_index: int) -> void:
	if follower == false:
		get_node("CharacterBody2D/Timer").start()
		for i in get_node("CharacterBody2D/Area2D").get_overlapping_bodies():
			for j in NAMES:
				if i.get_parent().name.contains(j):
					get_node("CharacterBody2D/ProgressBar").show()
					i.get_parent().set_process(false)


func _on_timer_timeout() -> void:
	var minus = 0
	if self.get_node("AudioStreamPlayer2D").playing == false:
		self.get_node("AudioStreamPlayer2D").playing = true
				
	for i in get_node("CharacterBody2D/Area2D").get_overlapping_bodies():
			for j in NAMES:
				if i.get_parent().name.contains(j):
					minus += 0.3

	health -= minus
	get_node("CharacterBody2D/ProgressBar").value = health
	if minus == 0:
		get_node("CharacterBody2D/Timer").stop()
	
	if health <= 0:
		for i in get_node("CharacterBody2D/Area2D").get_overlapping_bodies():
			for j in NAMES:
				if i.get_parent().name.contains(j):
					i.get_parent().set_process(true)
		queue_free()
		
func WallRelease() -> void:
	for i in get_node("CharacterBody2D/Area2D").get_overlapping_bodies():
			for j in NAMES:
				if i.get_parent().name.contains(j):
					i.get_parent().set_process(true)
