Ejemplo n.º 1
0
        /// <summary>
        /// Helper for updating a locally controlled gamer.
        /// </summary>
        void UpdateLocalGamer(LocalNetworkGamer gamer)
        {
            // Look up what tank is associated with this local player,
            // and read the latest user inputs for it. The server will
            // later use these values to control the tank movement.
            NetworkPlayer localPlayer = gamer.Tag as NetworkPlayer;

            localPlayer.Update();
            // Only send if we are not the server. There is no point sending packets
            // to ourselves, because we already know what they will contain!
            if (!networkSession.IsHost)
            {
                // Write our latest input state into a network packet.
                packetWriter.Write(localPlayer.Position);

                // Send our input data to the server.
                gamer.SendData(packetWriter,
                               SendDataOptions.InOrder, networkSession.Host);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method only runs on the server. It calls Update on all the
        /// tank instances, both local and remote, using inputs that have
        /// been received over the network. It then sends the resulting
        /// tank position data to everyone in the session.
        /// </summary>
        void UpdateServer(GameTime gameTime)
        {
            // First off, our packet will indicate how many tanks it has data for.
            packetWriter.Write(networkSession.AllGamers.Count);

            // Loop over all the players in the session, not just the local ones!
            foreach (NetworkGamer gamer in networkSession.AllGamers)
            {
                // Look up what tank is associated with this player.
                NetworkPlayer player = gamer.Tag as NetworkPlayer;

                // Update the tank.
                player.Update();

                // Write the tank state into the output network packet.
                packetWriter.Write(player.Position);
            }

            // Send the combined data for all tanks to everyone in the session.
            LocalNetworkGamer server = (LocalNetworkGamer)networkSession.Host;

            server.SendData(packetWriter, SendDataOptions.InOrder);
        }