@tool
extends Control

const CARD_SCENE = preload("uid://dl3ve6prnn2m6")

@export var radius: float = 10.
@export_range(0,360,0.01,"or_greater","radians_as_degrees") var arch: float = PI/6
@export var rotate_cards: bool = true

func _ready() -> void:
	GameplaySignalBus.draw_card.connect(draw_card)

func draw_card(card_resource: CardResource):
	var card: Card = CARD_SCENE.instantiate()
	
	card.card_resource = card_resource
	add_child(card)

func _process(_delta: float) -> void:
	for i in range(get_child_count()):
		var child: Card = get_child(i)
		if child.top_level:
			continue
		
		var angle: float = -arch+2*arch*((float(i+0.5))/get_child_count())
		var pointing_up: float = angle - PI/2
		child.position = lerp(child.position,get_combined_pivot_offset()+Vector2.from_angle(pointing_up) * radius-child.get_combined_pivot_offset(),0.25)
		if rotate_cards:
			child.rotation = lerp(child.rotation,angle,0.25)
