PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/DashXna/DashXna/Game/IntroScreen.cs

https://github.com/timfel/dash
C# | 107 lines | 76 code | 19 blank | 12 comment | 3 complexity | 1a5935bb5a101b9150e27d8467e017fe MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using System.Xml.Linq;
  12. using Microsoft.Xna.Framework.Input.Touch;
  13. namespace Dash
  14. {
  15. /// <summary>
  16. /// This is a game component that implements IUpdateable.
  17. /// </summary>
  18. public class IntroScreen : Microsoft.Xna.Framework.DrawableGameComponent
  19. {
  20. SpriteBatch spriteBatch;
  21. SpriteFont font;
  22. Texture2D background;
  23. string story;
  24. Vector2 textSize;
  25. double textOffset;
  26. double opacity = 1;
  27. public IntroScreen(Game game)
  28. : base(game)
  29. {
  30. DrawOrder = 15;
  31. }
  32. /// <summary>
  33. /// Allows the game component to perform any initialization it needs to before starting
  34. /// to run. This is where it can query for any required services and load content.
  35. /// </summary>
  36. public override void Initialize()
  37. {
  38. // TODO: Add your initialization code here
  39. base.Initialize();
  40. }
  41. protected override void LoadContent()
  42. {
  43. font = Game.Content.Load<SpriteFont>("Default");
  44. spriteBatch = new SpriteBatch(Game.GraphicsDevice);
  45. background = Game.Content.Load<Texture2D>("StoryBackground");
  46. XDocument doc = XDocument.Load(TitleContainer.OpenStream("Content\\Intro-Story.xml"));
  47. story = doc.Descendants("story").First().Element("text").Value;
  48. textSize = font.MeasureString(story);
  49. textOffset = 500;
  50. base.LoadContent();
  51. }
  52. /// <summary>
  53. /// Allows the game component to update itself.
  54. /// </summary>
  55. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  56. public override void Update(GameTime gameTime)
  57. {
  58. textOffset -= gameTime.ElapsedGameTime.TotalSeconds * 40;
  59. if (TouchPanel.GetState().Any())
  60. textOffset = -5000;
  61. if (textOffset < -textSize.Y)
  62. {
  63. if (opacity > 0)
  64. {
  65. opacity -= gameTime.ElapsedGameTime.TotalSeconds * 0.5f;
  66. }
  67. else
  68. {
  69. this.Enabled = false;
  70. this.Visible = false;
  71. (Game.Services.GetService(typeof(GameplayComponent)) as GameplayComponent).Enabled = true;
  72. }
  73. }
  74. base.Update(gameTime);
  75. }
  76. public override void Draw(GameTime gameTime)
  77. {
  78. var transform = Matrix.Identity;
  79. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default,
  80. RasterizerState.CullNone, null, transform);
  81. spriteBatch.Draw(background, new Vector2(0, 0), new Color(1, 1, 1, (float)opacity));
  82. spriteBatch.DrawString(font, "The Story (Tap to skip)", new Vector2(40, (float)textOffset - 10), Color.Gray);
  83. spriteBatch.DrawString(font, story, new Vector2(40, (int)(10 + textOffset)), Color.White);
  84. spriteBatch.End();
  85. base.Draw(gameTime);
  86. }
  87. }
  88. }