BackgroundScreen.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // BackgroundScreen.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.Graphics;
  13. using GameStateManagement;
  14. #endregion
  15. namespace MemoryMadness
  16. {
  17. class BackgroundScreen : GameScreen
  18. {
  19. #region Fields
  20. Texture2D background;
  21. Texture2D leftDoor;
  22. Texture2D rightDoor;
  23. Vector2 leftDoorPosition;
  24. Vector2 rightDoorPosition;
  25. bool animateDoors;
  26. bool doorsInTranistion;
  27. bool doorsHitFinalPosition = false;
  28. bool doorsBounceStarted = false;
  29. #endregion
  30. #region Initialization
  31. public BackgroundScreen(bool animateDoors)
  32. {
  33. TransitionOnTime = TimeSpan.FromSeconds(0.0);
  34. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  35. this.animateDoors = animateDoors;
  36. if (animateDoors)
  37. {
  38. AudioManager.PlaySound("doorOpen");
  39. }
  40. }
  41. #endregion
  42. #region Loading
  43. /// <summary>
  44. /// Load screen resources
  45. /// </summary>
  46. public override void LoadContent()
  47. {
  48. background = Load<Texture2D>(@"Textures\Backgrounds\titleBG");
  49. leftDoor = Load<Texture2D>(@"Textures\Backgrounds\leftDoor");
  50. rightDoor = Load<Texture2D>(@"Textures\Backgrounds\rightDoor");
  51. // Prepare to run the doors' animation
  52. if (animateDoors)
  53. doorsInTranistion = true;
  54. // Set the doors' start position
  55. leftDoorPosition = Settings.LeftDoorClosedPosition;
  56. rightDoorPosition = Settings.RightDoorClosedPosition;
  57. }
  58. #endregion
  59. #region Update
  60. /// <summary>
  61. /// Update the screen
  62. /// </summary>
  63. /// <param name="gameTime">The game time</param>
  64. /// <param name="otherScreenHasFocus"></param>
  65. /// <param name="coveredByOtherScreen"></param>
  66. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  67. bool coveredByOtherScreen)
  68. {
  69. if (doorsInTranistion && animateDoors)
  70. AnimateDoors();
  71. base.Update(gameTime, otherScreenHasFocus, false);
  72. }
  73. private void AnimateDoors()
  74. {
  75. if (!doorsHitFinalPosition || doorsBounceStarted)
  76. {
  77. // Update door X positions between the opened and closed states
  78. leftDoorPosition.X = MathHelper.Clamp(
  79. leftDoorPosition.X - Settings.DoorsAnimationStep,
  80. Settings.LeftDoorOpenedPosition.X,
  81. Settings.LeftDoorClosedPosition.X);
  82. rightDoorPosition.X = MathHelper.Clamp(
  83. rightDoorPosition.X + Settings.DoorsAnimationStep,
  84. Settings.RightDoorClosedPosition.X,
  85. Settings.RightDoorOpenedPosition.X);
  86. // If both doors reach their final position, raise a flag
  87. if (leftDoorPosition == Settings.LeftDoorOpenedPosition &&
  88. rightDoorPosition == Settings.RightDoorOpenedPosition)
  89. {
  90. if (!doorsHitFinalPosition)
  91. doorsHitFinalPosition = true;
  92. else
  93. doorsInTranistion = false;
  94. }
  95. }
  96. else if (doorsHitFinalPosition)
  97. {
  98. // Move the doors back towards their original opened position slightly
  99. // to create a bouncing effect
  100. leftDoorPosition.X = MathHelper.Clamp(
  101. leftDoorPosition.X + Settings.DoorsAnimationStep / 2,
  102. Settings.LeftDoorOpenedPosition.X,
  103. Settings.LeftDoorOpenedPosition.X + Settings.DoorsAnimationStep * 3);
  104. rightDoorPosition.X = MathHelper.Clamp(
  105. rightDoorPosition.X - Settings.DoorsAnimationStep / 2,
  106. Settings.RightDoorOpenedPosition.X - Settings.DoorsAnimationStep * 3,
  107. Settings.RightDoorOpenedPosition.X);
  108. if ((leftDoorPosition.X == Settings.LeftDoorOpenedPosition.X +
  109. Settings.DoorsAnimationStep * 3) &&
  110. (rightDoorPosition.X == Settings.RightDoorOpenedPosition.X -
  111. Settings.DoorsAnimationStep * 3))
  112. {
  113. doorsBounceStarted = true;
  114. }
  115. }
  116. }
  117. #endregion
  118. #region Render
  119. /// <summary>
  120. /// Renders the screen
  121. /// </summary>
  122. /// <param name="gameTime"></param>
  123. public override void Draw(GameTime gameTime)
  124. {
  125. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  126. spriteBatch.Begin();
  127. // Draw background
  128. spriteBatch.Draw(background, new Vector2(0, 0),
  129. Color.White * TransitionAlpha);
  130. // Draw the doors
  131. spriteBatch.Draw(leftDoor, leftDoorPosition, Color.White * TransitionAlpha);
  132. spriteBatch.Draw(rightDoor, rightDoorPosition, Color.White * TransitionAlpha);
  133. spriteBatch.End();
  134. }
  135. #endregion
  136. }
  137. }