Esempio n. 1
0
        /// <summary>
        /// Helper for updating a locally controlled gamer.
        /// </summary>
        void UpdateLocalGamer(LocalNetworkGamer gamer, GameTime gameTime,
                              bool sendPacketThisFrame)
        {
            // Look up what tank is associated with this local player.
            Tank tank = gamer.Tag as Tank;

            // Read the inputs controlling this tank.
            PlayerIndex playerIndex = gamer.SignedInGamer.PlayerIndex;

            Vector2 tankInput;
            Vector2 turretInput;

            ReadTankInputs(playerIndex, out tankInput, out turretInput);

            // Update the tank.
            tank.UpdateLocal(tankInput, turretInput);

            // Periodically send our state to everyone in the session.
            if (sendPacketThisFrame)
            {
                tank.WriteNetworkPacket(packetWriter, gameTime);

                gamer.SendData(packetWriter, SendDataOptions.InOrder);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// This method only runs on the server. It reads tank inputs that
        /// have been sent over the network by a client machine, storing
        /// them for later use by the UpdateServer method.
        /// </summary>
        void ServerReadPlayerFromClients(LocalNetworkGamer gamer)
        {
            // Keep reading as long as incoming packets are available.
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;

                sender = networkHelper.ReadServerData(gamer);

                // Read a single packet from the network.
                //gamer.ReceiveData(packetReader, out sender);

                if (!sender.IsLocal)
                {
                    // Look up the tank associated with whoever sent this packet.
                    Player remotePlayer = sender.Tag as Player;

                    while (networkHelper.ServerPacketReader.PeekChar() != -1)
                    {
                        char header = networkHelper.ServerPacketReader.ReadChar();
                        switch (header)
                        {
                        case 'P':
                            // Read the latest inputs controlling this tank.
                            remotePlayer.Position = networkHelper.ServerPacketReader.ReadVector3();
                            remotePlayer.Rotation = networkHelper.ServerPacketReader.ReadSingle();
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Helper for reading incoming network packets.
        /// </summary>
        void ReadIncomingPackets(LocalNetworkGamer gamer, GameTime gameTime)
        {
            // Keep reading as long as incoming packets are available.
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;

                // Read a single packet from the network.
                gamer.ReceiveData(packetReader, out sender);

                // Discard packets sent by local gamers: we already know their state!
                if (sender.IsLocal)
                {
                    continue;
                }

                // Look up the tank associated with whoever sent this packet.
                Tank tank = sender.Tag as Tank;

                // Estimate how long this packet took to arrive.
                TimeSpan latency = networkSession.SimulatedLatency +
                                   TimeSpan.FromTicks(sender.RoundtripTime.Ticks / 2);

                // Read the state of this tank from the network packet.
                tank.ReadNetworkPacket(packetReader, gameTime, latency,
                                       enablePrediction, enableSmoothing);
            }
        }
Esempio n. 4
0
        public void ServerReadInputFromClients(LocalNetworkGamer gamer)
        {
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;

                // Read a single packet from the network.
                gamer.ReceiveData(packetReader, out sender);

                if (!sender.IsLocal)
                {
                    // Look up the tank associated with whoever sent this packet.
                    BasicOrb orb = sender.Tag as BasicOrb;

                    Vector2 OrbPos = packetReader.ReadVector2();
                    orb.Position = new Vector3(OrbPos.X, orb.Position.Y, OrbPos.Y);

                    orb.Rotation = new Vector3(0, packetReader.ReadSingle(), 0);

                    orb.PrimaryWeaponQue   = packetReader.ReadInt32();
                    orb.SecondaryWeaponQue = packetReader.ReadInt32();
                    orb.AbilityQue         = packetReader.ReadInt32();
                    orb.GunCurrent         = packetReader.ReadInt32();
                    orb.Abilty[0]          = packetReader.ReadInt32();
                    orb.Team  = packetReader.ReadInt32();
                    orb.Alpha = packetReader.ReadSingle();

                    orb.IsPhasing     = packetReader.ReadBoolean();
                    orb.PhaseTimer    = packetReader.ReadInt32();
                    orb.PhaseVelocity = packetReader.ReadVector3();
                    orb.MyController.MoveStickTrack = packetReader.ReadVector2();
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Helper for reading incoming network packets.
        /// </summary>
        void ReadIncomingPackets(LocalNetworkGamer gamer)
        {
            // Keep reading as long as incoming packets are available.
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;

                // Read a single packet from the network.
                gamer.ReceiveData(packetReader, out sender);

                // Discard packets sent by local gamers: we already know their state!
                if (sender.IsLocal)
                {
                    continue;
                }

                // Look up the ship associated with whoever sent this packet.
                Racer racer = sender.Tag as Racer;

                // Read the state of this ship from the network packet.
                try
                {
                    racer.shipPhysics.ShipPosition = packetReader.ReadVector3();
                    //tempShip.ShipYaw = packetReader.ReadSingle();
                    //tempShip.shipSpeed = packetReader.ReadSingle();//EndOfStreamException was unhandled.
                    //roll??
                }
                catch (EndOfStreamException e)
                {
                    //Do something here
                    returnToMenu();
                }
            }
        }
Esempio n. 6
0
        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.
            // Tank localTank = gamer.Tag as Tank;
            Ship localShip = gamer.Tag as Ship;

            // ReadTankInputs(localTank, gamer.SignedInGamer.PlayerIndex);
            // checkInput();

            // 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(localTank.TankInput);
                // packetWriter.Write(localTank.TurretInput);

                // send messages across?
                // packetWriter.Write(

                // Send our input data to the server.
                // gamer.SendData(packetWriter,
                //              SendDataOptions.InOrder, networkSession.Host);
            }
        }
        private static void ReadPackets(LocalNetworkGamer gamer)
        {
#if XBOX
            while (gamer.IsDataAvailable)
            {
                if (GameManager.GetLevel() == null)
                {
                    NetworkGamer sender;

                    gamer.ReceiveData(packetReader, out sender);

                    Level        NewLevel = new Level(false);
                    BinaryReader b        = new BinaryReader(packetReader.BaseStream);
                    NewLevel.Read(b);
                    b.Close();

                    GameManager.SetLevel(NewLevel);
                    break;
                }
            }
#endif
#if WINDOWS
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;
                gamer.ReceiveData(packetReader, out sender);
                ShouldSendLevel = true;
            }
#endif
        }
Esempio n. 8
0
 public static void Disconnect()
 {
     if (session != null)
     {
         if (Main.netMode != 1)
         {
             if (session.SessionState == NetworkSessionState.Playing)
             {
                 session.EndGame();
                 session.Update();
             }
             clients.Clear();
             for (int num = 7; num >= 0; num--)
             {
                 playerSlots[num] = false;
             }
         }
         DisposeSession();
         gamer = null;
         gamersWaitingForPlayerId.Clear();
         gamersWaitingToSendSpawn.Clear();
         gamersWaitingToSpawn.Clear();
         for (int i = 0; i < 4; i++)
         {
             Main.ui[i].LeaveSession();
         }
     }
     disconnect   = false;
     hookEvents   = false;
     stopSession  = false;
     Main.netMode = 0;
 }
Esempio n. 9
0
 private void setupSinglePlayer()
 {
     if (Global.input.isAnyFirstPress(Buttons.A) && !Guide.IsVisible)
     {
         Guide.ShowSignIn(1, true);
     }
     else if (SignedInGamer.SignedInGamers.Count > 0 && Global.input.isAnyFirstPress(Buttons.Start))
     {
         Global.numLocalGamers             = (byte)SignedInGamer.SignedInGamers.Count;
         Global.networkManager.sessionType = NetworkSessionType.Local;
         if (Global.networkManager.createSession())
         {
             timeInSinglePlayer = 0;
             Global.levelManager.currentLevel = 1;
             Global.levelManager.setupLevel();
             LocalNetworkGamer gamer = Global.networkManager.networkSession.LocalGamers[0];
             gamer.Tag = new LocalPlayer(Vector3.Zero, PlayerIndex.One, 1, gamer);
             Global.localPlayers.Add((LocalPlayer)gamer.Tag);
             Global.localPlayers[0].setAsJuggernaut();
             Global.menusong.Stop();
             Global.actionsong.Play();
             Global.gameState = Global.GameState.SinglePlayerPlaying;
         }
     }
     else if (Global.input.isFirstPress(Buttons.B))
     {
         Global.gameState = Global.GameState.Menu;
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Helper for reading incoming network packets.
        /// </summary>
        void ReadIncomingPackets(LocalNetworkGamer gamer)
        {
            // Keep reading as long as incoming packets are available.
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;

                // Read a single packet from the network.
                gamer.ReceiveData(packetReader, out sender);

                // Discard packets sent by local gamers: we already know their state!
                if (sender.IsLocal)
                {
                    continue;
                }

                // Look up the tank associated with whoever sent this packet.
                Tank remoteTank = sender.Tag as Tank;

                // Read the state of this tank from the network packet.
                remoteTank.Position       = packetReader.ReadVector2();
                remoteTank.TankRotation   = packetReader.ReadSingle();
                remoteTank.TurretRotation = packetReader.ReadSingle();
            }
        }
Esempio n. 11
0
        /// <summary>
        /// This method only runs on client machines. It reads
        /// tank position data that has been computed by the server.
        /// </summary>
        void ClientReadGameStateFromServer(LocalNetworkGamer gamer)
        {
            // Keep reading as long as incoming packets are available.
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;

                // Read a single packet from the network.
                gamer.ReceiveData(packetReader, out sender);

                // If a player has recently joined or left, it is possible the server
                // might have sent information about a different number of players
                // than the client currently knows about. If so, we will be unable
                // to match up which data refers to which player. The solution is
                // just to ignore the packet for now: this situation will resolve
                // itself as soon as the client gets the join/leave notification.
                if (networkSession.AllGamers.Count != packetReader.ReadInt32())
                {
                    continue;
                }

                // This packet contains data about all the players in the session.
                foreach (NetworkGamer remoteGamer in networkSession.AllGamers)
                {
                    NetworkPlayer ship = remoteGamer.Tag as NetworkPlayer;

                    // Read the state of this tank from the network packet.
                    ship.Position = packetReader.ReadVector2();
                }
            }
        }
Esempio n. 12
0
        public LocalPlayer(Vector3 pos, PlayerIndex index, int localIndex, LocalNetworkGamer associatedGamer)
            : base(Global.game, pos, Vector3.Zero, Global.Constants.PLAYER_RADIUS)
        {
            localPlayerIndex        = localIndex;
            timeSinceLastPacketSent = (localIndex - 1) * 2;
            score           = 0;
            health          = Global.Constants.MAX_HEALTH;
            isJuggernaut    = false;
            jetpackDisabled = false;
            lastFiringTime  = 0;
            jetFuel         = Global.Constants.MAX_JET_FUEL;
            gunCoolDownModeNoShootingPermitted = false;

            sphere            = new Sphere(Global.game, Global.Constants.DEFAULT_PLAYER_COLOR, pos);
            sphere.localScale = Matrix.CreateScale(Global.Constants.PLAYER_RADIUS);
            sphere.SetWireframe(1);
            Texture2D blankTexture = Global.game.Content.Load <Texture2D>(@"Textures\blankTexture");

            cube = new Cube(blankTexture, Color.Black);
            cubeTransformation = Matrix.CreateScale(1, 1, gunLength) * Matrix.CreateTranslation(new Vector3(radius, 0, gunLength));
            cube.wireFrame     = false;
            cube.textured      = false;

            gamer       = associatedGamer;
            playerIndex = gamer.SignedInGamer.PlayerIndex;
            setupViewport();
        }
Esempio n. 13
0
        public NetworkGamer ReadClientData(LocalNetworkGamer gamer)
        {
            NetworkGamer sender;

            // Read a single packet from the network
            gamer.ReceiveData(networkHelper.ClientPacketReader, out sender);
            return(sender);
        }
Esempio n. 14
0
        /// <summary>
        /// Read server data
        /// </summary>
        public NetworkGamer ReadServerData(LocalNetworkGamer gamer)
        {
            NetworkGamer sender;

            // Read a single packet from the network.
            gamer.ReceiveData(ServerPacketReader, out sender);
            return(sender);
        }
Esempio n. 15
0
        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="playerInput"></param>
        internal LiveIdentifiedPlayer(PlayerInput playerInput, LocalNetworkGamer localNetworkGamer)
            : base(playerInput)
        {
            LiveGamer   = localNetworkGamer;
            UniqueId    = localNetworkGamer.Gamertag;
            DisplayName = localNetworkGamer.Gamertag;

            IsHost = localNetworkGamer.IsHost;
        }
Esempio n. 16
0
        /// <summary>
        /// Constructs a new lobby screen.
        /// </summary>
        public LobbyScreen(NetworkSession networkSession)
        {
            this.networkSession = networkSession;
            localGamer          = networkSession.LocalGamers[0];
            localGamer.SetLocalPlayerCount(0);

            TransitionOnTime  = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }
 private void UpdateLocalGamer(LocalNetworkGamer gamer)
 {
     // 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)
     {
         _packetWriter.Write((byte)NetworkMessageType.PlayerMove);
     }
 }
Esempio n. 18
0
        /// <summary>
        /// Send all server data
        /// </summary>
        public void SendServerData()
        {
            if (networkHelper.serverPacketWriter.Length > 0)
            {
                // Send the combined data to everyone in the session
                LocalNetworkGamer server = (LocalNetworkGamer)networkHelper.session.Host;

                server.SendData(networkHelper.serverPacketWriter, SendDataOptions.InOrder);
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Send the local gamer's local player count to network.
        /// </summary>
        public static void SendToNetwork(this LocalNetworkGamer localGamer)
        {
            PacketWriter writer = new PacketWriter();

            writer.Write(PacketHeader.LOBBY_DATA);
            writer.Write(localGamer.GetLocalPlayerCount());

            // send in order, since old data must not overwrite the new data.
            localGamer.SendData(writer, SendDataOptions.InOrder);
        }
Esempio n. 20
0
 /// <summary>
 /// Handle MenuCancel inputs by clearing our ready status, or if it is
 /// already clear, prompting if the user wants to leave the session.
 /// </summary>
 void HandleMenuCancel(LocalNetworkGamer gamer)
 {
     if (gamer.IsReady)
     {
         gamer.IsReady = false;
     }
     else
     {
         NetworkSessionComponent.LeaveSessionFromGame(ScreenManager, audioManager); //audioHelper);
     }
 }
Esempio n. 21
0
 /// <summary>
 /// Handle MenuCancel inputs by clearing our ready status, or if it is
 /// already clear, prompting if the user wants to leave the session.
 /// </summary>
 void HandleMenuCancel(LocalNetworkGamer gamer)
 {
     if (gamer.IsReady)
     {
         gamer.IsReady = false;
     }
     else
     {
         NetworkSessionComponent.LeaveSession(ScreenManager);
     }
 }
Esempio n. 22
0
        /// <summary>
        /// Handle MenuCancel inputs by clearing our ready status, or if it is
        /// already clear, prompting if the user wants to leave the session.
        /// </summary>
        void HandleMenuCancel(LocalNetworkGamer gamer)
        {
            if (gamer.IsReady)
            {
                gamer.IsReady = false;
            }
            else
            {
                PlayerIndex playerIndex = gamer.SignedInGamer.PlayerIndex;

                NetworkSessionComponent.LeaveSession(ScreenManager, playerIndex);
            }
        }
Esempio n. 23
0
        /// <summary>
        /// This method only runs on client machines. It reads
        /// tank position data that has been computed by the server.
        /// </summary>
        void ClientReadGameStateFromServer(LocalNetworkGamer gamer)
        {
            // Keep reading as long as incoming packets are available.
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;

                sender = networkHelper.ReadClientData(gamer);
                // Read a single packet from the network.
                //gamer.ReceiveData(packetReader, out sender);

                if (!sender.IsLocal)
                {
                    while (networkHelper.ServerPacketReader.PeekChar() != -1)
                    {
                        char header = networkHelper.ServerPacketReader.ReadChar();
                        switch (header)
                        {
                        case 'P':
                            // This packet contains data about all the players in the session.
                            foreach (NetworkGamer remoteGamer in networkSession.AllGamers)
                            {
                                Player player = remoteGamer.Tag as Player;

                                // Read the state of this tank from the network packet.
                                player.Position = networkHelper.ClientPacketReader.ReadVector3();
                                player.Rotation = networkHelper.ClientPacketReader.ReadSingle();
                            }
                            break;

                        case 'N':
                            // If a player has recently joined or left, it is possible the server
                            // might have sent information about a different number of players
                            // than the client currently knows about. If so, we will be unable
                            // to match up which data refers to which player. The solution is
                            // just to ignore the packet for now: this situation will resolve
                            // itself as soon as the client gets the join/leave notification.
                            if (networkSession.AllGamers.Count != networkHelper.ClientPacketReader.ReadInt32())
                            {
                                continue;
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }
        public NetworkBehavior(Entity parent, string type, float timeBetween, int id)
            : base(parent)
        {
            this.Id   = id;
            this.Type = type;

            _player = GameMain.CurrentSession.LocalGamers[0];

            _timeBetween = _timeElapsed = timeBetween;

            GameMain.Connection.AddBehavior(this);

            _isUnique = false;
        }
Esempio n. 25
0
        protected virtual void UpdateLocalGamer(LocalNetworkGamer gamer, GameTime gameTime, bool sendPacketThisFrame)
        {
            Player p = gamer.Tag as Player;

            if (p != null)
            {
                //p.UpdateLocalPlayer(gameTime);

                // Periodically send our state to everyone in the session.
                if (sendPacketThisFrame)
                {
                    p.WriteNetworkPacket(packetWriter, gameTime);
                    gamer.SendData(packetWriter, SendDataOptions.InOrder);
                }
            }
        }
Esempio n. 26
0
        private List <PickupItem> ReadRemoteItems(LocalNetworkGamer gamer)
        {
            List <PickupItem> itemList = new List <PickupItem>();
            int        itemID;
            int        ammoCount;
            int        spawnID;
            int        team;
            Vector2    itemPos;
            PickupItem item;

            while (gamer.IsDataAvailable)
            {
                if (_sender.IsHost)
                {
                    itemID  = _packetReader.ReadInt32();
                    itemPos = _packetReader.ReadVector2();
                    spawnID = _packetReader.ReadInt32();

                    item = getItem(itemID);
                    if (item != null)
                    {
                        item.Position = itemPos;
                        if (itemID > 10)
                        {
                            ammoCount = _packetReader.ReadInt32();
                            ((Weapon)item).AmmoCount = ammoCount;
                        }
                        else if (itemID == 1)
                        {
                            team = _packetReader.ReadInt32();
                            ((Flag)item).Team = team;
                        }
                        item.SpawnID = spawnID;
                        itemList.Add(item);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            return(itemList);
        }
        protected void ProcessIncomingData(GameTime gameTime)
        {
            LocalNetworkGamer localGamer = networkSession.LocalGamers[0];

            while (localGamer.IsDataAvailable)
            {
                NetworkGamer sender;
                localGamer.ReceiveData(reader, out sender);

                if (!sender.IsLocal)
                {
                    MessageType type = (MessageType)reader.ReadInt32();
                    switch (type)
                    {
                    case MessageType.StartGame:
                        StartGame();
                        break;

                    case MessageType.EndGame:
                        EndGame();
                        break;

                    case MessageType.RejoinLobby:
                        RejoinLobby();
                        break;

                    case MessageType.UpdatePlayerPos:
                        UpdateOtherPlayer(gameTime);
                        break;

                    case MessageType.RestartGame:
                        RestartGame();
                        break;

                    case MessageType.AddBomb:
                        //packet reader will, after reading message type, receive a vector2 of position of bomb
                        AddBomb(reader.ReadVector2());
                        break;

                    case MessageType.HitBomb:
                        //packet reader will, after reading message type, receive a int of index of bomb in bombList
                        HitBomb(reader.ReadInt32());
                        break;
                    }
                }
            }
        }
Esempio n. 28
0
        protected virtual void ReadIncomingPackets(LocalNetworkGamer gamer, GameTime gameTime)
        {
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;
                gamer.ReceiveData(packetReader, out sender);
                if (!sender.IsLocal && sender.Tag != null)
                {
                    Player   p       = sender.Tag as Player;
                    TimeSpan latency = NetworkManager.Session.SimulatedLatency +
                                       TimeSpan.FromTicks(sender.RoundtripTime.Ticks / 2);

                    // Read the state of this tank from the network packet.
                    p.ReadNetworkPacket(packetReader, gameTime, latency);
                }
            }
        }
Esempio n. 29
0
        public void UpdateServer()
        {
            Int32 GameMode = -1;
            Int32 Map      = -1;

            packetWriter.Write(game.GameOver);
            packetWriter.Write((Int32)(game.Winner != null ? game.Winner.ID : 0));

            if (game.menus.IsInGame)
            {
                packetWriter.Write((Int32)game.gamemode);
                packetWriter.Write((Int32)game.map);
            }
            else
            {
                packetWriter.Write((Int32)(-1));
                packetWriter.Write((Int32)(-1));
            }

            packetWriter.Write(GameMode);
            packetWriter.Write(Map);

            foreach (BasicOrb orb in game.Orbs)
            {
                packetWriter.Write((bool)orb.relevent);
            }

            foreach (BasicOrb orb in game.Orbs)
            {
                if (orb.relevent)
                {
                    orb.Write(packetWriter);
                }
            }

            if (game.gamemode == Game1.GameMode.KeepAway)
            {
                packetWriter.Write(game.flag.IsCarried);
                packetWriter.Write(game.flag.Position);
                packetWriter.Write(game.flag.carrier.ID);
            }

            LocalNetworkGamer server = (LocalNetworkGamer)networkSession.Host;

            server.SendData(packetWriter, SendDataOptions.InOrder);
        }
        protected void Update_Start(GameTime gameTime)
        {
            //local gamers is a list of gamers
            LocalNetworkGamer localGamer = networkSession.LocalGamers[0];

            if (networkSession.AllGamers.Count == 2)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    writer.Write((int)MessageType.StartGame);
                    localGamer.SendData(writer, SendDataOptions.Reliable);

                    StartGame();
                }
            }
            ProcessIncomingData(gameTime);
        }