extends Node2D

@onready var player = get_node("/root/Game/Player")
@onready var bar = get_node("/root/Game/CanvasLayer/ProgressBar")
signal leveled_up

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	bar.max_value = player.max_xp
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

func on_enemy_died(enemy):
	call_deferred("drop_item", enemy)
	var enemy_xp = enemy.xp
	player.current_xp += enemy.xp
	bar.value = player.current_xp
	if player.current_xp >= player.max_xp:
		leveled_up.emit()
		player.max_xp = player.max_xp * 1.3
		bar.max_value = player.max_xp
		player.current_xp = 0
		bar.value = 0
		player.level += 1
		

	
func drop_item(enemy):
	if not is_instance_valid(enemy):
		return
	for entry in enemy.drop_table:
		if randf() < entry.chance:
			var drop = entry.drop.instantiate()
			drop.global_position = enemy.global_position
			add_child(drop)
