GameplayScreen.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.Collections.Generic;
  12. using System.Text;
  13. using GameStateManagement;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Input;
  16. using CardsFramework;
  17. using Microsoft.Xna.Framework.GamerServices;
  18. using Microsoft.Xna.Framework.Input.Touch;
  19. #endregion
  20. namespace Blackjack
  21. {
  22. class GameplayScreen : GameScreen
  23. {
  24. #region Fields and Properties
  25. BlackjackCardGame blackJackGame;
  26. InputHelper inputHelper;
  27. string theme;
  28. List<DrawableGameComponent> pauseEnabledComponents = new List<DrawableGameComponent>();
  29. List<DrawableGameComponent> pauseVisibleComponents = new List<DrawableGameComponent>();
  30. Rectangle safeArea;
  31. static Vector2[] playerCardOffset = new Vector2[]
  32. {
  33. new Vector2(100f * BlackjackGame.WidthScale, 190f * BlackjackGame.HeightScale),
  34. new Vector2(336f * BlackjackGame.WidthScale, 210f * BlackjackGame.HeightScale),
  35. new Vector2(570f * BlackjackGame.WidthScale, 190f * BlackjackGame.HeightScale)
  36. };
  37. #endregion
  38. #region Initiaizations
  39. /// <summary>
  40. /// Initializes a new instance of the screen.
  41. /// </summary>
  42. public GameplayScreen(string theme)
  43. {
  44. TransitionOnTime = TimeSpan.FromSeconds(0.0);
  45. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  46. #if WINDOWS_PHONE
  47. EnabledGestures = GestureType.Tap;
  48. #endif
  49. this.theme = theme;
  50. }
  51. #endregion
  52. #region Loading
  53. /// <summary>
  54. /// Load content and initializes the actual game.
  55. /// </summary>
  56. public override void LoadContent()
  57. {
  58. safeArea = ScreenManager.SafeArea;
  59. // Initialize virtual cursor
  60. inputHelper = new InputHelper(ScreenManager.Game);
  61. inputHelper.DrawOrder = 1000;
  62. ScreenManager.Game.Components.Add(inputHelper);
  63. // Ignore the curser when not run in Xbox
  64. #if !XBOX
  65. inputHelper.Visible = false;
  66. inputHelper.Enabled = false;
  67. #endif
  68. blackJackGame = new BlackjackCardGame(ScreenManager.GraphicsDevice.Viewport.Bounds,
  69. new Vector2(safeArea.Left + safeArea.Width / 2 - 50, safeArea.Top + 20),
  70. GetPlayerCardPosition, ScreenManager, theme);
  71. InitializeGame();
  72. base.LoadContent();
  73. }
  74. /// <summary>
  75. /// Unload content loaded by the screen.
  76. /// </summary>
  77. public override void UnloadContent()
  78. {
  79. ScreenManager.Game.Components.Remove(inputHelper);
  80. base.UnloadContent();
  81. }
  82. #endregion
  83. #region Update and Render
  84. /// <summary>
  85. /// Handle user input.
  86. /// </summary>
  87. /// <param name="input">User input information.</param>
  88. public override void HandleInput(InputState input)
  89. {
  90. if (input.IsPauseGame(null))
  91. {
  92. PauseCurrentGame();
  93. }
  94. base.HandleInput(input);
  95. }
  96. /// <summary>
  97. /// Perform the screen's update logic.
  98. /// </summary>
  99. /// <param name="gameTime">The time that has passed since the last call to
  100. /// this method.</param>
  101. /// <param name="otherScreenHasFocus">Whether or not another screen has
  102. /// the focus.</param>
  103. /// <param name="coveredByOtherScreen">Whether or not another screen covers
  104. /// this one.</param>
  105. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  106. {
  107. #if XBOX
  108. if (Guide.IsVisible)
  109. {
  110. PauseCurrentGame();
  111. }
  112. #endif
  113. if (blackJackGame != null && !coveredByOtherScreen)
  114. {
  115. blackJackGame.Update(gameTime);
  116. }
  117. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  118. }
  119. /// <summary>
  120. /// Draw the screen
  121. /// </summary>
  122. /// <param name="gameTime"></param>
  123. public override void Draw(GameTime gameTime)
  124. {
  125. base.Draw(gameTime);
  126. if (blackJackGame != null)
  127. {
  128. blackJackGame.Draw(gameTime);
  129. }
  130. }
  131. #endregion
  132. #region Private Methods
  133. /// <summary>
  134. /// Initializes the game component.
  135. /// </summary>
  136. private void InitializeGame()
  137. {
  138. blackJackGame.Initialize();
  139. // Add human player
  140. blackJackGame.AddPlayer(new BlackjackPlayer("Abe", blackJackGame));
  141. // Add AI players
  142. BlackjackAIPlayer player = new BlackjackAIPlayer("Benny", blackJackGame);
  143. blackJackGame.AddPlayer(player);
  144. player.Hit += player_Hit;
  145. player.Stand += player_Stand;
  146. player = new BlackjackAIPlayer("Chuck", blackJackGame);
  147. blackJackGame.AddPlayer(player);
  148. player.Hit += player_Hit;
  149. player.Stand += player_Stand;
  150. // Load UI assets
  151. string[] assets = { "blackjack", "bust", "lose", "push", "win", "pass", "shuffle_" + theme };
  152. for (int chipIndex = 0; chipIndex < assets.Length; chipIndex++)
  153. {
  154. blackJackGame.LoadUITexture("UI", assets[chipIndex]);
  155. }
  156. blackJackGame.StartRound();
  157. }
  158. /// <summary>
  159. /// Gets the player hand positions according to the player index.
  160. /// </summary>
  161. /// <param name="player">The player's index.</param>
  162. /// <returns>The position for the player's hand on the game table.</returns>
  163. private Vector2 GetPlayerCardPosition(int player)
  164. {
  165. switch (player)
  166. {
  167. case 0:
  168. case 1:
  169. case 2:
  170. return new Vector2(ScreenManager.SafeArea.Left,
  171. ScreenManager.SafeArea.Top + 200 * (BlackjackGame.HeightScale - 1)) +
  172. playerCardOffset[player];
  173. default:
  174. throw new ArgumentException(
  175. "Player index should be between 0 and 2", "player");
  176. }
  177. }
  178. /// <summary>
  179. /// Pause the game.
  180. /// </summary>
  181. private void PauseCurrentGame()
  182. {
  183. // Move to the pause screen
  184. ScreenManager.AddScreen(new BackgroundScreen(), null);
  185. ScreenManager.AddScreen(new PauseScreen(), null);
  186. // Hide and disable all components which are related to the gameplay screen
  187. pauseEnabledComponents.Clear();
  188. pauseVisibleComponents.Clear();
  189. foreach (IGameComponent component in ScreenManager.Game.Components)
  190. {
  191. if (component is BetGameComponent ||
  192. component is AnimatedGameComponent ||
  193. component is GameTable ||
  194. component is InputHelper)
  195. {
  196. DrawableGameComponent pauseComponent = (DrawableGameComponent)component;
  197. if (pauseComponent.Enabled)
  198. {
  199. pauseEnabledComponents.Add(pauseComponent);
  200. pauseComponent.Enabled = false;
  201. }
  202. if (pauseComponent.Visible)
  203. {
  204. pauseVisibleComponents.Add(pauseComponent);
  205. pauseComponent.Visible = false;
  206. }
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// Returns from pause.
  212. /// </summary>
  213. public void ReturnFromPause()
  214. {
  215. // Reveal and enable all previously hidden components
  216. foreach (DrawableGameComponent component in pauseEnabledComponents)
  217. {
  218. component.Enabled = true;
  219. }
  220. foreach (DrawableGameComponent component in pauseVisibleComponents)
  221. {
  222. component.Visible = true;
  223. }
  224. }
  225. #endregion
  226. #region Event Handler
  227. /// <summary>
  228. /// Responds to the event sent when AI player's choose to "Stand".
  229. /// </summary>
  230. /// <param name="sender">The source of the event.</param>
  231. /// <param name="e">The
  232. /// <see cref="System.EventArgs"/> instance containing the event data.</param>
  233. void player_Stand(object sender, EventArgs e)
  234. {
  235. blackJackGame.Stand();
  236. }
  237. /// <summary>
  238. /// Responds to the event sent when AI player's choose to "Split".
  239. /// </summary>
  240. /// <param name="sender">The source of the event.</param>
  241. /// <param name="e">The
  242. /// <see cref="System.EventArgs"/> instance containing the event data.</param>
  243. void player_Split(object sender, EventArgs e)
  244. {
  245. blackJackGame.Split();
  246. }
  247. /// <summary>
  248. /// Responds to the event sent when AI player's choose to "Hit".
  249. /// </summary>
  250. /// <param name="sender">The source of the event.</param>
  251. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  252. void player_Hit(object sender, EventArgs e)
  253. {
  254. blackJackGame.Hit();
  255. }
  256. /// <summary>
  257. /// Responds to the event sent when AI player's choose to "Double".
  258. /// </summary>
  259. /// <param name="sender">The source of the event.</param>
  260. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  261. void player_Double(object sender, EventArgs e)
  262. {
  263. blackJackGame.Double();
  264. }
  265. #endregion
  266. }
  267. }