using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace Multiplayer
|
|
{
|
|
|
|
public class ClientManager : MonoSingleton<ClientManager>
|
|
{
|
|
|
|
#region Inspector Variables
|
|
[Header("Client Settings")]
|
|
[Tooltip("Address server is hosted on")]
|
|
[SerializeField]
|
|
private string ServerAddress = "127.0.0.1";
|
|
[Tooltip("Port to connect to server on")]
|
|
[SerializeField]
|
|
private int Port = 4444;
|
|
[Tooltip("Connects client to server on awake")]
|
|
[SerializeField]
|
|
private bool ConnectOnStart = false;
|
|
|
|
public string LobbyScene;
|
|
|
|
public string GameScene;
|
|
|
|
public string Name;
|
|
#endregion
|
|
|
|
//Returns if client is current connected to a server
|
|
public bool IsConnected;
|
|
public byte ID;
|
|
|
|
//UNET client class which connects to the server;
|
|
public NetworkClient Client { get; private set;}
|
|
|
|
public ClientLoginManager loginManager;
|
|
|
|
/// <summary>
|
|
/// Call to connect client to host
|
|
/// </summary>
|
|
[ContextMenu("Start Client")]
|
|
public void StartClient()
|
|
{
|
|
StartClient(ServerAddress, Port);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Call to connect the client to the host
|
|
/// </summary>
|
|
/// <param name="ServerAddress">Server Address to connect to</param>
|
|
/// <param name="Port">Port to connect on</param>
|
|
public void StartClient(string ServerAddress, int Port)
|
|
{
|
|
//If client is already connected to don't continue
|
|
if (IsConnected)
|
|
{
|
|
Debug.Log("Client already connected to a server. Client needs to disconnect before it can connect to a server");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("Attempting to connect to: '" + ServerAddress + "' on port : '" + Port + "'");
|
|
|
|
Client = new NetworkClient();
|
|
loginManager = new ClientLoginManager(this);
|
|
|
|
Client.Configure(ChannelConfig.DefaultTopology());
|
|
Client.Connect(ServerAddress, Port);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Sends message to server
|
|
/// </summary>
|
|
/// <param name="msgType">Message type, used to determin message handler</param>
|
|
/// <param name="msg">The message to send</param>
|
|
/// <param name="channel">Which channel to send on, by deafult sends on Reliable</param>
|
|
/// <param name="BackLog">If client isn't connected to server add to BackLog, by default true</param>
|
|
public void SendMessage(short msgType, MessageBase msg, ServerChannel channel = ServerChannel.Reliable)
|
|
{
|
|
//if client isn't connected add to backlog
|
|
if (!IsConnected)
|
|
Debug.Log("Player not connected to server");
|
|
|
|
//Debug.Log("Sending message to server");
|
|
Client.SendByChannel(msgType, msg, (int)channel);
|
|
}
|
|
|
|
public void SendMessage(short msgType)
|
|
{
|
|
SendMessage(msgType, new PlayerMsg(ID));
|
|
}
|
|
|
|
public void StartGame(NetworkMessage msg)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|