/// <summary> /// Reads input data from keyboard and gamepad, and stores /// it into the specified tank object. /// </summary> void ReadTankInputs(Tank tank, PlayerIndex playerIndex) { // Read the gamepad. GamePadState gamePad = GamePad.GetState(playerIndex); Vector2 tankInput = gamePad.ThumbSticks.Left; Vector2 turretInput = gamePad.ThumbSticks.Right; // Read the keyboard. KeyboardState keyboard = Keyboard.GetState(playerIndex); if (keyboard.IsKeyDown(Keys.Left)) { tankInput.X = -1; } else if (keyboard.IsKeyDown(Keys.Right)) { tankInput.X = 1; } if (keyboard.IsKeyDown(Keys.Up)) { tankInput.Y = 1; } else if (keyboard.IsKeyDown(Keys.Down)) { tankInput.Y = -1; } if (keyboard.IsKeyDown(Keys.A)) { turretInput.X = -1; } else if (keyboard.IsKeyDown(Keys.D)) { turretInput.X = 1; } if (keyboard.IsKeyDown(Keys.W)) { turretInput.Y = 1; } else if (keyboard.IsKeyDown(Keys.S)) { turretInput.Y = -1; } // Normalize the input vectors. if (tankInput.Length() > 1) { tankInput.Normalize(); } if (turretInput.Length() > 1) { turretInput.Normalize(); } // Store these input values into the tank object. tank.TankInput = tankInput; tank.TurretInput = turretInput; }
/// <summary> /// Reads input data from keyboard and gamepad, and stores /// it into the specified tank object. /// </summary> void ReadTankInputs(Tank tank, PlayerIndex playerIndex) { // Read the gamepad. GamePadState gamePad = GamePad.GetState(playerIndex); Vector2 tankInput = gamePad.ThumbSticks.Left; Vector2 turretInput = gamePad.ThumbSticks.Right; // Read the keyboard. KeyboardState keyboard = Keyboard.GetState(playerIndex); if (keyboard.IsKeyDown(Keys.Left)) tankInput.X = -1; else if (keyboard.IsKeyDown(Keys.Right)) tankInput.X = 1; if (keyboard.IsKeyDown(Keys.Up)) tankInput.Y = 1; else if (keyboard.IsKeyDown(Keys.Down)) tankInput.Y = -1; if (keyboard.IsKeyDown(Keys.A)) turretInput.X = -1; else if (keyboard.IsKeyDown(Keys.D)) turretInput.X = 1; if (keyboard.IsKeyDown(Keys.W)) turretInput.Y = 1; else if (keyboard.IsKeyDown(Keys.S)) turretInput.Y = -1; // Normalize the input vectors. if (tankInput.Length() > 1) tankInput.Normalize(); if (turretInput.Length() > 1) turretInput.Normalize(); // Store these input values into the tank object. tank.TankInput = tankInput; tank.TurretInput = turretInput; }