/// <summary> /// Step 4/4 of joining a lobby. /// /// This will cause this script to handle the logic of being in a lobby such as fetching /// the true lobby state from the server, and sending your lobby changes. /// </summary> public void JoinLobby_ToggleLobbyClientLogic(bool isOn) { this.TrueLobbyState = new LobbyStatePacket(true); this.toggle_InLobbyClientLogic = isOn; if (!isOn) { if (NwMgr.NetworkHost != null) { NwMgr.NetworkHost.Disconnected -= new ConnectionDisconnectedHandler(ClientDisconnectEvent); } this.inLobbyClientLogicCurStatus = inLobbyClientLogicStatus.NotStarted; } else { NwMgr.NetworkHost.Disconnected -= new ConnectionDisconnectedHandler(ClientDisconnectEvent); NwMgr.NetworkHost.Disconnected += new ConnectionDisconnectedHandler(ClientDisconnectEvent); this.inLobbyClientLogicCurStatus = inLobbyClientLogicStatus.DelayingStart; } }
/// <summary> /// As a client of the lobby, handle any received-packet events from the host of the lobby. /// </summary> protected void InLobbyClientLogic() { // Need to delay a bit before transferring packets. NwMgr needs to establish connection event after calling Run() // before sending packets. if (inLobbyClientLogicCurStatus == inLobbyClientLogicStatus.NotStarted) { inLobbyClientLogicStartDelayFinish = Time.time + inLobbyClientLogicStartDelayTime; inLobbyClientLogicCurStatus = inLobbyClientLogicStatus.DelayingStart; return; } else if (inLobbyClientLogicCurStatus == inLobbyClientLogicStatus.DelayingStart) { if (Time.time > inLobbyClientLogicStartDelayFinish) { inLobbyClientLogicCurStatus = inLobbyClientLogicStatus.Started; } else { return; } } // If just joined the lobby, request a player id. if (myIdStatus == PlayerIdStatus.None) { if (IsDebug) { Debug.Log("InLobbyClientLogic: Requesting a player id."); } NwMgr.SendPacket <LobbyRequestPlayerIdPacket>("info", new LobbyRequestPlayerIdPacket()); myIdStatus = PlayerIdStatus.Requested; } // If received my requested player id. if (NwMgr.PacketQueue.HasPacket <LobbyGivenPlayerIdPacket>()) { LobbyGivenPlayerIdPacket recvPkt = NwMgr.PacketQueue.GetNextPacket <LobbyGivenPlayerIdPacket>(); myId = recvPkt.PlayerId; myIdStatus = PlayerIdStatus.Received; if (IsDebug) { Debug.Log("InLobbyClientLogic: Received my player id: " + myId); } } // Retrieved true lobby state from server. if (NwMgr.PacketQueue.HasPacket <LobbyStatePacket>()) { if (IsDebug) { Debug.Log("InLobbyClientLogic: Received lobby state from server."); } TrueLobbyState = NwMgr.PacketQueue.GetNextPacket <LobbyStatePacket>(); Menus.RefreshLobby(TrueLobbyState, myId); // Go in-game mode if signalled by server. if (TrueLobbyState.IsStartGame) { Debug.Log("Lobby.cs: Got packet to start game. Time: " + Time.time); Menus.OnClick_StartGame(); } } // Received alert that server is disconnecting. Exit the lobby. if (NwMgr.PacketQueue.HasPacket <LobbyPlayerDisconnectPacket>()) { if (IsDebug) { Debug.Log("InLobbyClientLogic: Received alert that server is disconnecting."); } NwMgr.PacketQueue.GetNextPacket <LobbyPlayerDisconnectPacket>(); Menus.OnClick_Disconnect("Server has ended the game."); return; } // Received signal to return to the lobby. if (NwMgr.PacketQueue.HasPacket <LobbyReturnPacket>()) { if (IsDebug) { Debug.Log("InLobbyClientLogic: Received signal to return to lobby."); } NwMgr.PacketQueue.GetNextPacket <LobbyReturnPacket>(); Menus.OnClick_ReturnToLobby(); } }