StartScene.cs 5.8 KB

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