GameplayScreen.cs 5.7 KB

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