class_name PlayerSpawnerController extends Node3D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass
	
func _get_spawner() -> MultiplayerSpawner:
	return $MultiplayerSpawner
	
func _get_root() -> Node3D:
	return $Players

# -- Spawn a player node and sync it across all peers
func spawn_players(spawn_location: SpawnController, id: int) -> Error:
	if multiplayer.is_server():
		var char : Node3D = null
		var player_data: PlayerState = GameServerManager.players[id]
		char = ResourceLoader.load("res://scenes/player/placeholder.tscn").instantiate()
		char.name = "PlayerPlaceholder_" + str(player_data.id)
		char.owner_id = id
		var position: Vector3 = spawn_location.get_spawner(SpawnController.Sides.BLUE)
		char.set_server_position(position)
		_get_root().add_child(char)
		return OK
	return ERR_UNAUTHORIZED
	
func remove_player(id: int) -> Error:
	if multiplayer.is_server():
		var found_childen: Array[Node] =_get_root().get_children()
		for found_child in found_childen:
			if found_child.owner_id:
				if found_child.owner_id == id:
					found_child.queue_free()
		return OK
	return ERR_UNAUTHORIZED
