extends CharacterBody2D

@onready var player = $"../Player"
@onready var bloodParticles = preload("res://assets/scenes/blood.tscn")

var dmgtotal := float(randi_range(20,40))

func _ready() -> void:
	print(dmgtotal)
	
	if Globals.unlockedUpgrades.get("dumbbell", 0) > 0:
		dmgtotal *= pow(1.5, Globals.unlockedUpgrades["dumbbell"])
	if Globals.unlockedUpgrades.get("speedMayhem", 0) > 0:
		dmgtotal *= pow(1 + (player.velocity.length() + player.dashVel.length()) / 400, Globals.unlockedUpgrades["speedMayhem"])

	print(dmgtotal)

func _physics_process(delta: float) -> void:
	if player.transitioning: return
	
	move_and_collide(velocity * delta)

func _on_area_entered(area: Area2D) -> void:	
	if not area.get_parent().has_meta("dontKillUrselfPlease"):
		queue_free()


func _on_body_entered(body: Node2D) -> void:
	if body.get_meta("enemy", 0) == 1:
		#body.get_node("explode").emitting = true
		#body.get_node("explode").reparent(body.geat_parent())
		
		body.health -= dmgtotal * player.damage_stack
		body.knockback = Vector2(velocity.x * 0.5, velocity.y * 0.5)
		body.hitsfx.play()
		#AnimationPlayer.new().stop()
		body.get_node("AnimationPlayer").stop()
		body.get_node("AnimationPlayer").play("flash")
		
		var bloodInsta = bloodParticles.instantiate()
		bloodInsta.emitting = true
		bloodInsta.unlock = true
		bloodInsta.global_position = body.global_position
		get_parent().add_child(bloodInsta)
		
		#player.heal(randi_range(3,5))
		
		#body.queue_free()
	
	print(body.name)
	queue_free()
