mitko900

Untitled

Feb 12th, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.54 KB | None | 0 0
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ScreenManager.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9.  
  10. #region Using Statements
  11. using System.Diagnostics;
  12. using System.Collections.Generic;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using Microsoft.Xna.Framework.Input.Touch;
  17. #endregion
  18.  
  19. namespace SumoGame.Screens
  20. {
  21. /// <summary>
  22. /// The screen manager is a component which manages one or more GameScreen
  23. /// instances. It maintains a stack of screens, calls their Update and Draw
  24. /// methods at the appropriate times, and automatically routes input to the
  25. /// topmost active screen.
  26. /// </summary>
  27. public class ScreenManager : DrawableGameComponent
  28. {
  29. #region Fields
  30.  
  31. List<GameScreen> screens = new List<GameScreen>();
  32. List<GameScreen> screensToUpdate = new List<GameScreen>();
  33. InputState input = new InputState();
  34. AnimatedAsset[] animatedAssets = new AnimatedAsset[15];
  35. SumoPlayer[] players = new SumoPlayer[2];
  36. Rectangle gameRect = new Rectangle(0, 0, 1920, 1080);
  37.  
  38. SpriteBatch spriteBatch;
  39. SpriteFont font;
  40. Texture2D blankTexture;
  41.  
  42. bool isInitialized;
  43.  
  44. bool traceEnabled;
  45.  
  46. #endregion
  47.  
  48. #region Properties
  49.  
  50.  
  51. /// <summary>
  52. /// A default SpriteBatch shared by all the screens. This saves
  53. /// each screen having to bother creating their own local instance.
  54. /// </summary>
  55. public SpriteBatch SpriteBatch
  56. {
  57. get { return spriteBatch; }
  58. }
  59.  
  60. public InputState InputState
  61. {
  62. get { return input; }
  63. }
  64.  
  65. public Rectangle GameRect
  66. {
  67. get { return gameRect; }
  68. }
  69.  
  70. /// <summary>
  71. /// Return the asset descriptions
  72. /// </summary>
  73. public AnimatedAsset[] Assets
  74. {
  75. get { return animatedAssets; }
  76. }
  77.  
  78. /// <summary>
  79. /// Current players
  80. /// </summary>
  81. public SumoPlayer[] Players
  82. {
  83. get { return players; }
  84. }
  85.  
  86. /// <summary>
  87. /// A default font shared by all the screens. This saves
  88. /// each screen having to bother loading their own local copy.
  89. /// </summary>
  90. public SpriteFont Font
  91. {
  92. get { return font; }
  93. }
  94.  
  95.  
  96. /// <summary>
  97. /// If true, the manager prints out a list of all the screens
  98. /// each time it is updated. This can be useful for making sure
  99. /// everything is being added and removed at the right times.
  100. /// </summary>
  101. public bool TraceEnabled
  102. {
  103. get { return traceEnabled; }
  104. set { traceEnabled = value; }
  105. }
  106.  
  107. #endregion
  108.  
  109. #region Initialization
  110.  
  111.  
  112. /// <summary>
  113. /// Constructs a new screen manager component.
  114. /// </summary>
  115. public ScreenManager(Game game)
  116. : base(game)
  117. {
  118. // we must set EnabledGestures before we can query for them, but
  119. // we don't assume the game wants to read them.
  120. // TouchPanel.EnabledGestures = GestureType.None;
  121. }
  122.  
  123.  
  124. /// <summary>
  125. /// Initializes the screen manager component.
  126. /// </summary>
  127. public override void Initialize()
  128. {
  129. base.Initialize();
  130.  
  131. isInitialized = true;
  132. }
  133.  
  134.  
  135. /// <summary>
  136. /// Load your graphics content.
  137. /// </summary>
  138. protected override void LoadContent()
  139. {
  140. // Load content belonging to the screen manager.
  141. ContentManager content = Game.Content;
  142.  
  143. spriteBatch = new SpriteBatch(GraphicsDevice);
  144. font = content.Load<SpriteFont>("UIFont");
  145. blankTexture = content.Load<Texture2D>("blank");
  146.  
  147. // Define AnimatedAssets[] := [name, frames]
  148. animatedAssets[0] = new AnimatedAsset("bout", 4);
  149. animatedAssets[1] = new AnimatedAsset("bowOpponent", 12);
  150. animatedAssets[2] = new AnimatedAsset("bowUser", 12);
  151. animatedAssets[3] = new AnimatedAsset("bowWin", 40);
  152. animatedAssets[4] = new AnimatedAsset("clapUp", 20);
  153. animatedAssets[5] = new AnimatedAsset("crouch", 30);
  154. animatedAssets[6] = new AnimatedAsset("fists", 10);
  155. animatedAssets[7] = new AnimatedAsset("loss1", 12);
  156. animatedAssets[8] = new AnimatedAsset("loss2", 10);
  157. animatedAssets[9] = new AnimatedAsset("salt", 18);
  158. animatedAssets[10] = new AnimatedAsset("stomp", 55);
  159. animatedAssets[11] = new AnimatedAsset("win1", 10);
  160. animatedAssets[12] = new AnimatedAsset("win2", 12);
  161. animatedAssets[13] = new AnimatedAsset("confetti", 2);
  162. animatedAssets[14] = new AnimatedAsset("break", 8);
  163.  
  164. players[0] = new SumoPlayer(SumoPlayer.LEFT,
  165. "Mitko", Color.Red); // left
  166. players[0].PlayerFlagName = new string[] { "Bulgaria" };
  167.  
  168. players[1] = new SumoPlayer(SumoPlayer.RIGHT,
  169. "Mitko", Color.Blue); // right
  170. players[1].PlayerFlagName = new string[] { "Sofia" };
  171.  
  172. // Tell each of the screens to load their content.
  173. foreach (GameScreen screen in screens)
  174. {
  175. screen.LoadContent();
  176. }
  177. }
  178.  
  179.  
  180. /// <summary>
  181. /// Unload your graphics content.
  182. /// </summary>
  183. protected override void UnloadContent()
  184. {
  185. // Tell each of the screens to unload their content.
  186. foreach (GameScreen screen in screens)
  187. {
  188. screen.UnloadContent();
  189. }
  190. }
  191.  
  192.  
  193. #endregion
  194.  
  195. #region Update and Draw
  196.  
  197.  
  198. /// <summary>
  199. /// Allows each screen to run logic.
  200. /// </summary>
  201. public override void Update(GameTime gameTime)
  202. {
  203. // Read the keyboard and gamepad.
  204. input.Update(gameTime);
  205.  
  206. // Make a copy of the master screen list, to avoid confusion if
  207. // the process of updating one screen adds or removes others.
  208. screensToUpdate.Clear();
  209.  
  210. foreach (GameScreen screen in screens)
  211. {
  212. if (screen.KeepAlive == false)
  213. {
  214. screensToUpdate.Add(screen);
  215. }
  216. }
  217.  
  218.  
  219. bool otherScreenHasFocus = !Game.IsActive;
  220. bool coveredByOtherScreen = false;
  221.  
  222. // Loop as long as there are screens waiting to be updated.
  223. while (screensToUpdate.Count > 0)
  224. {
  225. // Pop the topmost screen off the waiting list.
  226. GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
  227.  
  228. screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
  229.  
  230. // Update the screen.
  231. screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  232.  
  233. if (screen.ScreenState == ScreenState.TransitionOn ||
  234. screen.ScreenState == ScreenState.Active)
  235. {
  236. // If this is the first active screen we came across,
  237. // give it a chance to handle input.
  238. if (!otherScreenHasFocus)
  239. {
  240. screen.HandleInput(input);
  241.  
  242. otherScreenHasFocus = true;
  243. }
  244.  
  245. // If this is an active non-popup, inform any subsequent
  246. // screens that they are covered by it.
  247. if (!screen.IsPopup)
  248. coveredByOtherScreen = true;
  249. }
  250. }
  251.  
  252. // Print debug trace?
  253. if (traceEnabled)
  254. TraceScreens();
  255. }
  256.  
  257.  
  258. /// <summary>
  259. /// Prints a list of all the screens, for debugging.
  260. /// </summary>
  261. void TraceScreens()
  262. {
  263. List<string> screenNames = new List<string>();
  264.  
  265. foreach (GameScreen screen in screens)
  266. screenNames.Add(screen.GetType().Name);
  267.  
  268. Debug.WriteLine(string.Join(", ", screenNames.ToArray()));
  269. }
  270.  
  271.  
  272. /// <summary>
  273. /// Tells each screen to draw itself.
  274. /// </summary>
  275. public override void Draw(GameTime gameTime)
  276. {
  277. foreach (GameScreen screen in screens)
  278. {
  279. if (screen.ScreenState == ScreenState.Hidden || screen.KeepAlive == true)
  280. continue;
  281.  
  282. screen.Draw(gameTime);
  283. }
  284. }
  285.  
  286.  
  287. #endregion
  288.  
  289. #region Public Methods
  290.  
  291. public AnimatedAsset GetAnimatedAsset(string name)
  292. {
  293. for (int i = 0; i < animatedAssets.Length; i++)
  294. if (animatedAssets[i].Name.Equals(name))
  295. return animatedAssets[i];
  296. return null;
  297. }
  298.  
  299.  
  300. /// <summary>
  301. /// Adds a new screen to the screen manager.
  302. /// </summary>
  303. public void AddScreen(GameScreen screen, PlayerIndex? controllingPlayer)
  304. {
  305. bool same = false;
  306.  
  307. foreach(GameScreen one in screens) {
  308. if (one.GetType().Name.Equals(screen.GetType().Name))
  309. {
  310. same = true;
  311. }
  312. }
  313.  
  314. if (same == false)
  315. {
  316. screen.ControllingPlayer = controllingPlayer;
  317. screen.ScreenManager = this;
  318. screen.IsExiting = false;
  319.  
  320. // If we have a graphics device, tell the screen to load content.
  321. if (isInitialized)
  322. {
  323. screen.LoadContent();
  324. }
  325.  
  326. screens.Add(screen);
  327.  
  328. // update the TouchPanel to respond to gestures this screen is interested in
  329. // TouchPanel.EnabledGestures = screen.EnabledGestures;
  330. }
  331. }
  332.  
  333. public void hideScreenAtIndex(int index)
  334. {
  335. //ScreenState screenState = ScreenState.TransitionOn;
  336. screens[index].KeepAlive = true;
  337. }
  338.  
  339.  
  340. /// <summary>
  341. /// Removes a screen from the screen manager. You should normally
  342. /// use GameScreen.ExitScreen instead of calling this directly, so
  343. /// the screen can gradually transition off rather than just being
  344. /// instantly removed.
  345. /// </summary>
  346. public void RemoveScreen(GameScreen screen)
  347. {
  348. // If we have a graphics device, tell the screen to unload content.
  349. if (isInitialized)
  350. {
  351. screen.UnloadContent();
  352. }
  353.  
  354. screens.Remove(screen);
  355. screensToUpdate.Remove(screen);
  356.  
  357. // if there is a screen still in the manager, update TouchPanel
  358. // to respond to gestures that screen is interested in.
  359. if (screens.Count > 0)
  360. {
  361. // TouchPanel.EnabledGestures = screens[screens.Count - 1].EnabledGestures;
  362. }
  363. }
  364.  
  365.  
  366. /// <summary>
  367. /// Expose an array holding all the screens. We return a copy rather
  368. /// than the real master list, because screens should only ever be added
  369. /// or removed using the AddScreen and RemoveScreen methods.
  370. /// </summary>
  371. public GameScreen[] GetScreens()
  372. {
  373. return screens.ToArray();
  374. }
  375.  
  376.  
  377. /// <summary>
  378. /// Helper draws a translucent black fullscreen sprite, used for fading
  379. /// screens in and out, and for darkening the background behind popups.
  380. /// </summary>
  381. public void FadeBackBufferToBlack(float alpha)
  382. {
  383. Viewport viewport = GraphicsDevice.Viewport;
  384.  
  385. spriteBatch.Begin();
  386.  
  387. spriteBatch.Draw(blankTexture,
  388. new Rectangle(0, 0, viewport.Width, viewport.Height),
  389. Color.Black * alpha);
  390.  
  391. spriteBatch.End();
  392. }
  393.  
  394.  
  395. #endregion
  396. }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment