extends Node3D

class_name Table

var MaskList = [
	preload("res://Masks/Mutaconda/Mutaconda.tscn")
]
var AvailableMasks = []
#var UsedMasks = []


	

func GetRandEntry():
	var rand = randi() % len(AvailableMasks)
	var rEntry = AvailableMasks[rand]
	AvailableMasks.remove_at(rand)
	return rEntry

var TakenPositions: Array[Vector2] = []
func GetRandomPosition():
	var rx := randi_range(-6,6)/10.0
	var rz := randf_range(-4,4)/10.0
	var vec = Vector2(rx,rz)
	var alrEx = false
	for i in TakenPositions:
		if i.x == rx and i.y == rz:
			alrEx = false
			break
	if alrEx:
		return GetRandomPosition()
	else:
		return vec
	

func Present(side:Node3D,plr):
	AvailableMasks = MaskList.duplicate_deep()
	for i in range(5):
		var MaskInstance = GetRandEntry().instantiate() as Mask
		side.add_child(MaskInstance)
		var pos = GetRandomPosition()
		#MaskInstance.mesh.top_level = true
		MaskInstance.mesh.transform = (
			side.transform.translated(Vector3(pos.x,0.1,pos.y))
			.rotated_local(Vector3.RIGHT,-PI/3.5)
			.rotated_local(Vector3.FORWARD,randf_range(0.0, 2*PI))
		)
		MaskInstance.WearOn(plr)
		
	
		
@onready var Side1 = $Side1
@onready var Side2 = $Side2
		
	
