using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Networking.Client
{
[CreateAssetMenu(menuName = "Major Project/Networking/Client/OtherClientsList", order = 150)]
public class ConnectedClients : ScriptableObject
{
#if UNITY_EDITOR
[SerializeField]
[Multiline]
private string DevNotes;
#endif
///
/// All Clients which are Currently Connected;
///
[SerializeField]
public List AllClients;
///
/// Called when a client connects or disconnet
///
public System.Action> OnClientsChange;
public void SetUp(ClientObject client)
{
Reset();
client.client.RegisterHandler(LoginProtocols.OtherClientConnected, OnClientConnect);
client.client.RegisterHandler(LoginProtocols.OtherClientDisconnected, OnClientDisconnect);
}
public void Reset()
{
AllClients = new List();
OnClientsChange?.Invoke(AllClients);
}
public void OnClientConnect(NetworkMessage msg)
{
LoginProtocols.LoginMsg loginMsg;
if (!msg.TryRead(out loginMsg))
return;
AllClients.Add(new ClientData(loginMsg));
OnClientsChange?.Invoke(AllClients);
}
public void OnClientDisconnect(NetworkMessage msg)
{
LoginProtocols.LoginMsg loginMsg;
if (!msg.TryRead(out loginMsg))
return;
AllClients.RemoveAll(p => (p.Name == loginMsg.Name) && (p.Color == loginMsg.Color) && (p.characterAnimal == loginMsg.Animal));
OnClientsChange?.Invoke(AllClients);
}
}
[System.Serializable]
public class ClientData
{
///
/// Client Name
///
public string Name;
///
/// Color which represents player, picked by player
///
public Color Color;
///
/// Clients character animal - needed for instantiation
///
public string characterAnimal = "Bear";
public ClientData(LoginProtocols.LoginMsg msg)
{
this.Name = msg.Name;
this.Color = msg.Color;
this.characterAnimal = msg.Animal;
}
}
}