Game1.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System.Collections.Generic;
  5. using Tutorial019.Sprites;
  6. namespace Tutorial019
  7. {
  8. /// <summary>
  9. /// This is the main type for your game.
  10. /// </summary>
  11. public class Game1 : Game
  12. {
  13. GraphicsDeviceManager graphics;
  14. SpriteBatch spriteBatch;
  15. private List<Sprite> _sprites;
  16. public Game1()
  17. {
  18. graphics = new GraphicsDeviceManager(this);
  19. Content.RootDirectory = "Content";
  20. }
  21. /// <summary>
  22. /// Allows the game to perform any initialization it needs to before starting to run.
  23. /// This is where it can query for any required services and load any non-graphic
  24. /// related content. Calling base.Initialize will enumerate through any components
  25. /// and initialize them as well.
  26. /// </summary>
  27. protected override void Initialize()
  28. {
  29. // TODO: Add your initialization logic here
  30. base.Initialize();
  31. }
  32. /// <summary>
  33. /// LoadContent will be called once per game and is the place to load
  34. /// all of your content.
  35. /// </summary>
  36. protected override void LoadContent()
  37. {
  38. // Create a new SpriteBatch, which can be used to draw textures.
  39. spriteBatch = new SpriteBatch(GraphicsDevice);
  40. var shipTexture = Content.Load<Texture2D>("Player");
  41. var bulletPrefab = new Bullet(Content.Load<Texture2D>("Bullet"));
  42. _sprites = new List<Sprite>()
  43. {
  44. new Ship(shipTexture)
  45. {
  46. Bullet = bulletPrefab,
  47. Position = new Vector2(100, 100),
  48. Colour = Color.Green,
  49. },
  50. new Sprite(shipTexture)
  51. {
  52. Position = new Vector2(200, 200),
  53. Colour = Color.Red,
  54. },
  55. new Sprite(Content.Load<Texture2D>("Enemy_1"))
  56. {
  57. Position = new Vector2(300, 100),
  58. Colour = Color.Red,
  59. }
  60. };
  61. }
  62. /// <summary>
  63. /// UnloadContent will be called once per game and is the place to unload
  64. /// game-specific content.
  65. /// </summary>
  66. protected override void UnloadContent()
  67. {
  68. // TODO: Unload any non ContentManager content here
  69. }
  70. /// <summary>
  71. /// Allows the game to run logic such as updating the world,
  72. /// checking for collisions, gathering input, and playing audio.
  73. /// </summary>
  74. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  75. protected override void Update(GameTime gameTime)
  76. {
  77. foreach (var sprite in _sprites)
  78. sprite.Update(gameTime);
  79. PostUpdate(gameTime);
  80. base.Update(gameTime);
  81. }
  82. protected void PostUpdate(GameTime gameTime)
  83. {
  84. // 1. Check collision between all current "Sprites"
  85. // 2. Add "Children" to the list of "_sprites" and clear
  86. // 3. Remove all "IsRemoved" sprites
  87. foreach (var spriteA in _sprites)
  88. {
  89. foreach (var spriteB in _sprites)
  90. {
  91. if (spriteA == spriteB)
  92. continue;
  93. if (spriteA.Intersects(spriteB))
  94. spriteA.OnCollide(spriteB);
  95. }
  96. }
  97. int count = _sprites.Count;
  98. for (int i = 0; i < count; i++)
  99. {
  100. foreach (var child in _sprites[i].Children)
  101. _sprites.Add(child);
  102. _sprites[i].Children.Clear();
  103. }
  104. for (int i = 0; i < _sprites.Count; i++)
  105. {
  106. if (_sprites[i].IsRemoved)
  107. {
  108. _sprites.RemoveAt(i);
  109. i--;
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// This is called when the game should draw itself.
  115. /// </summary>
  116. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  117. protected override void Draw(GameTime gameTime)
  118. {
  119. GraphicsDevice.Clear(Color.CornflowerBlue);
  120. spriteBatch.Begin();
  121. foreach (var sprite in _sprites)
  122. sprite.Draw(gameTime, spriteBatch);
  123. spriteBatch.End();
  124. base.Draw(gameTime);
  125. }
  126. }
  127. }