KitchenChaos/Assets/Scripts/CharacterSelectReady.cs

35 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Unity.Netcode;
public class CharacterSelectReady : NetworkBehaviour {
public static CharacterSelectReady Instance { get; private set; }
private readonly Dictionary<ulong, bool> playerReadyDictionary = new();
[CanBeNull] public event EventHandler OnReadyChanged;
private void Awake() => Instance = this;
public void SetPlayerReady() => SetPlayerReadyServerRpc();
[ServerRpc(RequireOwnership = false)]
private void SetPlayerReadyServerRpc(ServerRpcParams serverRpcParams = default) {
SetPlayerReadyClientRpc(serverRpcParams.Receive.SenderClientId);
playerReadyDictionary[serverRpcParams.Receive.SenderClientId] = true;
if (NetworkManager.Singleton.ConnectedClientsIds.All(clientId => playerReadyDictionary.ContainsKey(clientId) && playerReadyDictionary[clientId])) {
KitchenGameLobby.Instance.DeleteLobby();
Loader.LoadNetwork(Loader.Scene.GameScene);
}
}
[ClientRpc]
private void SetPlayerReadyClientRpc(ulong clientId) {
playerReadyDictionary[clientId] = true;
OnReadyChanged?.Invoke(this,EventArgs.Empty);
}
public bool IsPlayerReady(ulong clientId) => playerReadyDictionary.ContainsKey(clientId) && playerReadyDictionary[clientId];
}