GameScene.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Generic;
  2. using Microsoft.Xna.Framework;
  3. namespace RockRain.Core
  4. {
  5. /// <summary>
  6. /// This is the base class for all game scenes.
  7. /// </summary>
  8. public abstract class GameScene : DrawableGameComponent
  9. {
  10. /// <summary>
  11. /// List of child GameComponents
  12. /// </summary>
  13. private readonly List<GameComponent> components;
  14. public GameScene(Game game)
  15. : base(game)
  16. {
  17. components = new List<GameComponent>();
  18. Visible = false;
  19. Enabled = false;
  20. }
  21. /// <summary>
  22. /// Show the scene
  23. /// </summary>
  24. public virtual void Show()
  25. {
  26. Visible = true;
  27. Enabled = true;
  28. }
  29. /// <summary>
  30. /// Hide the scene
  31. /// </summary>
  32. public virtual void Hide()
  33. {
  34. Visible = false;
  35. Enabled = false;
  36. }
  37. /// <summary>
  38. /// Components of Game Scene
  39. /// </summary>
  40. public List<GameComponent> Components
  41. {
  42. get { return components; }
  43. }
  44. /// <summary>
  45. /// Allows the game component to update itself.
  46. /// </summary>
  47. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  48. public override void Update(GameTime gameTime)
  49. {
  50. // Update the child GameComponents
  51. for (int i = 0; i < components.Count; i++)
  52. {
  53. if (components[i].Enabled)
  54. {
  55. components[i].Update(gameTime);
  56. }
  57. }
  58. base.Update(gameTime);
  59. }
  60. /// <summary>
  61. /// Allows the game component draw your content in game screen
  62. /// </summary>
  63. public override void Draw(GameTime gameTime)
  64. {
  65. // Draw the child GameComponents (if drawable)
  66. for (int i = 0; i < components.Count; i++)
  67. {
  68. GameComponent gc = components[i];
  69. if ((gc is DrawableGameComponent) &&
  70. ((DrawableGameComponent) gc).Visible)
  71. {
  72. ((DrawableGameComponent) gc).Draw(gameTime);
  73. }
  74. }
  75. base.Draw(gameTime);
  76. }
  77. }
  78. }