class_name Map
extends Node3D

func _ready() -> void:
	if multiplayer.is_server():
		randomize()
		var random_id: int = Multiplayer.players.keys()[randi_range(0, Multiplayer.players.size() - 1)]
		Multiplayer.players[random_id].hunter = true
		for i: int in $PlayerSpawners.get_children().size():
			if i < Multiplayer.players.size():
				var spawner: PlayerSpawner = $PlayerSpawners.get_child(i)
				var player: Dictionary = Multiplayer.players[Multiplayer.players.keys()[i]]
				spawner.spawn_player(player)

func _on_exit_entered(body: Node3D) -> void:
	if multiplayer.is_server() and (body is Player):
		var player: Player = body
		if !player.hunter and Multiplayer.fuseboxes <= 0:
			Multiplayer.players[player.get_multiplayer_authority()].escaped = true
			player.get_caught.rpc_id(player.get_multiplayer_authority(), $SpectatorCamera.get_path())

func _process(_delta: float) -> void:
	if Multiplayer.fuseboxes <= 0:
		(($EscapeArea/Light as CSGBox3D).material as StandardMaterial3D).albedo_color.r8 = 0
		(($EscapeArea/Light as CSGBox3D).material as StandardMaterial3D).albedo_color.g8 = 255
	else:
		(($EscapeArea/Light as CSGBox3D).material as StandardMaterial3D).albedo_color.r8 = 255
		(($EscapeArea/Light as CSGBox3D).material as StandardMaterial3D).albedo_color.g8 = 0
