GameplayScreen.cs 7.1 KB

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