MainMenuScreen.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MainMenuScreen.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.GamerServices;
  13. #if !WINDOWS_PHONE
  14. using Microsoft.Xna.Framework.Storage;
  15. #endif
  16. #endregion
  17. namespace HoneycombRush
  18. {
  19. class MainMenuScreen : MenuScreen
  20. {
  21. #region Fields
  22. bool isExiting = false;
  23. #endregion
  24. #region Initializations
  25. public MainMenuScreen()
  26. : base("")
  27. {
  28. }
  29. public override void LoadContent()
  30. {
  31. // Create our menu entries.
  32. MenuEntry startGameMenuEntry = new MenuEntry("Start");
  33. MenuEntry exitMenuEntry = new MenuEntry("Exit");
  34. // Calculate menu positions - we do this here since we want the screen
  35. // manager to be available
  36. int quarterViewportWidth = ScreenManager.GraphicsDevice.Viewport.Width / 4;
  37. int menuEntryHeight = SafeArea.Bottom - ScreenManager.ButtonBackground.Height * 2;
  38. startGameMenuEntry.Position = new Vector2(quarterViewportWidth -
  39. ScreenManager.ButtonBackground.Width / 2, menuEntryHeight);
  40. exitMenuEntry.Position = new Vector2(3 * quarterViewportWidth -
  41. ScreenManager.ButtonBackground.Width / 2, menuEntryHeight);
  42. // Hook up menu event handlers.
  43. startGameMenuEntry.Selected += StartGameMenuEntrySelected;
  44. exitMenuEntry.Selected += OnCancel;
  45. // Add entries to the menu.
  46. MenuEntries.Add(startGameMenuEntry);
  47. MenuEntries.Add(exitMenuEntry);
  48. AudioManager.LoadSounds();
  49. AudioManager.LoadMusic();
  50. AudioManager.PlayMusic("MenuMusic_Loop");
  51. base.LoadContent();
  52. }
  53. #endregion
  54. #region Update
  55. /// <summary>
  56. /// Performs necessary update logic.
  57. /// </summary>
  58. /// <param name="gameTime">Game time information.</param>
  59. /// <param name="otherScreenHasFocus">Whether another screen has the focus.</param>
  60. /// <param name="coveredByOtherScreen">Whether this screen is covered by another.</param>
  61. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  62. {
  63. if (isExiting)
  64. {
  65. if (!HighScoreScreen.HighscoreSaved)
  66. {
  67. HighScoreScreen.SaveHighscore();
  68. }
  69. else
  70. {
  71. isExiting = false;
  72. ScreenManager.Game.Exit();
  73. }
  74. }
  75. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  76. }
  77. /// <summary>
  78. /// Handles user input.
  79. /// </summary>
  80. /// <param name="gameTime">Game time information.</param>
  81. /// <param name="input">Input information.</param>
  82. public override void HandleInput(GameTime gameTime, InputState input)
  83. {
  84. if (isExiting)
  85. {
  86. return;
  87. }
  88. base.HandleInput(gameTime, input);
  89. }
  90. #endregion
  91. #region Menu handlers
  92. /// <summary>
  93. /// Respond to "Play" Item Selection
  94. /// </summary>
  95. /// <param name="sender"></param>
  96. /// <param name="e"></param>
  97. void StartGameMenuEntrySelected(object sender, EventArgs e)
  98. {
  99. foreach (GameScreen screen in ScreenManager.GetScreens())
  100. {
  101. screen.ExitScreen();
  102. }
  103. #if WINDOWS_PHONE
  104. ScreenManager.AddScreen(new BackgroundScreen("Instructions"), null);
  105. #elif XBOX
  106. ScreenManager.AddScreen(new BackgroundScreen("InstructionsXbox"), null);
  107. #else
  108. ScreenManager.AddScreen(new BackgroundScreen("InstructionsPC"), null);
  109. #endif
  110. ScreenManager.AddScreen(new LoadingAndInstructionScreen(), null);
  111. AudioManager.StopSound("MenuMusic_Loop");
  112. }
  113. /// <summary>
  114. /// Respond to "Exit" Item Selection
  115. /// </summary>
  116. /// <param name="playerIndex"></param>
  117. protected override void OnCancel(PlayerIndex playerIndex)
  118. {
  119. isExiting = true;
  120. AudioManager.StopSound("MenuMusic_Loop");
  121. }
  122. #endregion
  123. }
  124. }