/// <summary> /// Updates the Entity. /// </summary> /// <param name="graphics">The graphics.</param> /// <param name="input">The input.</param> /// <param name="delta">The delta.</param> public void Update(GraphicsDevice graphics, Input input, long delta) { MenuScreen screen = Screens[SelectedMenuScreen]; int max = (Screens[SelectedMenuScreen] == Screens[MainMenuIndex]) ? screen.Elements.Count : screen.Elements.Count + 1; if (input.Down()) { if (_down == false) { _down = true; _menuMove.Play(); screen.SelectedIndex += 1; if (screen.SelectedIndex >= max) { screen.SelectedIndex = 0; } } } else { _down = false; } if (input.Up()) { if (_up == false) { _up = true; _menuMove.Play(); screen.SelectedIndex -= 1; if (screen.SelectedIndex < 0) { screen.SelectedIndex = max - 1; } } } else { _up = false; } if (input.Fire()) { if (_enter == false) { _enter = true; Action[] actions = screen.Elements.Values.ToArray(); if (screen.SelectedIndex == actions.Count()) { _menuBack.Play(); SelectedMenuScreen = screen.Parent == null ? MainMenuIndex : Screens.IndexOf(screen.Parent); } else { Action action = screen.Elements.Values.ToArray()[screen.SelectedIndex]; if (action != null) { _menuSelect.Play(); action(); } } } } else { _enter = false; } if (input.Escape()) { if (_escape == false) { _escape = true; if (screen.Parent != null) { _menuBack.Play(); SelectedMenuScreen = Screens.IndexOf(screen.Parent); } else if (screen != Screens[MainMenuIndex]) { _menuBack.Play(); SelectedMenuScreen = MainMenuIndex; } } } else { _escape = false; } }
/// <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); } }