private void Update() { if (Input.GetMouseButtonDown(0)) { isDragging = true; } if (Input.GetMouseButtonUp(0)) { isDragging = false; } if (isDragging && playerInitialized) { // The game server is server authoritative. That is, it is the // "source of truth", so only update the UI based on the information // it sends from the GameStateUpdatePacket in response to PlayerUpdateStatePackets // it gets from each player. syncServer.SendPlayerPosition(ScreenToWorldPosition(new Vector2(Input.mousePosition.x, Input.mousePosition.y))); } // Process all packets that have been received by the server thus far byte[] data; while (syncServer.GetNextPacket(out data)) { var packet = PacketFactory.BytesToPacket(data); var byteBuffer = new ByteBuffer(data); switch ((Opcode)packet.Opcode) { case Opcode.Success: Debug.Log("Success packet received"); break; case Opcode.GameStateUpdate: OnGameStateUpdated(GameStateUpdatePacket.GetRootAsGameStateUpdatePacket(byteBuffer)); break; case Opcode.MatchSuccess: // All players have entered the match, the game can now be played. // set the tickRate so that we can properly interpolate animations on the client side tickRate = MatchSuccessPacket.GetRootAsMatchSuccessPacket(byteBuffer).TickRate; Debug.Log("Match found, joining... Tick Rate: " + tickRate); OnMatchReady(); break; case Opcode.GameOver: OnGameOver(GameOverPacket.GetRootAsGameOverPacket(byteBuffer)); break; default: Debug.Log($"Packet received with Opcode={(Opcode)packet.Opcode}"); break; } } // If we've initialized our players, lets start interpolating between circlesPreviousPositions and circlesDesiredPositions if (playerInitialized) { DrawAllCircles(); } }
private void Update() { if (doAbort) { doAbort = false; AbortGame(); } if (didConnectSuccessfully) { didConnectSuccessfully = false; OnConnected(); } if (doAttemptConnect) { doAttemptConnect = false; OnAttemptingReconnect(); } if (!client.IsConnected && tickCount > 0 && !UserData.Instance.IsGameOver) { matchInfoDisplay.GameState = "Disconnected"; } // Process all packets that have been received by the server thus far byte[] data; while (client.GetNextPacket(out data)) { if (UserData.Instance.IsGameOver) { return; } var packet = PacketFactory.BytesToPacket(data); var byteBuffer = new ByteBuffer(data); // Uncomment for logs containing which packet type you receive // Debug.Log("SyncGameController: Received packet: " + (Opcode)packet.Opcode); switch ((Opcode)packet.Opcode) { case Opcode.MatchSuccess: client.ResetReadTimer(); on(MatchSuccess.GetRootAsMatchSuccess(byteBuffer)); client.SetReadTimeout(2000); break; case Opcode.GameState: on(GameState.GetRootAsGameState(byteBuffer)); break; case Opcode.MatchOver: on(MatchOver.GetRootAsMatchOver(byteBuffer)); break; case Opcode.OpponentConnectionStatus: on(OpponentConnectionStatus.GetRootAsOpponentConnectionStatus(byteBuffer)); break; case Opcode.PlayerReconnected: on(PlayerReconnected.GetRootAsPlayerReconnected(byteBuffer)); break; case Opcode.OpponentPaused: on(OpponentPaused.GetRootAsOpponentPaused(byteBuffer)); break; case Opcode.OpponentResumed: on(OpponentResumed.GetRootAsOpponentResumed(byteBuffer)); break; case Opcode.Chat: on(Chat.GetRootAsChat(byteBuffer)); break; case Opcode.KeepAlive: break; default: Debug.Log("SyncGameController: Received packet with unimplemented/unsupported authcode: " + packet.Opcode); break; } } }