LobbyScreen.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // LobbyScreen.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 Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.Input;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Net;
  16. #endregion
  17. namespace NetworkStateManagement
  18. {
  19. /// <summary>
  20. /// The lobby screen provides a place for gamers to congregate before starting
  21. /// the actual gameplay. It displays a list of all the gamers in the session,
  22. /// and indicates which ones are currently talking. Each gamer can press a button
  23. /// to mark themselves as ready: gameplay will begin after everyone has done this.
  24. /// </summary>
  25. class LobbyScreen : GameScreen
  26. {
  27. #region Fields
  28. NetworkSession networkSession;
  29. Texture2D isReadyTexture;
  30. Texture2D hasVoiceTexture;
  31. Texture2D isTalkingTexture;
  32. Texture2D voiceMutedTexture;
  33. #endregion
  34. #region Initialization
  35. /// <summary>
  36. /// Constructs a new lobby screen.
  37. /// </summary>
  38. public LobbyScreen (NetworkSession networkSession)
  39. {
  40. this.networkSession = networkSession;
  41. TransitionOnTime = TimeSpan.FromSeconds (0.5);
  42. TransitionOffTime = TimeSpan.FromSeconds (0.5);
  43. }
  44. /// <summary>
  45. /// Loads graphics content used by the lobby screen.
  46. /// </summary>
  47. public override void LoadContent ()
  48. {
  49. ContentManager content = ScreenManager.Game.Content;
  50. isReadyTexture = content.Load<Texture2D> ("chat_ready");
  51. hasVoiceTexture = content.Load<Texture2D> ("chat_able");
  52. isTalkingTexture = content.Load<Texture2D> ("chat_talking");
  53. voiceMutedTexture = content.Load<Texture2D> ("chat_mute");
  54. }
  55. #endregion
  56. #region Update
  57. /// <summary>
  58. /// Updates the lobby screen.
  59. /// </summary>
  60. public override void Update (GameTime gameTime, bool otherScreenHasFocus,
  61. bool coveredByOtherScreen)
  62. {
  63. base.Update (gameTime, otherScreenHasFocus, coveredByOtherScreen);
  64. if (!IsExiting) {
  65. if (networkSession.SessionState == NetworkSessionState.Playing) {
  66. // Check if we should leave the lobby and begin gameplay.
  67. // We pass null as the controlling player, because the networked
  68. // gameplay screen accepts input from any local players who
  69. // are in the session, not just a single controlling player.
  70. LoadingScreen.Load (ScreenManager, true, null,
  71. new GameplayScreen (networkSession));
  72. } else if (networkSession.IsHost && networkSession.IsEveryoneReady) {
  73. // The host checks whether everyone has marked themselves
  74. // as ready, and starts the game in response.
  75. networkSession.StartGame ();
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// Handles user input for all the local gamers in the session. Unlike most
  81. /// screens, which use the InputState class to combine input data from all
  82. /// gamepads, the lobby needs to individually mark specific players as ready,
  83. /// so it loops over all the local gamers and reads their inputs individually.
  84. /// </summary>
  85. public override void HandleInput (InputState input)
  86. {
  87. foreach (LocalNetworkGamer gamer in networkSession.LocalGamers) {
  88. PlayerIndex playerIndex = gamer.SignedInGamer.PlayerIndex;
  89. PlayerIndex unwantedOutput;
  90. if (input.IsMenuSelect (playerIndex, out unwantedOutput)) {
  91. HandleMenuSelect (gamer);
  92. } else if (input.IsMenuCancel (playerIndex, out unwantedOutput)) {
  93. HandleMenuCancel (gamer);
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// Handle MenuSelect inputs by marking ourselves as ready.
  99. /// </summary>
  100. void HandleMenuSelect (LocalNetworkGamer gamer)
  101. {
  102. if (!gamer.IsReady) {
  103. gamer.IsReady = true;
  104. } else if (gamer.IsHost) {
  105. // The host has an option to force starting the game, even if not
  106. // everyone has marked themselves ready. If they press select twice
  107. // in a row, the first time marks the host ready, then the second
  108. // time we ask if they want to force start.
  109. MessageBoxScreen messageBox = new MessageBoxScreen (
  110. Resources.ConfirmForceStartGame);
  111. messageBox.Accepted += ConfirmStartGameMessageBoxAccepted;
  112. ScreenManager.AddScreen (messageBox, gamer.SignedInGamer.PlayerIndex);
  113. }
  114. }
  115. /// <summary>
  116. /// Event handler for when the host selects ok on the "are you sure
  117. /// you want to start even though not everyone is ready" message box.
  118. /// </summary>
  119. void ConfirmStartGameMessageBoxAccepted (object sender, PlayerIndexEventArgs e)
  120. {
  121. if (networkSession.SessionState == NetworkSessionState.Lobby) {
  122. networkSession.StartGame ();
  123. }
  124. }
  125. /// <summary>
  126. /// Handle MenuCancel inputs by clearing our ready status, or if it is
  127. /// already clear, prompting if the user wants to leave the session.
  128. /// </summary>
  129. void HandleMenuCancel (LocalNetworkGamer gamer)
  130. {
  131. if (gamer.IsReady) {
  132. gamer.IsReady = false;
  133. } else {
  134. PlayerIndex playerIndex = gamer.SignedInGamer.PlayerIndex;
  135. NetworkSessionComponent.LeaveSession (ScreenManager, playerIndex);
  136. }
  137. }
  138. #endregion
  139. #region Draw
  140. /// <summary>
  141. /// Draws the lobby screen.
  142. /// </summary>
  143. public override void Draw (GameTime gameTime)
  144. {
  145. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  146. SpriteFont font = ScreenManager.Font;
  147. Vector2 position = new Vector2 (100, 150);
  148. // Make the lobby slide into place during transitions.
  149. float transitionOffset = (float)Math.Pow (TransitionPosition, 2);
  150. if (ScreenState == ScreenState.TransitionOn)
  151. position.X -= transitionOffset * 256;
  152. else
  153. position.X += transitionOffset * 512;
  154. spriteBatch.Begin ();
  155. // Draw all the gamers in the session.
  156. int gamerCount = 0;
  157. foreach (NetworkGamer gamer in networkSession.AllGamers) {
  158. DrawGamer (gamer, position);
  159. // Advance to the next screen position, wrapping into two
  160. // columns if there are more than 8 gamers in the session.
  161. if (++gamerCount == 8) {
  162. position.X += 433;
  163. position.Y = 150;
  164. } else
  165. position.Y += ScreenManager.Font.LineSpacing;
  166. }
  167. // Draw the screen title.
  168. string title = Resources.Lobby;
  169. Vector2 titlePosition = new Vector2 (533, 80);
  170. Vector2 titleOrigin = font.MeasureString (title) / 2;
  171. Color titleColor = new Color (192, 192, 192) * TransitionAlpha;
  172. float titleScale = 1.25f;
  173. titlePosition.Y -= transitionOffset * 100;
  174. spriteBatch.DrawString (font, title, titlePosition, titleColor, 0,
  175. titleOrigin, titleScale, SpriteEffects.None, 0);
  176. spriteBatch.End ();
  177. }
  178. /// <summary>
  179. /// Helper draws the gamertag and status icons for a single NetworkGamer.
  180. /// </summary>
  181. void DrawGamer (NetworkGamer gamer, Vector2 position)
  182. {
  183. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  184. SpriteFont font = ScreenManager.Font;
  185. Vector2 iconWidth = new Vector2 (34, 0);
  186. Vector2 iconOffset = new Vector2 (0, 12);
  187. Vector2 iconPosition = position + iconOffset;
  188. // Draw the "is ready" icon.
  189. if (gamer.IsReady) {
  190. spriteBatch.Draw (isReadyTexture, iconPosition,
  191. Color.Lime * TransitionAlpha);
  192. }
  193. iconPosition += iconWidth;
  194. // Draw the "is muted", "is talking", or "has voice" icon.
  195. if (gamer.IsMutedByLocalUser) {
  196. spriteBatch.Draw (voiceMutedTexture, iconPosition,
  197. Color.Red * TransitionAlpha);
  198. } else if (gamer.IsTalking) {
  199. spriteBatch.Draw (isTalkingTexture, iconPosition,
  200. Color.Yellow * TransitionAlpha);
  201. } else if (gamer.HasVoice) {
  202. spriteBatch.Draw (hasVoiceTexture, iconPosition,
  203. Color.White * TransitionAlpha);
  204. }
  205. // Draw the gamertag, normally in white, but yellow for local players.
  206. string text = gamer.Gamertag;
  207. if (gamer.IsHost)
  208. text += Resources.HostSuffix;
  209. Color color = (gamer.IsLocal) ? Color.Yellow : Color.White;
  210. spriteBatch.DrawString (font, text, position + iconWidth * 2,
  211. color * TransitionAlpha);
  212. }
  213. #endregion
  214. }
  215. }