MainMenuScreen.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //-----------------------------------------------------------------------------
  2. // MainMenuScreen.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Net;
  13. namespace CatapultGame
  14. {
  15. class MainMenuScreen : MenuScreen
  16. {
  17. public MainMenuScreen()
  18. : base(String.Empty)
  19. {
  20. IsPopup = true;
  21. // Create our menu entries.
  22. MenuEntry startGameMenuEntry = new MenuEntry("Single Player vs AI");
  23. MenuEntry startMultiPlayerGameMenuEntry = new MenuEntry("Multi Player on LAN");
  24. MenuEntry exitMenuEntry = new MenuEntry("Exit");
  25. // Hook up menu event handlers.
  26. startGameMenuEntry.Selected += StartGameMenuEntrySelected;
  27. startMultiPlayerGameMenuEntry.Selected += StartMultiPlayerGameMenuEntrySelected;
  28. exitMenuEntry.Selected += OnCancel;
  29. // Add entries to the menu.
  30. MenuEntries.Add(startGameMenuEntry);
  31. MenuEntries.Add(startMultiPlayerGameMenuEntry);
  32. MenuEntries.Add(exitMenuEntry);
  33. }
  34. protected override void UpdateMenuEntryLocations()
  35. {
  36. base.UpdateMenuEntryLocations();
  37. foreach (var entry in MenuEntries)
  38. {
  39. Vector2 position = entry.Position;
  40. position.Y += 60;
  41. entry.Position = position;
  42. }
  43. }
  44. /// <summary>
  45. /// Handles "Play" menu item selection
  46. /// </summary>
  47. /// <param name="sender"></param>
  48. /// <param name="e"></param>
  49. void StartGameMenuEntrySelected(object sender, EventArgs e)
  50. {
  51. // Lets make sure we get rid of our network session
  52. // so we can start up clean
  53. ScreenManager.Game.Services.RemoveService(typeof(NetworkSession));
  54. ScreenManager.AddScreen(new InstructionsScreen(), null);
  55. }
  56. /// <summary>
  57. /// Handles "Exit" menu item selection
  58. /// </summary>
  59. ///
  60. protected override void OnCancel(PlayerIndex playerIndex)
  61. {
  62. ScreenManager.Game.Exit();
  63. }
  64. void StartMultiPlayerGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  65. {
  66. CreateOrFindSession(NetworkSessionType.SystemLink, e.PlayerIndex);
  67. }
  68. /// <summary>
  69. /// Helper method shared by the Live and System Link menu event handlers.
  70. /// </summary>
  71. void CreateOrFindSession(NetworkSessionType sessionType, PlayerIndex playerIndex)
  72. {
  73. // First, we need to make sure a suitable gamer profile is signed in.
  74. ProfileSignInScreen profileSignIn = new ProfileSignInScreen(sessionType);
  75. // Hook up an event so once the ProfileSignInScreen is happy,
  76. // it will activate the CreateOrFindSessionScreen.
  77. profileSignIn.ProfileSignedIn += delegate
  78. {
  79. GameScreen createOrFind = new CreateOrFindSessionScreen(sessionType);
  80. ScreenManager.AddScreen(createOrFind, playerIndex);
  81. };
  82. // Activate the ProfileSignInScreen.
  83. ScreenManager.AddScreen(profileSignIn, playerIndex);
  84. }
  85. }
  86. }