LobbyScreen.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. {
  66. if (networkSession.SessionState == NetworkSessionState.Playing)
  67. {
  68. // Check if we should leave the lobby and begin gameplay.
  69. // We pass null as the controlling player, because the networked
  70. // gameplay screen accepts input from any local players who
  71. // are in the session, not just a single controlling player.
  72. LoadingScreen.Load(ScreenManager, true, null,
  73. new GameplayScreen(networkSession));
  74. }
  75. else if (networkSession.IsHost && networkSession.IsEveryoneReady)
  76. {
  77. // The host checks whether everyone has marked themselves
  78. // as ready, and starts the game in response.
  79. networkSession.StartGame();
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Handles user input for all the local gamers in the session. Unlike most
  85. /// screens, which use the InputState class to combine input data from all
  86. /// gamepads, the lobby needs to individually mark specific players as ready,
  87. /// so it loops over all the local gamers and reads their inputs individually.
  88. /// </summary>
  89. public override void HandleInput(InputState input)
  90. {
  91. foreach (LocalNetworkGamer gamer in networkSession.LocalGamers)
  92. {
  93. PlayerIndex playerIndex = gamer.SignedInGamer.PlayerIndex;
  94. PlayerIndex unwantedOutput;
  95. if (input.IsMenuSelect(playerIndex, out unwantedOutput))
  96. {
  97. HandleMenuSelect(gamer);
  98. }
  99. else if (input.IsMenuCancel(playerIndex, out unwantedOutput))
  100. {
  101. HandleMenuCancel(gamer);
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// Handle MenuSelect inputs by marking ourselves as ready.
  107. /// </summary>
  108. void HandleMenuSelect(LocalNetworkGamer gamer)
  109. {
  110. if (!gamer.IsReady)
  111. {
  112. gamer.IsReady = true;
  113. }
  114. else if (gamer.IsHost)
  115. {
  116. // The host has an option to force starting the game, even if not
  117. // everyone has marked themselves ready. If they press select twice
  118. // in a row, the first time marks the host ready, then the second
  119. // time we ask if they want to force start.
  120. MessageBoxScreen messageBox = new MessageBoxScreen(
  121. Resources.ConfirmForceStartGame);
  122. messageBox.Accepted += ConfirmStartGameMessageBoxAccepted;
  123. ScreenManager.AddScreen(messageBox, gamer.SignedInGamer.PlayerIndex);
  124. }
  125. }
  126. /// <summary>
  127. /// Event handler for when the host selects ok on the "are you sure
  128. /// you want to start even though not everyone is ready" message box.
  129. /// </summary>
  130. void ConfirmStartGameMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
  131. {
  132. if (networkSession.SessionState == NetworkSessionState.Lobby)
  133. {
  134. networkSession.StartGame();
  135. }
  136. }
  137. /// <summary>
  138. /// Handle MenuCancel inputs by clearing our ready status, or if it is
  139. /// already clear, prompting if the user wants to leave the session.
  140. /// </summary>
  141. void HandleMenuCancel(LocalNetworkGamer gamer)
  142. {
  143. if (gamer.IsReady)
  144. {
  145. gamer.IsReady = false;
  146. }
  147. else
  148. {
  149. PlayerIndex playerIndex = gamer.SignedInGamer.PlayerIndex;
  150. NetworkSessionComponent.LeaveSession(ScreenManager, playerIndex);
  151. }
  152. }
  153. #endregion
  154. #region Draw
  155. /// <summary>
  156. /// Draws the lobby screen.
  157. /// </summary>
  158. public override void Draw(GameTime gameTime)
  159. {
  160. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  161. SpriteFont font = ScreenManager.Font;
  162. Vector2 position = new Vector2(100, 150);
  163. // Make the lobby slide into place during transitions.
  164. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  165. if (ScreenState == ScreenState.TransitionOn)
  166. position.X -= transitionOffset * 256;
  167. else
  168. position.X += transitionOffset * 512;
  169. spriteBatch.Begin();
  170. // Draw all the gamers in the session.
  171. int gamerCount = 0;
  172. foreach (NetworkGamer gamer in networkSession.AllGamers)
  173. {
  174. DrawGamer(gamer, position);
  175. // Advance to the next screen position, wrapping into two
  176. // columns if there are more than 8 gamers in the session.
  177. if (++gamerCount == 8)
  178. {
  179. position.X += 433;
  180. position.Y = 150;
  181. }
  182. else
  183. position.Y += ScreenManager.Font.LineSpacing;
  184. }
  185. // Draw the screen title.
  186. string title = Resources.Lobby;
  187. Vector2 titlePosition = new Vector2(533, 80);
  188. Vector2 titleOrigin = font.MeasureString(title) / 2;
  189. Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
  190. float titleScale = 1.25f;
  191. titlePosition.Y -= transitionOffset * 100;
  192. spriteBatch.DrawString(font, title, titlePosition, titleColor, 0,
  193. titleOrigin, titleScale, SpriteEffects.None, 0);
  194. spriteBatch.End();
  195. }
  196. /// <summary>
  197. /// Helper draws the gamertag and status icons for a single NetworkGamer.
  198. /// </summary>
  199. void DrawGamer(NetworkGamer gamer, Vector2 position)
  200. {
  201. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  202. SpriteFont font = ScreenManager.Font;
  203. Vector2 iconWidth = new Vector2(34, 0);
  204. Vector2 iconOffset = new Vector2(0, 12);
  205. Vector2 iconPosition = position + iconOffset;
  206. // Draw the "is ready" icon.
  207. if (gamer.IsReady)
  208. {
  209. spriteBatch.Draw(isReadyTexture, iconPosition,
  210. Color.Lime * TransitionAlpha);
  211. }
  212. iconPosition += iconWidth;
  213. // Draw the "is muted", "is talking", or "has voice" icon.
  214. if (gamer.IsMutedByLocalUser)
  215. {
  216. spriteBatch.Draw(voiceMutedTexture, iconPosition,
  217. Color.Red * TransitionAlpha);
  218. }
  219. else if (gamer.IsTalking)
  220. {
  221. spriteBatch.Draw(isTalkingTexture, iconPosition,
  222. Color.Yellow * TransitionAlpha);
  223. }
  224. else if (gamer.HasVoice)
  225. {
  226. spriteBatch.Draw(hasVoiceTexture, iconPosition,
  227. Color.White * TransitionAlpha);
  228. }
  229. // Draw the gamertag, normally in white, but yellow for local players.
  230. string text = gamer.Gamertag;
  231. if (gamer.IsHost)
  232. text += Resources.HostSuffix;
  233. Color color = (gamer.IsLocal) ? Color.Yellow : Color.White;
  234. spriteBatch.DrawString(font, text, position + iconWidth * 2,
  235. color * TransitionAlpha);
  236. }
  237. #endregion
  238. }
  239. }