Exemplo n.º 1
0
 void Update()
 {
     player.Update();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Reference page contains links to related conceptual articles.
        /// </summary>
        /// <param name="gameTime">Time passed since the last call to Update.</param>
        protected override void Update(GameTime gameTime)
        {
            long delta      = gameTime.ElapsedGameTime.Milliseconds;
            var  inputState = new Input(Keyboard.GetState(), GamePad.GetState(PlayerIndex.One));

            if (_running)
            {
                if (inputState.Escape())
                {
                    _running = false;
                    _menu.SelectedMenuScreen = _menu.MainMenuIndex;
                    return;
                }

                _player.Update(GraphicsDevice, inputState, delta);

                _lastFire += delta;

                if (inputState.Fire())
                {
                    if (_lastFire > _fireTime.Milliseconds)
                    {
                        _lastFire = 0;
                        _shootSound.Play();
                        AddProjectile();
                    }
                }

                UpdateCollisions();
                for (int i = _projectiles.Count - 1; i >= 0; i--)
                {
                    _projectiles[i].Update(GraphicsDevice, inputState, delta);

                    if (_projectiles[i].Active == false)
                    {
                        _projectiles.RemoveAt(i);
                    }
                }


                for (int i = _explosions.Count - 1; i >= 0; i--)
                {
                    _explosions[i].Update(GraphicsDevice, inputState, delta);

                    if (_explosions[i].Active == false)
                    {
                        _explosions.RemoveAt(i);
                    }
                }

                for (int i = _enemies.Count - 1; i >= 0; i--)
                {
                    _enemies[i].Update(GraphicsDevice, inputState, delta);

                    if (_enemies[i].Active == false)
                    {
                        _enemies.RemoveAt(i);
                    }
                    else
                    {
                        if (_enemies[i].LastFired > _fireTime.Milliseconds)
                        {
                            _enemies[i].LastFired = 0;
                            double angle;
                            if (_enemies[i].Accurate)
                            {
                                angle = Math.Atan2(_player.Position.Y - _enemies[i].Position.Y,
                                                   _player.Position.X - _enemies[i].Position.X);
                            }
                            else
                            {
                                angle = _rand.NextDouble() * MathHelper.TwoPi;
                            }

                            Vector2 position = _enemies[i].Position +
                                               new Vector2((float)(32 * Math.Cos(angle)), (float)(32 * Math.Sin(angle)));

                            var projectile = new Projectile();
                            projectile.Initialize(GraphicsDevice.Viewport, _projectileTexture, position, angle,
                                                  ProjectileMoveSpeed, true);
                            _projectiles.Add(projectile);
                        }
                    }
                }

                UpdateAsteroids(delta, inputState);
                base.Update(gameTime);
            }
            else
            {
                _menu.Update(GraphicsDevice, inputState, delta);
            }
        }
Exemplo n.º 3
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));
                }
            }
        }