NetRumbleGame.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 graphics;
  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. graphics = new GraphicsDeviceManager(this);
  48. graphics.PreferredBackBufferWidth = 1280;
  49. graphics.PreferredBackBufferHeight = 720;
  50. #if MOBILE
  51. graphics.IsFullScreen = true;
  52. #endif
  53. // initialize the content manager
  54. Content.RootDirectory = "Content";
  55. // initialize the gamer-services component
  56. // this component enables Live sign-in functionality
  57. // and updates the Gamer.SignedInGamers collection.
  58. Components.Add(new GamerServicesComponent(this));
  59. // initialize the screen manager
  60. screenManager = new ScreenManager(this);
  61. Components.Add(screenManager);
  62. // initialize the audio system
  63. var audioPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Content.RootDirectory, "Audio", "wav");
  64. AudioManager.Initialize(this, new DirectoryInfo(audioPath));
  65. }
  66. /// <summary>
  67. /// Allows the game to perform any initialization it needs to before starting.
  68. /// This is where it can query for any required services and load any
  69. /// non-graphic related content. Calling base.Initialize will enumerate through
  70. /// any components and initialize them as well.
  71. /// </summary>
  72. protected override void Initialize()
  73. {
  74. base.Initialize();
  75. // Useful to turn on SimulateTrialMode here if you want to test launching the game
  76. // from an invite and have it start in trial mode.
  77. //#if DEBUG
  78. // Guide.SimulateTrialMode = true;
  79. //#endif
  80. // load the initial screens
  81. screenManager.AddScreen(new BackgroundScreen());
  82. screenManager.AddScreen(new MainMenuScreen());
  83. // hookup the invite event-processing function
  84. NetworkSession.InviteAccepted += new EventHandler<InviteAcceptedEventArgs>(NetworkSession_InviteAccepted);
  85. }
  86. /// <summary>
  87. /// Begins the asynchronous process of joining a game from an invitation.
  88. /// </summary>
  89. void NetworkSession_InviteAccepted(object sender, InviteAcceptedEventArgs e)
  90. {
  91. if (Guide.IsTrialMode)
  92. {
  93. screenManager.invited = e.Gamer as SignedInGamer;
  94. string message = "Need to unlock full version before you can accept this invite.";
  95. MessageBoxScreen messageBox = new MessageBoxScreen(message);
  96. screenManager.AddScreen(messageBox);
  97. System.Console.WriteLine("Cannot accept invite yet because we're in trial mode");
  98. return;
  99. }
  100. // We will join the game from a method in this screen.
  101. MainMenuScreen mainMenu = null;
  102. // Keep the background screen and main menu screen but remove all others
  103. // to prepare for joining the game we were invited to.
  104. foreach (GameScreen screen in screenManager.GetScreens())
  105. {
  106. if (screen is BackgroundScreen)
  107. {
  108. continue;
  109. }
  110. else if (screen is MainMenuScreen)
  111. {
  112. mainMenu = screen as MainMenuScreen;
  113. }
  114. else
  115. {
  116. // If there's an active network session, we'll need to end it
  117. // before attempting to join a new one.
  118. MethodInfo method = screen.GetType().GetMethod("EndSession");
  119. if (method != null)
  120. {
  121. method.Invoke(screen, null);
  122. }
  123. // Now exit and remove this screen.
  124. screen.ExitScreen();
  125. screenManager.RemoveScreen(screen);
  126. }
  127. }
  128. // Now attempt to join the game to which we were invited!
  129. if (mainMenu != null)
  130. mainMenu.JoinInvitedGame();
  131. }
  132. /// <summary>
  133. /// This is called when the game should draw itself.
  134. /// </summary>
  135. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  136. protected override void Draw(GameTime gameTime)
  137. {
  138. graphics.GraphicsDevice.Clear(Color.MonoGameOrange);
  139. base.Draw(gameTime);
  140. }
  141. }
  142. }