GameplayScreen.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //-----------------------------------------------------------------------------
  2. // GameplayScreen.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Threading;
  9. using Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.Content;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. using RolePlaying.Data;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. namespace RolePlaying
  17. {
  18. /// <summary>
  19. /// This screen implements the actual game logic. It is just a
  20. /// placeholder to get the idea across: you'll probably want to
  21. /// put some more interesting gameplay in here!
  22. /// </summary>
  23. class GameplayScreen : GameScreen
  24. {
  25. GameStartDescription gameStartDescription = null;
  26. SaveGameDescription saveGameDescription = null;
  27. /// <summary>
  28. /// Create a new GameplayScreen object.
  29. /// </summary>
  30. private GameplayScreen()
  31. : base()
  32. {
  33. CombatEngine.ClearCombat();
  34. this.Exiting += new EventHandler(GameplayScreen_Exiting);
  35. }
  36. /// <summary>
  37. /// Create a new GameplayScreen object from a new-game description.
  38. /// </summary>
  39. public GameplayScreen(GameStartDescription gameStartDescription)
  40. : this()
  41. {
  42. this.gameStartDescription = gameStartDescription;
  43. this.saveGameDescription = null;
  44. }
  45. /// <summary>
  46. /// Create a new GameplayScreen object from a saved-game description.
  47. /// </summary>
  48. public GameplayScreen(SaveGameDescription saveGameDescription)
  49. : this()
  50. {
  51. this.gameStartDescription = null;
  52. this.saveGameDescription = saveGameDescription;
  53. }
  54. /// <summary>
  55. /// Handle the closing of this screen.
  56. /// </summary>
  57. void GameplayScreen_Exiting(object sender, EventArgs e)
  58. {
  59. // make sure the session is ending
  60. // -- EndSession must be re-entrant safe, as the EndSession may be
  61. // making this screen close itself
  62. Session.EndSession();
  63. }
  64. /// <summary>
  65. /// Load graphics content for the game.
  66. /// </summary>
  67. public override void LoadContent()
  68. {
  69. if (gameStartDescription != null)
  70. {
  71. Session.StartNewSession(gameStartDescription, ScreenManager, this);
  72. }
  73. else if (saveGameDescription != null)
  74. {
  75. Session.LoadSession(saveGameDescription, ScreenManager, this);
  76. }
  77. // once the load has finished, we use ResetElapsedTime to tell the game's
  78. // timing mechanism that we have just finished a very long frame, and that
  79. // it should not try to catch up.
  80. ScreenManager.Game.ResetElapsedTime();
  81. }
  82. /// <summary>
  83. /// Updates the state of the game. This method checks the GameScreen.IsActive
  84. /// property, so the game will stop updating when the pause menu is active,
  85. /// or if you tab away to a different application.
  86. /// </summary>
  87. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  88. bool coveredByOtherScreen)
  89. {
  90. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  91. if (IsActive && !coveredByOtherScreen)
  92. {
  93. Session.Update(gameTime);
  94. }
  95. }
  96. /// <summary>
  97. /// Lets the game respond to player input. Unlike the Update method,
  98. /// this will only be called when the gameplay screen is active.
  99. /// </summary>
  100. public override void HandleInput()
  101. {
  102. if (InputManager.IsActionTriggered(InputManager.Action.MainMenu))
  103. {
  104. ScreenManager.AddScreen(new MainMenuScreen());
  105. return;
  106. }
  107. if (InputManager.IsActionTriggered(InputManager.Action.ExitGame))
  108. {
  109. // add a confirmation message box
  110. const string message =
  111. "Are you sure you want to exit? All unsaved progress will be lost.";
  112. MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);
  113. confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;
  114. ScreenManager.AddScreen(confirmExitMessageBox);
  115. return;
  116. }
  117. if (!CombatEngine.IsActive &&
  118. InputManager.IsActionTriggered(InputManager.Action.CharacterManagement))
  119. {
  120. ScreenManager.AddScreen(new StatisticsScreen(Session.Party.Players[0]));
  121. return;
  122. }
  123. }
  124. /// <summary>
  125. /// Event handler for when the user selects Yes
  126. /// on the "Are you sure?" message box.
  127. /// </summary>
  128. void ConfirmExitMessageBoxAccepted(object sender, EventArgs e)
  129. {
  130. ScreenManager.Game.Exit();
  131. }
  132. /// <summary>
  133. /// Draws the gameplay screen.
  134. /// </summary>
  135. public override void Draw(GameTime gameTime)
  136. {
  137. Session.Draw(gameTime);
  138. }
  139. }
  140. }