Game1.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System.Collections.Generic;
  5. using Tutorial014.Core;
  6. using Tutorial014.Sprites;
  7. namespace Tutorial014
  8. {
  9. /// <summary>
  10. /// This is the main type for your game.
  11. /// </summary>
  12. public class Game1 : Game
  13. {
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16. private Camera _camera;
  17. private List<Component> _components;
  18. private Player _player;
  19. public static int ScreenHeight;
  20. public static int ScreenWidth;
  21. public Game1()
  22. {
  23. graphics = new GraphicsDeviceManager(this);
  24. Content.RootDirectory = "Content";
  25. }
  26. /// <summary>
  27. /// Allows the game to perform any initialization it needs to before starting to run.
  28. /// This is where it can query for any required services and load any non-graphic
  29. /// related content. Calling base.Initialize will enumerate through any components
  30. /// and initialize them as well.
  31. /// </summary>
  32. protected override void Initialize()
  33. {
  34. ScreenHeight = graphics.PreferredBackBufferHeight;
  35. ScreenWidth = graphics.PreferredBackBufferWidth;
  36. base.Initialize();
  37. }
  38. /// <summary>
  39. /// LoadContent will be called once per game and is the place to load
  40. /// all of your content.
  41. /// </summary>
  42. protected override void LoadContent()
  43. {
  44. // Create a new SpriteBatch, which can be used to draw textures.
  45. spriteBatch = new SpriteBatch(GraphicsDevice);
  46. _camera = new Camera();
  47. _player = new Player(Content.Load<Texture2D>("Player"));
  48. _components = new List<Component>()
  49. {
  50. new Sprite(Content.Load<Texture2D>("Background")),
  51. _player,
  52. new Sprite(Content.Load<Texture2D>("NPC")),
  53. };
  54. }
  55. /// <summary>
  56. /// UnloadContent will be called once per game and is the place to unload
  57. /// game-specific content.
  58. /// </summary>
  59. protected override void UnloadContent()
  60. {
  61. // TODO: Unload any non ContentManager content here
  62. }
  63. /// <summary>
  64. /// Allows the game to run logic such as updating the world,
  65. /// checking for collisions, gathering input, and playing audio.
  66. /// </summary>
  67. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  68. protected override void Update(GameTime gameTime)
  69. {
  70. foreach (var component in _components)
  71. component.Update(gameTime);
  72. _camera.Follow(_player);
  73. base.Update(gameTime);
  74. }
  75. /// <summary>
  76. /// This is called when the game should draw itself.
  77. /// </summary>
  78. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  79. protected override void Draw(GameTime gameTime)
  80. {
  81. GraphicsDevice.Clear(Color.CornflowerBlue);
  82. spriteBatch.Begin(transformMatrix: _camera.Transform);
  83. foreach (var component in _components)
  84. component.Draw(gameTime, spriteBatch);
  85. spriteBatch.End();
  86. base.Draw(gameTime);
  87. }
  88. }
  89. }