NetRumbleGame.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //-----------------------------------------------------------------------------
  2. // NetRumbleGame.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using System.Reflection;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Net;
  15. using Microsoft.Xna.Framework.GamerServices;
  16. namespace NetRumble
  17. {
  18. /// <summary>
  19. /// While this used to be an enum in the XNA Framework API, it was removed in version 4.0.
  20. /// However, it is still useful as a parameter to specify what kind of particles to draw,
  21. /// so it is revitalized here as a global enum.
  22. /// </summary>
  23. public enum SpriteBlendMode
  24. {
  25. Additive,
  26. AlphaBlend
  27. }
  28. /// <summary>
  29. /// The main type for this game.
  30. /// </summary>
  31. public class NetRumbleGame : Microsoft.Xna.Framework.Game
  32. {
  33. /// <summary>
  34. /// The graphics device manager used to render the game.
  35. /// </summary>
  36. GraphicsDeviceManager graphicsDeviceManager;
  37. /// <summary>
  38. /// The manager for all of the user-interface data.
  39. /// </summary>
  40. ScreenManager screenManager;
  41. /// <summary>
  42. /// Constructs a new NetRumbleGame object.
  43. /// </summary>
  44. public NetRumbleGame()
  45. {
  46. // initialize the graphics device manager
  47. graphicsDeviceManager = new GraphicsDeviceManager(this);
  48. graphicsDeviceManager.PreferredBackBufferWidth = ScreenManager.BASE_BUFFER_WIDTH;
  49. graphicsDeviceManager.PreferredBackBufferHeight = ScreenManager.BASE_BUFFER_HEIGHT;
  50. if (UIUtility.IsMobile)
  51. {
  52. graphicsDeviceManager.IsFullScreen = true;
  53. IsMouseVisible = false;
  54. }
  55. else if (UIUtility.IsDesktop)
  56. {
  57. graphicsDeviceManager.IsFullScreen = false;
  58. IsMouseVisible = true;
  59. }
  60. else
  61. {
  62. throw new PlatformNotSupportedException();
  63. }
  64. // initialize the content manager
  65. Content.RootDirectory = "Content";
  66. // initialize the gamer-services component
  67. // this component enables Live sign-in functionality
  68. // and updates the Gamer.SignedInGamers collection.
  69. Components.Add(new GamerServicesComponent(this));
  70. // initialize the screen manager
  71. screenManager = new ScreenManager(this);
  72. Components.Add(screenManager);
  73. // initialize the audio system
  74. var audioPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Content.RootDirectory, "Audio", "wav");
  75. AudioManager.Initialize(this, new DirectoryInfo(audioPath));
  76. }
  77. /// <summary>
  78. /// Allows the game to perform any initialization it needs to before starting.
  79. /// This is where it can query for any required services and load any
  80. /// non-graphic related content. Calling base.Initialize will enumerate through
  81. /// any components and initialize them as well.
  82. /// </summary>
  83. protected override void Initialize()
  84. {
  85. base.Initialize();
  86. // Useful to turn on SimulateTrialMode here if you want to test launching the game
  87. // from an invite and have it start in trial mode.
  88. //#if DEBUG
  89. // Guide.SimulateTrialMode = true;
  90. //#endif
  91. // load the initial screens
  92. screenManager.AddScreen(new BackgroundScreen());
  93. screenManager.AddScreen(new MainMenuScreen());
  94. // hookup the invite event-processing function
  95. NetworkSession.InviteAccepted += new EventHandler<InviteAcceptedEventArgs>(NetworkSession_InviteAccepted);
  96. }
  97. /// <summary>
  98. /// Begins the asynchronous process of joining a game from an invitation.
  99. /// </summary>
  100. void NetworkSession_InviteAccepted(object sender, InviteAcceptedEventArgs e)
  101. {
  102. if (Guide.IsTrialMode)
  103. {
  104. screenManager.invited = e.Gamer as SignedInGamer;
  105. string message = "Need to unlock full version before you can accept this invite.";
  106. MessageBoxScreen messageBox = new MessageBoxScreen(message);
  107. screenManager.AddScreen(messageBox);
  108. System.Console.WriteLine("Cannot accept invite yet because we're in trial mode");
  109. return;
  110. }
  111. // We will join the game from a method in this screen.
  112. MainMenuScreen mainMenu = null;
  113. // Keep the background screen and main menu screen but remove all others
  114. // to prepare for joining the game we were invited to.
  115. foreach (GameScreen screen in screenManager.GetScreens())
  116. {
  117. if (screen is BackgroundScreen)
  118. {
  119. continue;
  120. }
  121. else if (screen is MainMenuScreen)
  122. {
  123. mainMenu = screen as MainMenuScreen;
  124. }
  125. else
  126. {
  127. // If there's an active network session, we'll need to end it
  128. // before attempting to join a new one.
  129. MethodInfo method = screen.GetType().GetMethod("EndSession");
  130. if (method != null)
  131. {
  132. method.Invoke(screen, null);
  133. }
  134. // Now exit and remove this screen.
  135. screen.ExitScreen();
  136. screenManager.RemoveScreen(screen);
  137. }
  138. }
  139. // Now attempt to join the game to which we were invited!
  140. if (mainMenu != null)
  141. mainMenu.JoinInvitedGame();
  142. }
  143. /// <summary>
  144. /// This is called when the game should draw itself.
  145. /// </summary>
  146. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  147. protected override void Draw(GameTime gameTime)
  148. {
  149. graphicsDeviceManager.GraphicsDevice.Clear(Color.MonoGameOrange);
  150. base.Draw(gameTime);
  151. }
  152. }
  153. }