extends Node3D

var bullet_target_pos = Vector3(1.95,0.03,4.612) #2.231

var loaded_bullets = Array()
var max_bullets: int = 6
var current_round: int = 1
var timer: Timer

@export var is_player_revolver: bool = false
@export var cylinder: Node3D

var is_cylinder_out: bool = false
func _ready() -> void:
	loaded_bullets.resize(max_bullets + 1)
	timer = $Timer
	if is_player_revolver:
		GameManager.set_player_revolver(self)
	else:
		GameManager.set_enemy_revolver(self)

func _process(delta: float) -> void:
	if is_cylinder_out:
		cylinder.position = lerp(cylinder.position, Vector3(1.947,0.543,3.979), delta/ 0.1)
	else:
		cylinder.position = lerp(cylinder.position, Vector3(0,0.543,3.979), delta/ 0.1)

	cylinder.rotation_degrees = lerp(cylinder.rotation_degrees,Vector3(30 + current_round*60,90,90), delta / 0.1 )
	#cylinder.rotation_degrees = Vector3(30 + current_round*60,90,90)
func load_bullet(bullet_object: Node3D):
	loaded_bullets[current_round] = bullet_object
	bullet_object.is_loaded = true
	bullet_object.destination_reached.connect(on_bullet_reached)
	bullet_object.moveTo(to_global(bullet_target_pos)) 
	bullet_object.rotate_x(deg_to_rad(-90))
	is_cylinder_out = true
	bullet_object.reparent(cylinder)
	timer.timeout.connect(on_load_anim_end)
	#cylinder.rotation.x = 90.0
	
func on_load_anim_end():
	is_cylinder_out = false
	if is_player_revolver:
		GameManager.end_player_turn()
	else:
		GameManager.end_enemy_turn()
	timer.timeout.disconnect(on_load_anim_end)
	
func on_bullet_reached():
	current_round += 1
	timer.start()
func shoot():
	pass
