Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand All @@ -166,7 +166,7 @@ private void OnAssignedPlayerNumber(int playerNum)
/// <summary>
/// Called by the server when any of the seats in the lobby have changed. (Including ours!)
/// </summary>
private void OnLobbyPlayerStateChanged(CharSelectData.LobbyPlayerArray lobbyArray )
private void OnLobbyPlayerStateChanged(NetworkListEvent<CharSelectData.LobbyPlayerState> lobbyArray )
{
UpdateSeats();

Expand Down
136 changes: 4 additions & 132 deletions Assets/BossRoom/Scripts/Shared/Game/State/CharSelectData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using MLAPI.NetworkVariable.Collections;
using UnityEngine;

namespace BossRoom
Expand Down Expand Up @@ -91,146 +92,17 @@ public void NetworkSerialize(NetworkSerializer serializer)
}
}

void OnDestroy()
{
m_LobbyPlayers.Cleanup();
}

/// <summary>
/// TEMP! This is substitute for NetworkVariableList<>. It will be replaced by a NetworkVariableList<LobbyPlayerState> when those are once again
/// supported.
/// </summary>
public class LobbyPlayerArray
{
private List<LobbyPlayerState> m_LobbyPlayers;
private CharSelectData m_CharSelectData;

/// <summary>
/// Event that gets raised when the array has changed somehow.
/// </summary>
public event Action<LobbyPlayerArray> ArrayChangedEvent;

public LobbyPlayerArray(CharSelectData data, int count )
{
m_LobbyPlayers = new List<LobbyPlayerState>();
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);
}
}
}

/// <summary>
/// Updates a single element. For use only be the CharSelectData class.
/// </summary>
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;
}
}
}

/// <summary>
/// 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<T> is supported again.
/// </summary>
[ClientRpc]
private void LobbyPlayerUpdateArrayClientRpc(LobbyPlayerState[] playerArray )
{
m_LobbyPlayers.ClientSyncUpdate(playerArray);
}

private LobbyPlayerArray m_LobbyPlayers;
private NetworkList<LobbyPlayerState> m_LobbyPlayers;

private void Awake()
{
m_LobbyPlayers = new LobbyPlayerArray(this, 8);
m_LobbyPlayers = new NetworkList<LobbyPlayerState>();
}

/// <summary>
/// Current state of all players in the lobby.
/// </summary>
public LobbyPlayerArray LobbyPlayers { get { return m_LobbyPlayers; } }
public NetworkList<LobbyPlayerState> LobbyPlayers { get { return m_LobbyPlayers; } }

/// <summary>
/// When this becomes true, the lobby is closed and in process of terminating (switching to gameplay).
Expand Down