private void OnClientAuthenticated(NetPeer peer, NetEventArgs args) { PacketAuth message = PacketBase.Read <PacketAuth>((NetDataReader)(args.Data)); PlayerManager.SetIdentity(new Player(message.ID, username)); NetEventManager.Trigger(NetEventType.Authenticated, null); }
private void OnConnect(NetPeer peer, NetEventArgs args) { NetEventManager.Trigger(NetEventType.Connected, args); PacketBase message = new PacketAuth(0, username, 0); GameClient.CurrentState = ClientState.Connected; GameClient.Send(message); }
/// <summary> /// Calls the disconnect procedure from the network client and triggers a disconnection event. /// The function iterates through every resettable component, invoking the reset method. /// </summary> public static void Disconnect() { foreach (IResettable comp in instance.gameObject.GetComponents <IResettable>()) { comp.Reset(); } instance.baseClient.Disconnect(); DisconnectInfo info = new DisconnectInfo(); info.Reason = DisconnectReason.DisconnectPeerCalled; NetEventManager.Trigger(NetEventType.Disconnected, new NetEventArgs(info)); }
private void OnPlayerReady(NetPeer peer, NetEventArgs args) { if (GameClient.CurrentState == ClientState.Game) { return; } PacketPlayerReady message = PacketBase.Read <PacketPlayerReady>((NetDataReader)(args.Data)); Player player = null; if (players.TryGetValue(message.Sender, out player)) { player.SetReady(message.Value); NetEventManager.Trigger(NetEventType.PlayerReady, args); } }
/// <summary> /// Handler for "player enter" packets. /// </summary> /// <param name="peer">server</param> /// <param name="args">raw data containing the player packet</param> private void OnPlayerEnter(NetPeer peer, NetEventArgs args) { if (GameClient.CurrentState == ClientState.Game) { return; } PacketPlayerEnter message = PacketBase.Read <PacketPlayerEnter>((NetDataReader)(args.Data)); if (message.Player.ID == identity.ID) { SetIdentity(message.Player); } Add(message.Player); NetEventManager.Trigger(NetEventType.PlayerEnter, args); }
/// <summary> /// Handler for 'player leaving' packets. /// Removes the player if the game is not started yet, otherwise mark him as 'disconnected'. /// </summary> /// <param name="peer">server</param> /// <param name="args">wrapper around raw data containing the packet</param> private void OnPlayerLeave(NetPeer peer, NetEventArgs args) { PacketPlayerEnter message = PacketBase.Read <PacketPlayerEnter>((NetDataReader)(args.Data)); if (message.Player.ID == Identity.ID) { UnityEngine.Debug.LogWarning("Dafuq, I can't leave myself!"); return; } if (GameClient.CurrentState != ClientState.Game) { Remove(message.Player); } else { SetDisconnected(message.Player.ID); LockstepLogic.Instance.UpdatePlayersCount(activePlayers, message.Player.ID); } NetEventManager.Trigger(NetEventType.PlayerLeave, args); }
private void OnReceiveGameInfo(NetPeer peer, NetEventArgs args) { //TODO deserialize game info NetEventManager.Trigger(NetEventType.LoggedIn, null); GameClient.CurrentState = ClientState.Lobby; }
public void LeaveLobby() { NetEventManager.Trigger(NetEventType.LoggedOut, null); GameClient.Disconnect(); }
/// <summary> /// Function called on receiving a network error. /// </summary> /// <param name="args">object containing the error code</param> private void OnNetworkError(NetPeer peer, NetEventArgs args) { NetEventManager.Trigger(NetEventType.NetworkError, args); }
/// <summary> /// Function called when losing connection from the server. /// </summary> /// <param name="args">object containing data about the disconnection</param> private void OnDisconnect(NetPeer peer, NetEventArgs args) { PlayerManager.Instance.Reset(); NetEventManager.Trigger(NetEventType.Disconnected, args); }