LobbyScreen.cs 8.0 KB

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