Example #1
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;
 }
Example #2
0
 /// <summary>
 /// Event handler for when the Return to Lobby menu entry is selected.
 /// </summary>
 void ReturnToLobbyMenuEntrySelected(object sender, PlayerIndexEventArgs e)
 {
     if (networkSession.SessionState == NetworkSessionState.Playing)
     {
         networkSession.EndGame();
     }
 }
Example #3
0
 /// <summary>
 /// Ends the Session and changes its SessionState from Playing to Ended
 /// </summary>
 public override void EndSession()
 {
     if (_networkSession.IsHost)
     {
         _networkSession.EndGame();
         OnEnded();
     }
 }
Example #4
0
        public void LeaveSession()
        {
            if (networkSession != null)
            {
                if (networkSession.SessionState == NetworkSessionState.Playing)
                {
                    networkSession.EndGame();
                }

                if (networkSession.IsHost)
                {
                    networkSession.Dispose();
                }

                networkSession = null;
            }
        }
Example #5
0
        /// <summary>
        /// Handles "Exit" menu item selection
        /// </summary>
        ///
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            // Tear down our network session
            NetworkSession session = ScreenManager.Game.Services.GetService(typeof(NetworkSession)) as NetworkSession;

            if (session != null)
            {
                if (session.AllGamers.Count == 1)
                {
                    session.EndGame();
                }
                session.Dispose();
                ScreenManager.Game.Services.RemoveService(typeof(NetworkSession));
            }
            AudioManager.StopSounds();
            ScreenManager.AddScreen(new MainMenuScreen(), null);
            ExitScreen();
        }
Example #6
0
        public void Dispose()
        {
            if (_netSession.IsHost)
            {
                _netSession.EndGame();
            }

            _netSession.Dispose();
            _netSession        = null;
            _availableSessions = null;
            _hasLeft           = true;
            _remoteMapName     = "";
            _packetReader.BaseStream.Flush();
            _packetWriter.BaseStream.Flush();
            _remoteItemList  = null;
            _remoteMatch     = null;
            _remoteMatchType = 999;
            _sender          = null;
            _sessionList.Clear();
            _isHost   = false;
            _isClient = false;
            _failure  = false;
        }
        protected override void Update(GameTime gameTime)
        {
            GamePadState padState = GamePad.GetState(PlayerIndex.One);

            if (padState.Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            KeyboardState keybState = Keyboard.GetState();

            if (this.IsActive)
            {
                switch (currentGameState)
                {
                case GameState.SignIn:
                {
                    if (Gamer.SignedInGamers.Count < 1)
                    {
                        Guide.ShowSignIn(1, false);
                        log.Add("Opened User SignIn Interface");
                    }
                    else
                    {
                        currentGameState = GameState.SearchSession;
                        log.Add(Gamer.SignedInGamers[0].Gamertag + " logged in - proceed to SearchSession");
                    }
                }
                break;

                case GameState.SearchSession:
                {
                    NetworkSessionProperties findProperties = new NetworkSessionProperties();
                    findProperties[0] = 3;
                    findProperties[1] = 4096;

                    AvailableNetworkSessionCollection activeSessions = NetworkSession.Find(NetworkSessionType.SystemLink, 4, findProperties);
                    if (activeSessions.Count == 0)
                    {
                        currentGameState = GameState.CreateSession;
                        log.Add("No active sessions found - proceed to CreateSession");
                    }
                    else
                    {
                        AvailableNetworkSession sessionToJoin = activeSessions[0];
                        networkSession = NetworkSession.Join(sessionToJoin);

                        string myString = "Joined session hosted by " + sessionToJoin.HostGamertag;
                        myString += " with " + sessionToJoin.CurrentGamerCount.ToString() + " players";
                        myString += " and " + sessionToJoin.OpenPublicGamerSlots.ToString() + " open player slots.";
                        log.Add(myString);

                        HookSessionEvents();
                        command          = "[Press X to signal you're ready]";
                        currentGameState = GameState.InSession;
                    }
                }
                break;

                case GameState.CreateSession:
                {
                    NetworkSessionProperties createProperties = new NetworkSessionProperties();
                    createProperties[0] = 3;
                    createProperties[1] = 4096;

                    networkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 4, 16, 0, createProperties);
                    networkSession.AllowHostMigration  = true;
                    networkSession.AllowJoinInProgress = false;
                    log.Add("New session created");

                    HookSessionEvents();

                    command          = "[Press X to signal you're ready]";
                    currentGameState = GameState.InSession;
                }
                break;

                case GameState.InSession:
                {
                    switch (networkSession.SessionState)
                    {
                    case NetworkSessionState.Lobby:
                    {
                        if ((keybState != lastKeybState) || (padState != lastPadState))
                        {
                            if (keybState.IsKeyDown(Keys.X) || (padState.IsButtonDown(Buttons.X)))
                            {
                                LocalNetworkGamer localGamer = networkSession.LocalGamers[0];
                                localGamer.IsReady = !localGamer.IsReady;
                            }
                        }

                        if (networkSession.IsHost)
                        {
                            if (networkSession.AllGamers.Count > 1)
                            {
                                if (networkSession.IsEveryoneReady)
                                {
                                    networkSession.StartGame();
                                    log.Add("All players ready -- start the game!");
                                }
                            }
                        }
                    }
                    break;

                    case NetworkSessionState.Playing:
                    {
                        if (networkSession.IsHost)
                        {
                            if ((keybState != lastKeybState) || (padState != lastPadState))
                            {
                                if (keybState.IsKeyDown(Keys.Y) || (padState.IsButtonDown(Buttons.Y)))
                                {
                                    networkSession.EndGame();
                                }
                            }
                        }
                    }
                    break;
                    }

                    networkSession.Update();
                }
                break;
                }
            }

            lastKeybState = keybState;

            base.Update(gameTime);
        }
