diff --git a/Assets/BossRoom/Scripts/Client/Game/State/ClientCharSelectState.cs b/Assets/BossRoom/Scripts/Client/Game/State/ClientCharSelectState.cs index 20766c5c8..1bce35af1 100644 --- a/Assets/BossRoom/Scripts/Client/Game/State/ClientCharSelectState.cs +++ b/Assets/BossRoom/Scripts/Client/Game/State/ClientCharSelectState.cs @@ -132,7 +132,7 @@ protected override void OnDestroy() CharSelectData.IsLobbyClosed.OnValueChanged -= OnLobbyClosedChanged; CharSelectData.OnFatalLobbyError -= OnFatalLobbyError; CharSelectData.OnAssignedPlayerNumber -= OnAssignedPlayerNumber; - CharSelectData.LobbyPlayers.ArrayChangedEvent -= OnLobbyPlayerStateChanged; + CharSelectData.LobbyPlayers.OnListChanged -= OnLobbyPlayerStateChanged; } if (Instance == this) Instance = null; @@ -150,7 +150,7 @@ public override void NetworkStart() CharSelectData.IsLobbyClosed.OnValueChanged += OnLobbyClosedChanged; CharSelectData.OnFatalLobbyError += OnFatalLobbyError; CharSelectData.OnAssignedPlayerNumber += OnAssignedPlayerNumber; - CharSelectData.LobbyPlayers.ArrayChangedEvent += OnLobbyPlayerStateChanged; + CharSelectData.LobbyPlayers.OnListChanged += OnLobbyPlayerStateChanged; } } @@ -166,7 +166,7 @@ private void OnAssignedPlayerNumber(int playerNum) /// /// Called by the server when any of the seats in the lobby have changed. (Including ours!) /// - private void OnLobbyPlayerStateChanged(CharSelectData.LobbyPlayerArray lobbyArray ) + private void OnLobbyPlayerStateChanged(NetworkListEvent lobbyArray ) { UpdateSeats(); diff --git a/Assets/BossRoom/Scripts/Shared/Game/State/CharSelectData.cs b/Assets/BossRoom/Scripts/Shared/Game/State/CharSelectData.cs index f7e399bd9..e74bf378d 100644 --- a/Assets/BossRoom/Scripts/Shared/Game/State/CharSelectData.cs +++ b/Assets/BossRoom/Scripts/Shared/Game/State/CharSelectData.cs @@ -7,6 +7,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; +using MLAPI.NetworkVariable.Collections; using UnityEngine; namespace BossRoom @@ -91,146 +92,17 @@ public void NetworkSerialize(NetworkSerializer serializer) } } - void OnDestroy() - { - m_LobbyPlayers.Cleanup(); - } - - /// - /// TEMP! This is substitute for NetworkVariableList<>. It will be replaced by a NetworkVariableList when those are once again - /// supported. - /// - public class LobbyPlayerArray - { - private List m_LobbyPlayers; - private CharSelectData m_CharSelectData; - - /// - /// Event that gets raised when the array has changed somehow. - /// - public event Action ArrayChangedEvent; - - public LobbyPlayerArray(CharSelectData data, int count ) - { - m_LobbyPlayers = new List(); - m_CharSelectData = data; - - if (NetworkManager.Singleton.IsServer) - { - NetworkManager.Singleton.OnClientConnectedCallback += ClientConnected; - } - } - - void ClientConnected(ulong clientId) - { - m_CharSelectData.StartCoroutine(CoroClientConnected(clientId)); - } - - private IEnumerator CoroClientConnected(ulong clientId) - { - yield return new WaitForSeconds(2); - - //send the new guy our initial state. - m_CharSelectData.LobbyPlayerUpdateArrayClientRpc(m_LobbyPlayers.ToArray()); - } - - public int Count { get { return m_LobbyPlayers.Count; } } - - public void Add(LobbyPlayerState state, bool fromSync=false) - { - m_LobbyPlayers.Add(state); - ArrayChangedEvent?.Invoke(this); - - if (NetworkManager.Singleton.IsServer ) - { - m_CharSelectData.LobbyPlayerUpdateArrayClientRpc(m_LobbyPlayers.ToArray()); - } - } - - public void RemoveAt(int index) - { - m_LobbyPlayers.RemoveAt(index); - ArrayChangedEvent?.Invoke(this); - - if (NetworkManager.Singleton.IsServer ) - { - m_CharSelectData.LobbyPlayerUpdateArrayClientRpc(m_LobbyPlayers.ToArray()); - } - } - - public System.Collections.IEnumerator GetEnumerator() - { - return m_LobbyPlayers.GetEnumerator(); - } - - public LobbyPlayerState this[int i] - { - get - { - return m_LobbyPlayers[i]; - } - set - { - if(!NetworkManager.Singleton.IsServer ) - { - throw new MLAPI.Exceptions.NotServerException("CharSelectData.LobbyPlayerArray can only be written to on the server!"); - } - else - { - m_LobbyPlayers[i] = value; - m_CharSelectData.LobbyPlayerUpdateArrayClientRpc(m_LobbyPlayers.ToArray()); - ArrayChangedEvent?.Invoke(this); - } - } - } - - /// - /// Updates a single element. For use only be the CharSelectData class. - /// - public void ClientSyncUpdate(LobbyPlayerState[] playerArray ) - { - if( !NetworkManager.Singleton.IsServer ) - { - m_LobbyPlayers.Clear(); - m_LobbyPlayers.AddRange(playerArray); - - ArrayChangedEvent?.Invoke(this); - } - } - - public void Cleanup() - { - if (NetworkManager.Singleton && NetworkManager.Singleton.IsServer) - { - NetworkManager.Singleton.OnClientConnectedCallback -= ClientConnected; - } - } - } - - /// - /// Receives a new array of LobbyPlayerStates and replaces the existing contents of our m_LobbyPlayerArray with them. This "maximalist approach" - /// is because it's tricky to send incremental array updates right now. You can't just send an initial state on client connection, and then - /// subsequent incremental updates, because you don't know exactly when the client's connection is going to be open for transmission. If you send - /// a client an incremental update while it still has its inital state message pending, it will get confused. - /// In any case, this system is meant to be temporary, and will be replaced when NetworkVariableList is supported again. - /// - [ClientRpc] - private void LobbyPlayerUpdateArrayClientRpc(LobbyPlayerState[] playerArray ) - { - m_LobbyPlayers.ClientSyncUpdate(playerArray); - } - - private LobbyPlayerArray m_LobbyPlayers; + private NetworkList m_LobbyPlayers; private void Awake() { - m_LobbyPlayers = new LobbyPlayerArray(this, 8); + m_LobbyPlayers = new NetworkList(); } /// /// Current state of all players in the lobby. /// - public LobbyPlayerArray LobbyPlayers { get { return m_LobbyPlayers; } } + public NetworkList LobbyPlayers { get { return m_LobbyPlayers; } } /// /// When this becomes true, the lobby is closed and in process of terminating (switching to gameplay).