extends CharacterBody2D

class_name Enemy

@export var speed = 1000

@export var value = 10

var health = 0
@export var total_health := 30



@export var difficulty := 1

@export var player : CharacterBody2D

@onready var navigation : NavigationAgent2D = $NavigationAgent2D
@onready var health_bar : ProgressBar = $HealthBar

@onready var particles : CPUParticles2D = $CPUParticles2D

func _init():
	health = total_health

func _physics_process(delta: float) -> void:
	
	health_bar.value = (float(health)/total_health)*100
	navigation.target_position = player.position
	if not navigation.is_navigation_finished():
		navigation.velocity = ((navigation.get_next_path_position()-position).normalized()*speed*delta)
	else:
		navigation.velocity = Vector2(0,0)

	if health <= 0:
		player.score += difficulty
		player.money += int(value*100*(float(1)/(player.tax+100)))
		player.inventory.update_stats()
		queue_free()
	move_and_slide()

func damage(damage : int):
	particles.emitting = true
	health -= damage
	if health <= 0:
		return -health
	
	return 0


func _on_navigation_agent_2d_velocity_computed(safe_velocity: Vector2) -> void:
	velocity = safe_velocity