Example #8
0
 public virtual void EndGame()
 {
     Session.EndGame();
 }
Example #9
0
        /// <summary>
        /// Updates the state of the game.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                    bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (coveredByOtherScreen)
            {
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            }
            else
            {
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
            }

            if (IsActive)
            {
                starField.Update(gameTime);

                if (networkSession != null)
                {
                    // Process Incoming Packets
                    ReceiveNetworkData();

                    if (networkSession.IsHost)
                    {
                        LocalNetworkGamer gamer = networkSession.Host as LocalNetworkGamer;

                        if (updatesSinceGameDataSend >= updatesBetweenGameDataPackets)
                        {
                            updatesSinceGameDataSend = 0;

                            SendGameData();
                        }
                        else
                        {
                            updatesSinceGameDataSend++;
                        }
                    }

                    // Update players
                    foreach (NetworkGamer gamer in networkSession.AllGamers)
                    {
                        Player p = gamer.Tag as Player;

                        if (p != null)
                        {
                            p.Update(gameTime);

                            if (gamer.IsLocal && p.wasKilled)
                            {
                                // Reset the death flag
                                p.wasKilled = false;

                                SendLocalShipDeath();
                            }
                        }
                    }

                    // Send Player Data
                    if (updatesSincePlayerDataSend >= updatesBetweenPlayerDataPackets)
                    {
                        updatesSincePlayerDataSend = 0;

                        SendLocalPlayerData();
                    }
                    else
                    {
                        updatesSincePlayerDataSend++;
                    }

                    // Send Ship Data
                    if (updatesSinceStatusPacket >= updatesBetweenStatusPackets)
                    {
                        updatesSinceStatusPacket = 0;

                        SendLocalShipData();
                    }
                    else
                    {
                        updatesSinceStatusPacket++;
                    }
                }
                else
                {
                    players[(int)ControllingPlayer].Update(gameTime);
                }

                // Update Asteroids
                asteroidManager.Update(gameTime);

                // See if we need to increment the level
                if (asteroidManager.Asteroids.Count == 0)
                {
                    asteroidManager.StartLevel(level++);

                    // Enable Spawn Protection
                    if (networkSession == null)
                    {
                        players[0].EnableSpawnProtection();
                    }
                    else
                    {
                        foreach (NetworkGamer gamer in networkSession.AllGamers)
                        {
                            Player p = gamer.Tag as Player;

                            if (p != null)
                            {
                                p.EnableSpawnProtection();
                            }
                        }
                    }
                }

                // Handle collision detection
                if (networkSession == null)
                {
                    CheckCollisions();
                }
                else
                {
                    CheckMultiplayerCollisions();
                }
            }

            // Check for gameOver state
            if (networkSession != null)
            {
                bool shouldReturnToLobby = true;

                foreach (NetworkGamer gamer in networkSession.AllGamers)
                {
                    Player p = gamer.Tag as Player;

                    if (p.IsGameOver == false)
                    {
                        shouldReturnToLobby = false;
                    }
                }

                // If all players are in the gameOver state then return to the lobby
                if (networkSession.IsHost && shouldReturnToLobby)
                {
                    if (networkSession.SessionState == NetworkSessionState.Playing)
                    {
                        networkSession.EndGame();
                    }
                }
            }

            // If we are in a network game, check if we should return to the lobby.
            if ((networkSession != null) && !IsExiting)
            {
                if (networkSession.SessionState == NetworkSessionState.Lobby)
                {
                    LoadingScreen.Load(ScreenManager, false, null,
                                       new TitleBackgroundScreen(),
                                       new LobbyScreen(networkSession));
                }
            }
        }