Game1.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using Tutorial026.Interfaces;
  10. using Tutorial026.Sprites;
  11. namespace Tutorial026
  12. {
  13. /// <summary>
  14. /// This is the main type for your game.
  15. /// </summary>
  16. public class Game1 : Game
  17. {
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20. public static int ScreenWidth = 1280;
  21. public static int ScreenHeight = 720;
  22. private Player _player;
  23. private ObservableCollection<Sprite> _sprites;
  24. private IEnumerable<IMoveable> _worldObjects;
  25. public Game1()
  26. {
  27. graphics = new GraphicsDeviceManager(this);
  28. Content.RootDirectory = "Content";
  29. }
  30. /// <summary>
  31. /// Allows the game to perform any initialization it needs to before starting to run.
  32. /// This is where it can query for any required services and load any non-graphic
  33. /// related content. Calling base.Initialize will enumerate through any components
  34. /// and initialize them as well.
  35. /// </summary>
  36. protected override void Initialize()
  37. {
  38. graphics.PreferredBackBufferWidth = ScreenWidth;
  39. graphics.PreferredBackBufferHeight = ScreenHeight;
  40. graphics.ApplyChanges();
  41. base.Initialize();
  42. }
  43. /// <summary>
  44. /// LoadContent will be called once per game and is the place to load
  45. /// all of your content.
  46. /// </summary>
  47. protected override void LoadContent()
  48. {
  49. // Create a new SpriteBatch, which can be used to draw textures.
  50. spriteBatch = new SpriteBatch(GraphicsDevice);
  51. _player = new Player(Content.Load<Texture2D>("Player/boy"))
  52. {
  53. Position = new Vector2(50, 500),
  54. BaseAttributes = new Models.Attributes()
  55. {
  56. Speed = 3f,
  57. },
  58. };
  59. _sprites = new ObservableCollection<Sprite>();
  60. _sprites.CollectionChanged += _components_CollectionChanged;
  61. _sprites.Add(new Sprite(Content.Load<Texture2D>("Backgrounds/Sky")));
  62. for (int i = 0; i < 100; i++)
  63. {
  64. var powerUp = new PowerUp(Content.Load<Texture2D>("Collectables/snowcog"), new Models.Attributes() { Speed = 1, })
  65. {
  66. Position = new Vector2(200 * i, 500),
  67. };
  68. _sprites.Add(powerUp);
  69. var floorTexture = Content.Load<Texture2D>("Backgrounds/Floor");
  70. _sprites.Add(new Platform(floorTexture) { Position = new Vector2(i * floorTexture.Width, 0) });
  71. }
  72. _sprites.Add(_player);
  73. }
  74. private void _components_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  75. {
  76. _worldObjects = _sprites.Where(c => c is IMoveable).Cast<IMoveable>();
  77. }
  78. /// <summary>
  79. /// UnloadContent will be called once per game and is the place to unload
  80. /// game-specific content.
  81. /// </summary>
  82. protected override void UnloadContent()
  83. {
  84. // TODO: Unload any non ContentManager content here
  85. }
  86. /// <summary>
  87. /// Allows the game to run logic such as updating the world,
  88. /// checking for collisions, gathering input, and playing audio.
  89. /// </summary>
  90. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  91. protected override void Update(GameTime gameTime)
  92. {
  93. foreach (var component in _sprites)
  94. component.Update(gameTime);
  95. CheckCollision();
  96. ApplyPhysics();
  97. RemoveComponents();
  98. base.Update(gameTime);
  99. }
  100. private void CheckCollision()
  101. {
  102. foreach (var sprite in _sprites)
  103. {
  104. if (sprite == _player)
  105. continue;
  106. if (_player.Rectangle.Intersects(sprite.Rectangle))
  107. {
  108. _player.OnCollide(sprite);
  109. }
  110. }
  111. }
  112. private void ApplyPhysics()
  113. {
  114. foreach (var worldObject in _worldObjects)
  115. {
  116. worldObject.Velocity = new Vector2(-_player.TotalAttributes.Speed, worldObject.Velocity.Y);
  117. }
  118. }
  119. private void RemoveComponents()
  120. {
  121. for (int i = 0; i < _sprites.Count; i++)
  122. {
  123. if ((_sprites[i]).IsRemoved)
  124. {
  125. _sprites.RemoveAt(i);
  126. i--;
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// This is called when the game should draw itself.
  132. /// </summary>
  133. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  134. protected override void Draw(GameTime gameTime)
  135. {
  136. GraphicsDevice.Clear(Color.CornflowerBlue);
  137. spriteBatch.Begin();
  138. foreach (var sprite in _sprites)
  139. sprite.Draw(gameTime, spriteBatch);
  140. spriteBatch.End();
  141. base.Draw(gameTime);
  142. }
  143. }
  144. }