LobbyScreen.cs 7.8 KB

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