extends Area3D
var Bullet = preload("res://BARCO ELEMENTOS TEXTURAS/CAÑONES/area_3dprollectil_cañon.tscn")
var fire_cooldown: float = 1.7
var can_fire: bool = true
var bullet_speed: float = 20.0
var fire_timer: Timer
# Asegúrate de que esta variable exista. La agregué aquí.
var rotation_speed: float = 1.0
func _ready() -> void:
	fire_timer = Timer.new()
	add_child(fire_timer)
	fire_timer.wait_time = fire_cooldown
	fire_timer.one_shot = true
	fire_timer.timeout.connect(func(): can_fire = true)
func _process(delta: float) -> void:
	# Lógica de Disparo
	if Input.is_action_pressed("DISPARACAÑON") and can_fire:
		can_fire = false
		instantiate_bullet()
		fire_timer.start()
	# Lógica de Rotación
	var rotation_direction: float = 0.0
	if Input.is_action_pressed("girav"):
		rotation_direction = 1.0
	elif Input.is_action_pressed("girab"):
		rotation_direction = -1.0
	
	# Aplica la rotación en el eje X.
	#rotate_x(rotation_speed * rotation_direction * delta)
	rotate_x(delta * rotation_speed * rotation_direction  )
	if Input.is_action_pressed("giraz"):   
		rotation_direction = 1.0
	# Si se presiona la acción "girax", gira en la dirección opuesta.
	elif Input.is_action_pressed("girax"):
		rotation_direction = -1.0
	# Aplica la rotación en el eje Y.
	# Si ninguna tecla está presionada, rotation_direction será 0 y el objeto no girará.
	rotate_y(rotation_speed * rotation_direction * delta)
func instantiate_bullet() -> void:
	var bullet = Bullet.instantiate()
	bullet.global_transform = self.global_transform
	
	if bullet is RigidBody3D:
		bullet.apply_central_impulse(-self.global_transform.basis.z * bullet_speed)
	else:
		print("¡Error! La escena del proyectil no es un RigidBody3D y no puede recibir un impulso.")
		
	get_tree().root.add_child(bullet)