/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } bgScroll.Update(gameTime); bgScroll2.Update(gameTime); if (gameState == GameState.Menu) { textCounter--; if (textCounter <= 0) { textCounter = MaxRespawnCounter; textEnable = !textEnable; } if (InputHandler.IsKeyPressed(Keys.Space)) { gameState = GameState.Play; } } else if (gameState == GameState.Play) { if (ship.IsDead) { respawnCounter--; if (respawnCounter < 0) { ship.Center = new Vector2(Width / 2, Height / 2); ship.Velocity = Vector2.Zero; ship.IsDead = false; } } else { if (InputHandler.IsKeyDown(Keys.Left)) { ship.Rotation -= 0.05f; } if (InputHandler.IsKeyDown(Keys.Right)) { ship.Rotation += 0.05f; } ship.Thrust = InputHandler.IsKeyDown(Keys.Up); if (InputHandler.IsKeyDown(Keys.LeftControl)) { ShotManager.AddShot(ship.Center + ship.Heading * ship.Radius, ship.Heading * 3); } } ship.Update(gameTime); CollisionDetection(); ShotManager.Update(gameTime); AsteroidsManager.Update(gameTime); ExplosionsManager.Update(gameTime); } base.Update(gameTime); }
public static void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); GamePadState padState = GamePad.GetState(PlayerIndex.One); switch (AsteroidsGame.ControlStyle) { case ControlStyles.KBM_RH: { shipSprite.Velocity = Vector2.Zero; // Movement if (keyState.IsKeyDown(Keys.W)) { shipSprite.Velocity += new Vector2(0, -1); } if (keyState.IsKeyDown(Keys.S)) { shipSprite.Velocity += new Vector2(0, 1); } if (keyState.IsKeyDown(Keys.A)) { shipSprite.Velocity += new Vector2(-1, 0); } if (keyState.IsKeyDown(Keys.D)) { shipSprite.Velocity += new Vector2(1, 0); } shipSprite.Velocity.Normalize(); shipSprite.Velocity *= playerSpeed; fireShotsMouse(mouseState, elapsed); pointToMouseCursor(mouseState); } break; case ControlStyles.KBM_LH: { shipSprite.Velocity = Vector2.Zero; // Movement if (keyState.IsKeyDown(Keys.O)) { shipSprite.Velocity += new Vector2(0, -1); } if (keyState.IsKeyDown(Keys.L)) { shipSprite.Velocity += new Vector2(0, 1); } if (keyState.IsKeyDown(Keys.K)) { shipSprite.Velocity += new Vector2(-1, 0); } if (keyState.IsKeyDown(Keys.OemSemicolon)) { shipSprite.Velocity += new Vector2(1, 0); } shipSprite.Velocity.Normalize(); shipSprite.Velocity *= playerSpeed; fireShotsMouse(mouseState, elapsed); pointToMouseCursor(mouseState); } break; case ControlStyles.GamePad: { shipSprite.Velocity = Vector2.Zero; shipSprite.Velocity = new Vector2( padState.ThumbSticks.Left.X, -padState.ThumbSticks.Left.Y); shipSprite.Velocity.Normalize(); shipSprite.Velocity *= playerSpeed; fireShotsGamePad(padState, elapsed); shipSprite.RotateTo(new Vector2( padState.ThumbSticks.Right.X, padState.ThumbSticks.Right.Y) - shipSprite.Position); } break; } imposeMovementLimits(); shipSprite.Update(gameTime); shotManager.Update(gameTime); }