extends Node2D

@export var shot_scene: PackedScene
@export var enemy_scene: PackedScene

var b_direction = Vector2.ZERO

func _ready() -> void:
	$Music.play()

func _process(delta: float) -> void:
	var shot_ins = shot_scene.instantiate()
	
	if Global.someone_died == 1:
		$EnemyKill.play()
		await get_tree().create_timer(0.75).timeout
		Global.someone_died = 0
	
	if Input.is_action_just_pressed("shoot"):
		var direction = get_global_mouse_position() - $player_mask.global_position
		if $player_mask.element == "wind":
			shot_ins.element = "wind"
		if $player_mask.element == "fire":
			shot_ins.element = "fire"
		if $player_mask.element == "water":
			shot_ins.element = "water"
		shot_ins.position = $player_mask.global_position
		shot_ins.direction = direction.normalized()
		add_child(shot_ins)
	


func _on_timer_timeout() -> void:
	
	var enemy_ins = enemy_scene.instantiate()
	
	if randi() % 2 == 0:
		enemy_ins.position.x = randi_range(-100, 0)
	else:
		enemy_ins.position.x = randi_range(1175, 1250)
	if randi() % 2 == 0:
		enemy_ins.position.y = randi_range(-100, -50)
	else:
		enemy_ins.position.y = randi_range(700, 750)
	add_child(enemy_ins)

func shake_node(node: Node2D, duration: float = 0.5, strength: float = 10.0):
	var tween = node.create_tween()
	var original_pos = node.position

	# cantidad de movimientos (puede ser más grande = más violento)
	var steps = int(duration / 0.05)

	for i in range(steps):
		var offset = Vector2(randf_range(-strength, strength), randf_range(-strength, strength))
		tween.tween_property(node, "position", original_pos + offset, 0.05).set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN_OUT)

	# volver a la posición original al final
	tween.tween_property(node, "position", original_pos, 0.05).set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN_OUT)
