extends Node


signal bullet_selected(object)
signal state_changed

enum  GameState { PLAYER1_GUN_LOAD, PLAYER2_GUN_LOAD, GUN_ANIMATION}
var current_state = GameState.PLAYER1_GUN_LOAD
var prev_state: int
var bullets: Array

var player1_revolver: Node3D
var player2_revolver: Node3D
var bullet_stack: Node3D

func _ready() -> void:
	bullet_selected.connect(on_bullet_selected)

func _input(event: InputEvent) -> void:
	if event.is_action_released("load"):
		player1_revolver.load_bullet(bullet_stack.bullets[0])
	elif event.is_action_released("load2"):
		player1_revolver.load_bullet(bullet_stack.bullets[1])

func set_player_revolver(newRevolver):
	player1_revolver = newRevolver
func set_enemy_revolver(newRevolver):
	player2_revolver = newRevolver
func set_bullet_stack(newStack):
	bullet_stack = newStack

func add_bullet(bullet):
	bullets.append(bullet)


func on_bullet_selected(bullet_object: Node3D):
	if current_state == GameState.PLAYER1_GUN_LOAD:
		if bullet_object.is_loaded != true:
			set_state(GameState.GUN_ANIMATION)
			player1_revolver.load_bullet(bullet_object)

func set_state(newState):
	prev_state = current_state
	current_state = newState
	state_changed.emit()

		
	
func end_player_turn():
	set_state(GameState.PLAYER2_GUN_LOAD)
func end_enemy_turn():
	set_state(GameState.PLAYER1_GUN_LOAD)
func get_enemy_revolver() -> Node3D:
	return player2_revolver
