Game1.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 Tutorial030.Emitters;
  7. using Tutorial030.Misc;
  8. using Tutorial030.Models;
  9. using Tutorial030.Sprites;
  10. using Tutorial030.States;
  11. namespace Tutorial030
  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. public static Random Random;
  23. private GameModel _gameModel;
  24. private State _currentState;
  25. private LevelModel _sunnyLevel;
  26. public Game1()
  27. {
  28. graphics = new GraphicsDeviceManager(this);
  29. Content.RootDirectory = "Content";
  30. }
  31. /// <summary>
  32. /// Allows the game to perform any initialization it needs to before starting to run.
  33. /// This is where it can query for any required services and load any non-graphic
  34. /// related content. Calling base.Initialize will enumerate through any components
  35. /// and initialize them as well.
  36. /// </summary>
  37. protected override void Initialize()
  38. {
  39. graphics.PreferredBackBufferWidth = ScreenWidth;
  40. graphics.PreferredBackBufferHeight = ScreenHeight;
  41. graphics.ApplyChanges();
  42. Random = new Random();
  43. IsMouseVisible = true;
  44. base.Initialize();
  45. }
  46. /// <summary>
  47. /// LoadContent will be called once per game and is the place to load
  48. /// all of your content.
  49. /// </summary>
  50. protected override void LoadContent()
  51. {
  52. // Create a new SpriteBatch, which can be used to draw textures.
  53. spriteBatch = new SpriteBatch(GraphicsDevice);
  54. _gameModel = new GameModel()
  55. {
  56. ContentManger = Content,
  57. GraphicsDeviceManager = graphics,
  58. SpriteBatch = spriteBatch,
  59. };
  60. _currentState = new LevelSelectorState(_gameModel);
  61. _currentState.LoadContent();
  62. var player = new Player(new Dictionary<string, Animation>()
  63. {
  64. { "Running", new Animation(Content.Load<Texture2D>("Player/Running"), 4) },
  65. { "Jumping", new Animation(Content.Load<Texture2D>("Player/Jumping"), 4) },
  66. { "Falling", new Animation(Content.Load<Texture2D>("Player/Falling"), 4) },
  67. })
  68. {
  69. BaseAttributes = new Attributes()
  70. {
  71. Speed = 3f,
  72. },
  73. Position = new Vector2(50, 300),
  74. Layer = 1f,
  75. };
  76. _sunnyLevel = new LevelModel(player)
  77. {
  78. //Emitter = new SnowEmitter(new Particle(Content.Load<Texture2D>("Particles/Snow"))),
  79. ScrollingBackgrounds = new List<ScrollingBackground>()
  80. {
  81. new ScrollingBackground(Content.Load<Texture2D>("Levels/Sunny/Trees"), player, 60f)
  82. {
  83. Layer = 0.99f,
  84. },
  85. new ScrollingBackground(Content.Load<Texture2D>("Levels/Sunny/Floor"), player, 60f)
  86. {
  87. Layer = 0.9f,
  88. },
  89. new ScrollingBackground(Content.Load<Texture2D>("Levels/Sunny/Hills_Front"), player, 40f)
  90. {
  91. Layer = 0.8f,
  92. },
  93. new ScrollingBackground(Content.Load<Texture2D>("Levels/Sunny/Hills_Middle"), player, 30f)
  94. {
  95. Layer = 0.79f,
  96. },
  97. new ScrollingBackground(Content.Load<Texture2D>("Levels/Sunny/Clouds_Fast"), player, 25f, true)
  98. {
  99. Layer = 0.78f,
  100. },
  101. new ScrollingBackground(Content.Load<Texture2D>("Levels/Sunny/Hills_Back"), player, 0f)
  102. {
  103. Layer = 0.77f,
  104. },
  105. new ScrollingBackground(Content.Load<Texture2D>("Levels/Sunny/Clouds_Slow"), player, 10f, true)
  106. {
  107. Layer = 0.7f,
  108. },
  109. new ScrollingBackground(Content.Load<Texture2D>("Levels/Sunny/Sky"), player, 0f)
  110. {
  111. Layer = 0.1f,
  112. },
  113. }
  114. };
  115. }
  116. /// <summary>
  117. /// UnloadContent will be called once per game and is the place to unload
  118. /// game-specific content.
  119. /// </summary>
  120. protected override void UnloadContent()
  121. {
  122. // TODO: Unload any non ContentManager content here
  123. }
  124. /// <summary>
  125. /// Allows the game to run logic such as updating the world,
  126. /// checking for collisions, gathering input, and playing audio.
  127. /// </summary>
  128. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  129. protected override void Update(GameTime gameTime)
  130. {
  131. switch (_currentState)
  132. {
  133. case LevelSelectorState levelSelectorState:
  134. _currentState = new PlayingState(_gameModel, _sunnyLevel);
  135. _currentState.LoadContent();
  136. break;
  137. case CustomiseState customiseState:
  138. break;
  139. case PlayingState playingState:
  140. break;
  141. default:
  142. throw new Exception("Unknown state: " + _currentState.ToString());
  143. }
  144. _currentState.Update(gameTime);
  145. base.Update(gameTime);
  146. }
  147. /// <summary>
  148. /// This is called when the game should draw itself.
  149. /// </summary>
  150. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  151. protected override void Draw(GameTime gameTime)
  152. {
  153. GraphicsDevice.Clear(Color.CornflowerBlue);
  154. _currentState.Draw(gameTime);
  155. base.Draw(gameTime);
  156. }
  157. }
  158. }