2
0

StartScene.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Audio;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using RockRain.Core;
  6. using Microsoft.Xna.Framework.Media;
  7. using Microsoft.Xna.Framework.Input;
  8. namespace RockRain
  9. {
  10. /// <summary>
  11. /// This is a game component that implements the Game Start Scene.
  12. /// </summary>
  13. public class StartScene : GameScene
  14. {
  15. // Misc
  16. protected TextMenuComponent menu;
  17. protected readonly Texture2D elements;
  18. // Audio Bank
  19. protected AudioLibrary audio;
  20. // Spritebatch
  21. protected SpriteBatch spriteBatch = null;
  22. // Gui Stuff
  23. protected Rectangle rockRect = new Rectangle(3, 2, 197, 49);
  24. protected Vector2 rockPosition;
  25. protected Rectangle rainRect = new Rectangle(47, 63, 189, 48);
  26. protected Vector2 rainPosition;
  27. protected Rectangle enhancedRect = new Rectangle(7, 110, 122, 46);
  28. protected Vector2 enhancedPosition;
  29. protected bool showEnhanced;
  30. protected TimeSpan elapsedTime = TimeSpan.Zero;
  31. /// <summary>
  32. /// Default Constructor
  33. /// </summary>
  34. /// <param name="game">Main game object</param>
  35. /// <param name="smallFont">Font for the menu items</param>
  36. /// <param name="largeFont">Font for the menu selcted item</param>
  37. /// <param name="background">Texture for background image</param>
  38. /// <param name="elements">Texture with the foreground elements</param>
  39. public StartScene(Game game, SpriteFont smallFont, SpriteFont largeFont,
  40. Texture2D background,Texture2D elements)
  41. : base(game)
  42. {
  43. this.elements = elements;
  44. Components.Add(new ImageComponent(game, background,
  45. ImageComponent.DrawMode.Stretch));
  46. // Create the Menu
  47. string[] items = {"Play!", "Help", "Quit"};
  48. menu = new TextMenuComponent(game, smallFont, largeFont);
  49. menu.SetMenuItems(items);
  50. Components.Add(menu);
  51. // Get the current spritebatch
  52. spriteBatch = (SpriteBatch) Game.Services.GetService(typeof (SpriteBatch));
  53. // Get the current audiocomponent and play the background music
  54. audio = (AudioLibrary)Game.Services.GetService(typeof(AudioLibrary));
  55. }
  56. /// <summary>
  57. /// Show the start scene
  58. /// </summary>
  59. public override void Show()
  60. {
  61. // GamePad.Visible is not available in MonoGame 3.8.4
  62. // GamePad.Visible = false;
  63. audio.NewMeteor.Play();
  64. rockPosition.X = -1*rockRect.Width;
  65. rockPosition.Y = 20;
  66. rainPosition.X = Game.Window.ClientBounds.Width;
  67. rainPosition.Y = 70;
  68. // Put the menu centered in screen
  69. menu.Position = new Vector2((Game.Window.ClientBounds.Width -
  70. menu.Width)/2, 200);
  71. // These elements will be visible when the 'Rock Rain' title
  72. // is done.
  73. menu.Visible = false;
  74. menu.Enabled = false;
  75. menu.Selected = false;
  76. showEnhanced = false;
  77. base.Show();
  78. }
  79. /// <summary>
  80. /// Hide the start scene
  81. /// </summary>
  82. public override void Hide()
  83. {
  84. MediaPlayer.Stop();
  85. base.Hide();
  86. }
  87. /// <summary>
  88. /// Gets the selected menu option
  89. /// </summary>
  90. public int SelectedMenuIndex
  91. {
  92. get { return menu.SelectedIndex; }
  93. }
  94. public bool MenuSelected
  95. {
  96. get
  97. {
  98. return menu.Selected;
  99. }
  100. }
  101. /// <summary>
  102. /// Allows the game component to update itself.
  103. /// </summary>
  104. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  105. public override void Update(GameTime gameTime)
  106. {
  107. if (!menu.Visible)
  108. {
  109. if (rainPosition.X >= (Game.Window.ClientBounds.Width - rainRect.Width)/2)
  110. {
  111. rainPosition.X -= 5;
  112. }
  113. if (rockPosition.X <= (Game.Window.ClientBounds.Width - rockRect.Width)/2)
  114. {
  115. rockPosition.X += 5;
  116. }
  117. else
  118. {
  119. menu.Visible = true;
  120. menu.Enabled = true;
  121. MediaPlayer.Play(audio.StartMusic);
  122. enhancedPosition =
  123. new Vector2((rainPosition.X + rainRect.Width -
  124. enhancedRect.Width/2) - 40, rainPosition.Y+20);
  125. showEnhanced = true;
  126. }
  127. }
  128. else
  129. {
  130. elapsedTime += gameTime.ElapsedGameTime;
  131. if (elapsedTime > TimeSpan.FromSeconds(1))
  132. {
  133. elapsedTime -= TimeSpan.FromSeconds(1);
  134. showEnhanced = !showEnhanced;
  135. }
  136. }
  137. base.Update(gameTime);
  138. }
  139. /// <summary>
  140. /// Allows the game component to draw itself.
  141. /// </summary>
  142. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  143. public override void Draw(GameTime gameTime)
  144. {
  145. base.Draw(gameTime);
  146. spriteBatch.Draw(elements, rockPosition, rockRect, Color.White);
  147. spriteBatch.Draw(elements, rainPosition, rainRect, Color.White);
  148. if (showEnhanced)
  149. {
  150. spriteBatch.Draw(elements, enhancedPosition, enhancedRect,Color.White);
  151. }
  152. }
  153. }
  154. }