extends Control

var pawns : Array[PawnData] = [
	preload("res://data/pawns/van_reily.tres"),
	preload("res://data/pawns/lou_riche.tres"),
	preload("res://data/pawns/tico.tres"),
	preload("res://data/pawns/john_bishous.tres"),
	preload("res://data/pawns/abdoll_relin.tres"),
	preload("res://data/pawns/tenrou_ugetsu.tres")
]

var selector_wait : float = 0
var selection : int = 0
var selected : int = -1
@onready var selector = %Selector
@onready var selector_start : Control = %SelectorStart
@onready var p1_pawn_display : PawnDisplay = %P1PawnDisplay
@onready var switch_sound : AudioStreamPlayer = %SwitchSound
@onready var select_sound : AudioStreamPlayer = %SelectSound

func _ready() -> void:
	#Set up each pawn on bottom
	#Highlight P1 pawn as 0
	change_display(1, 0)
	pass


func _process(delta : float) -> void:
	if selected != -1:
		return
	
	if Input.is_action_just_pressed("ui_accept"):
		selected = selection
		selector.play_animation("selected")
		select_sound.play()
		
	if selector_wait > 0:
		selector_wait -= delta
		return
	var input_vector = Input.get_vector("west","east","north","south")
	var move_dir : Vector2 = Vector2.ZERO
	
	if input_vector.x < 0:
		move_dir.x -= 1
	
	if input_vector.x > 0:
		move_dir.x += 1
	
	if move_dir.x != 0:
		selection = wrapi(selection + sign(move_dir.x),0, len(pawns))
		change_display(1, selection)
		selector.position = selector_start.position + Vector2(175 * selection, 0)
		switch_sound.play()
		selector_wait = 0.25
	
func change_display(player : int, selection: int) -> void:
	var pd : PawnDisplay
	if player == 1:
		pd = p1_pawn_display
	pd.set_pawn_name(pawns[selection].name)
	pd.set_traps(pawns[selection].starting_traps)
