GameplayScreen.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameplayScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Threading;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Input;
  16. #endregion
  17. namespace GameStateManagement
  18. {
  19. /// <summary>
  20. /// This screen implements the actual game logic. It is just a
  21. /// placeholder to get the idea across: you'll probably want to
  22. /// put some more interesting gameplay in here!
  23. /// </summary>
  24. class GameplayScreen : GameScreen
  25. {
  26. #region Fields
  27. ContentManager content;
  28. SpriteFont gameFont;
  29. Vector2 playerPosition = new Vector2(100, 100);
  30. Vector2 enemyPosition = new Vector2(100, 100);
  31. Random random = new Random();
  32. float pauseAlpha;
  33. #endregion
  34. #region Initialization
  35. /// <summary>
  36. /// Constructor.
  37. /// </summary>
  38. public GameplayScreen()
  39. {
  40. TransitionOnTime = TimeSpan.FromSeconds(1.5);
  41. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  42. }
  43. /// <summary>
  44. /// Load graphics content for the game.
  45. /// </summary>
  46. public override void LoadContent()
  47. {
  48. if (content == null)
  49. content = new ContentManager(ScreenManager.Game.Services, "Content");
  50. gameFont = content.Load<SpriteFont>("gamefont");
  51. // A real game would probably have more content than this sample, so
  52. // it would take longer to load. We simulate that by delaying for a
  53. // while, giving you a chance to admire the beautiful loading screen.
  54. Thread.Sleep(1000);
  55. // once the load has finished, we use ResetElapsedTime to tell the game's
  56. // timing mechanism that we have just finished a very long frame, and that
  57. // it should not try to catch up.
  58. ScreenManager.Game.ResetElapsedTime();
  59. }
  60. /// <summary>
  61. /// Unload graphics content used by the game.
  62. /// </summary>
  63. public override void UnloadContent()
  64. {
  65. content.Unload();
  66. }
  67. #endregion
  68. #region Update and Draw
  69. /// <summary>
  70. /// Updates the state of the game. This method checks the GameScreen.IsActive
  71. /// property, so the game will stop updating when the pause menu is active,
  72. /// or if you tab away to a different application.
  73. /// </summary>
  74. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  75. bool coveredByOtherScreen)
  76. {
  77. base.Update(gameTime, otherScreenHasFocus, false);
  78. // Gradually fade in or out depending on whether we are covered by the pause screen.
  79. if (coveredByOtherScreen)
  80. pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
  81. else
  82. pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
  83. if (IsActive)
  84. {
  85. // Apply some random jitter to make the enemy move around.
  86. const float randomization = 10;
  87. enemyPosition.X += (float)(random.NextDouble() - 0.5) * randomization;
  88. enemyPosition.Y += (float)(random.NextDouble() - 0.5) * randomization;
  89. // Apply a stabilizing force to stop the enemy moving off the screen.
  90. Vector2 targetPosition = new Vector2(
  91. ScreenManager.GraphicsDevice.Viewport.Width / 2 - gameFont.MeasureString("Insert Gameplay Here").X / 2,
  92. 200);
  93. enemyPosition = Vector2.Lerp(enemyPosition, targetPosition, 0.05f);
  94. // TODO: this game isn't very fun! You could probably improve
  95. // it by inserting something more interesting in this space :-)
  96. }
  97. }
  98. /// <summary>
  99. /// Lets the game respond to player input. Unlike the Update method,
  100. /// this will only be called when the gameplay screen is active.
  101. /// </summary>
  102. public override void HandleInput(InputState input)
  103. {
  104. if (input == null)
  105. throw new ArgumentNullException("input");
  106. // Look up inputs for the active player profile.
  107. int playerIndex = (int)ControllingPlayer.Value;
  108. KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
  109. GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
  110. // The game pauses either if the user presses the pause button, or if
  111. // they unplug the active gamepad. This requires us to keep track of
  112. // whether a gamepad was ever plugged in, because we don't want to pause
  113. // on PC if they are playing with a keyboard and have no gamepad at all!
  114. bool gamePadDisconnected = !gamePadState.IsConnected &&
  115. input.GamePadWasConnected[playerIndex];
  116. if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
  117. {
  118. ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
  119. }
  120. else
  121. {
  122. // Otherwise move the player position.
  123. Vector2 movement = Vector2.Zero;
  124. if (keyboardState.IsKeyDown(Keys.Left))
  125. movement.X--;
  126. if (keyboardState.IsKeyDown(Keys.Right))
  127. movement.X++;
  128. if (keyboardState.IsKeyDown(Keys.Up))
  129. movement.Y--;
  130. if (keyboardState.IsKeyDown(Keys.Down))
  131. movement.Y++;
  132. Vector2 thumbstick = gamePadState.ThumbSticks.Left;
  133. movement.X += thumbstick.X;
  134. movement.Y -= thumbstick.Y;
  135. if (movement.Length() > 1)
  136. movement.Normalize();
  137. playerPosition += movement * 2;
  138. }
  139. }
  140. /// <summary>
  141. /// Draws the gameplay screen.
  142. /// </summary>
  143. public override void Draw(GameTime gameTime)
  144. {
  145. // This game has a blue background. Why? Because!
  146. ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
  147. Color.CornflowerBlue, 0, 0);
  148. // Our player and enemy are both actually just text strings.
  149. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  150. spriteBatch.Begin();
  151. spriteBatch.DrawString(gameFont, "// TODO", playerPosition, Color.Green);
  152. spriteBatch.DrawString(gameFont, "Insert Gameplay Here",
  153. enemyPosition, Color.DarkRed);
  154. spriteBatch.End();
  155. // If the game is transitioning on or off, fade it out to black.
  156. if (TransitionPosition > 0 || pauseAlpha > 0)
  157. {
  158. float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
  159. ScreenManager.FadeBackBufferToBlack(alpha);
  160. }
  161. }
  162. #endregion
  163. }
  164. }