/// <summary> /// Process the input for this ship, from the gamepad assigned to it. /// </summary> /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param> /// <para public virtual void ProcessInput(float elapsedTime, bool overlayPresent) { currentGamePadState = GamePad.GetState(playerIndex); currentKeyboardState = Keyboard.GetState(); if (overlayPresent == false) { if (playing == false) { // trying to join - update the a-button timer if ((currentGamePadState.Buttons.A == ButtonState.Pressed) || (currentKeyboardState.IsKeyDown(Keys.Z) && playerIndex == PlayerIndex.One) || (currentKeyboardState.IsKeyDown(Keys.M) && playerIndex == PlayerIndex.Two)) { aButtonTimer += elapsedTime; } else { aButtonTimer = 0f; } // if the timer has exceeded the expected value, join the game if (aButtonTimer > aButtonHeldToPlay) { JoinGame(); } } else { // check if we're trying to leave if ((currentGamePadState.Buttons.B == ButtonState.Pressed) || (currentKeyboardState.IsKeyDown(Keys.X) && playerIndex == PlayerIndex.One) || (currentKeyboardState.IsKeyDown(Keys.N) && playerIndex == PlayerIndex.Two)) { bButtonTimer += elapsedTime; } else { bButtonTimer = 0f; } // if the timer has exceeded the expected value, leave the game if (bButtonTimer > bButtonHeldToLeave) { LeaveGame(); } else if (dead == false) { // // the ship is alive, so process movement and firing // // calculate the current forward vector Vector2 forward = new Vector2((float)Math.Sin(Rotation), -(float)Math.Cos(Rotation)); Vector2 right = new Vector2(-forward.Y, forward.X); // calculate the current left stick value Vector2 leftStick = currentGamePadState.ThumbSticks.Left; leftStick.Y *= -1f; if (leftStick.LengthSquared() > 0f) { Vector2 wantedForward = Vector2.Normalize(leftStick); float angleDiff = (float)Math.Acos( Vector2.Dot(wantedForward, forward)); float facing = (Vector2.Dot(wantedForward, right) > 0f) ? 1f : -1f; if (angleDiff > 0f) { Rotation += Math.Min(angleDiff, facing * elapsedTime * rotationRadiansPerSecond); } // add velocity Velocity += leftStick * (elapsedTime * speed); if (Velocity.Length() > velocityLengthMaximum) { Velocity = Vector2.Normalize(Velocity) * velocityLengthMaximum; } } else if (currentKeyboardState != null) { if (playerIndex == PlayerIndex.One) { // Rotate Left if (currentKeyboardState.IsKeyDown(Keys.A)) { Rotation -= elapsedTime * rotationRadiansPerSecond; } // Rotate Right if (currentKeyboardState.IsKeyDown(Keys.D)) { Rotation += elapsedTime * rotationRadiansPerSecond; } //create some velocity if the right trigger is down Vector2 shipVelocityAdd = Vector2.Zero; //now scale our direction by how hard/long the trigger/keyboard is down if (currentKeyboardState.IsKeyDown(Keys.W)) { //find out what direction we should be thrusting, using rotation shipVelocityAdd.X = (float)Math.Sin(Rotation); shipVelocityAdd.Y = (float)-Math.Cos(Rotation); shipVelocityAdd = shipVelocityAdd / elapsedTime * MathHelper.ToRadians(9.0f); } //finally, add this vector to our velocity. Velocity += shipVelocityAdd; // Lets fire our weapon if (currentKeyboardState.IsKeyDown(Keys.Tab)) { // fire ahead of us weapon.Fire(Vector2.Normalize(forward)); } // Lets drop some Mines if (currentKeyboardState.IsKeyDown(Keys.S)) { // fire behind the ship mineWeapon.Fire(-forward); } } if (playerIndex == PlayerIndex.Two) { // Rotate Left if (currentKeyboardState.IsKeyDown(Keys.Left)) { Rotation -= elapsedTime * rotationRadiansPerSecond; } // Rotate Right if (currentKeyboardState.IsKeyDown(Keys.Right)) { Rotation += elapsedTime * rotationRadiansPerSecond; } //create some velocity if the right trigger is down Vector2 shipVelocityAdd = Vector2.Zero; //now scale our direction by how hard/long the trigger/keyboard is down if (currentKeyboardState.IsKeyDown(Keys.Up)) { //find out what direction we should be thrusting, using rotation shipVelocityAdd.X = (float)Math.Sin(Rotation); shipVelocityAdd.Y = (float)-Math.Cos(Rotation); shipVelocityAdd = shipVelocityAdd / elapsedTime * MathHelper.ToRadians(9.0f); } //finally, add this vector to our velocity. Velocity += shipVelocityAdd; // Lets drop some Mines if (currentKeyboardState.IsKeyDown(Keys.RightControl)) { // fire ahead of us weapon.Fire(Vector2.Normalize(forward)); } // Lets drop some Mines if (currentKeyboardState.IsKeyDown(Keys.Down)) { // fire behind the ship mineWeapon.Fire(-forward); } } } // check for firing with the right stick Vector2 rightStick = currentGamePadState.ThumbSticks.Right; rightStick.Y *= -1f; if (rightStick.LengthSquared() > fireThresholdSquared) { weapon.Fire(Vector2.Normalize(rightStick)); } if (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed) { // fire ahead of us weapon.Fire(Vector2.Normalize(forward)); } // check for laying mines if ((currentGamePadState.Buttons.B == ButtonState.Pressed) && (lastGamePadState.Buttons.B == ButtonState.Released)) { // fire behind the ship mineWeapon.Fire(-forward); } } } } // update the gamepad state lastGamePadState = currentGamePadState; lastKeyboardState = currentKeyboardState; return; }
/// <summary> /// Process the input for this ship, from the gamepad assigned to it. /// </summary> /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param> /// <para public virtual void ProcessInput(float elapsedTime, bool overlayPresent) { currentGamePadState = GamePad.GetState(playerIndex); if (overlayPresent == false) { if (playing == false) { // trying to join - update the a-button timer if (currentGamePadState.Buttons.A == ButtonState.Pressed) { aButtonTimer += elapsedTime; } else { aButtonTimer = 0f; } // if the timer has exceeded the expected value, join the game if (aButtonTimer > aButtonHeldToPlay) { JoinGame(); } } else { // check if we're trying to leave if (currentGamePadState.Buttons.B == ButtonState.Pressed) { bButtonTimer += elapsedTime; } else { bButtonTimer = 0f; } // if the timer has exceeded the expected value, leave the game if (bButtonTimer > bButtonHeldToLeave) { LeaveGame(); } else if (dead == false) { // // the ship is alive, so process movement and firing // // calculate the current forward vector Vector2 forward = new Vector2((float)Math.Sin(Rotation), -(float)Math.Cos(Rotation)); Vector2 right = new Vector2(-forward.Y, forward.X); // calculate the current left stick value Vector2 leftStick = currentGamePadState.ThumbSticks.Left; leftStick.Y *= -1f; if (leftStick.LengthSquared() > 0f) { Vector2 wantedForward = Vector2.Normalize(leftStick); float angleDiff = (float)Math.Acos( Vector2.Dot(wantedForward, forward)); float facing = (Vector2.Dot(wantedForward, right) > 0f) ? 1f : -1f; if (angleDiff > 0f) { Rotation += Math.Min(angleDiff, facing * elapsedTime * rotationRadiansPerSecond); } // add velocity Velocity += leftStick * (elapsedTime * speed); if (Velocity.Length() > velocityLengthMaximum) { Velocity = Vector2.Normalize(Velocity) * velocityLengthMaximum; } } // check for firing with the right stick Vector2 rightStick = currentGamePadState.ThumbSticks.Right; rightStick.Y *= -1f; if (rightStick.LengthSquared() > fireThresholdSquared) { weapon.Fire(Vector2.Normalize(rightStick)); } // check for laying mines if ((currentGamePadState.Buttons.B == ButtonState.Pressed) && (lastGamePadState.Buttons.B == ButtonState.Released)) { // fire behind the ship mineWeapon.Fire(-forward); } } } } // update the gamepad state lastGamePadState = currentGamePadState; return; }