class_name RoundRobinManager extends PlayManager
## Players take turns one after the other, looping back to the first player at the end.


func on_initialization() -> void:
	# Spawn all players
	for p: WorldPlayer in players:
		print("Emitting spawn signal for player ", p)
		spawning.emit(p)

	# Set first player as active
	players[0].shot_setup.phase = ShotSetup.Phase.AIM


func on_turn_finished(source: ShotSetup) -> void:
	print_debug("Shot finished for ", source.player.name)
	players.push_back(players.pop_front())
	players[0].shot_setup.queue_restart()
	turn_started.emit(players[0])


func on_player_death(player: WorldPlayer) -> void:
	# If the dying player was active, we need to finish it and activate the next player's turn
	if player.shot_setup.is_active():
		on_turn_finished(player.shot_setup)

	players.erase(player)
	player.deconstruct()
	print("Player 0 phase: ", ShotSetup.Phase.keys()[players[0].shot_setup.phase])
	if len(players) == 1:
		on_win_condition()


func on_win_condition() -> void:
	winner.emit(players[0])
