GameplayScreen.cs 7.8 KB

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