GameState.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Tutorial020.Sprites;
  10. using Microsoft.Xna.Framework.Input;
  11. using Tutorial020.Managers;
  12. namespace Tutorial020.States
  13. {
  14. public class GameState : State
  15. {
  16. private EnemyManager _enemyManager;
  17. private SpriteFont _font;
  18. private List<Player> _players;
  19. private ScoreManager _scoreManager;
  20. private List<Sprite> _sprites;
  21. public int PlayerCount;
  22. public GameState(Game1 game, ContentManager content)
  23. : base(game, content)
  24. {
  25. }
  26. public override void LoadContent()
  27. {
  28. var playerTexture = _content.Load<Texture2D>("Ships/Player");
  29. var bulletTexture = _content.Load<Texture2D>("Bullet");
  30. _font = _content.Load<SpriteFont>("Font");
  31. _scoreManager = ScoreManager.Load();
  32. _sprites = new List<Sprite>()
  33. {
  34. new Sprite(_content.Load<Texture2D>("Background/Game"))
  35. {
  36. Layer = 0.0f,
  37. Position = new Vector2(Game1.ScreenWidth / 2, Game1.ScreenHeight / 2),
  38. }
  39. };
  40. var bulletPrefab = new Bullet(bulletTexture)
  41. {
  42. Explosion = new Explosion(new Dictionary<string, Models.Animation>()
  43. {
  44. { "Explode", new Models.Animation(_content.Load<Texture2D>("Explosion"), 3) { FrameSpeed = 0.1f, } }
  45. })
  46. {
  47. Layer = 0.5f,
  48. }
  49. };
  50. if (PlayerCount >= 1)
  51. {
  52. _sprites.Add(new Player(playerTexture)
  53. {
  54. Colour = Color.Blue,
  55. Position = new Vector2(100, 50),
  56. Layer = 0.3f,
  57. Bullet = bulletPrefab,
  58. Input = new Models.Input()
  59. {
  60. Up = Keys.W,
  61. Down = Keys.S,
  62. Left = Keys.A,
  63. Right = Keys.D,
  64. Shoot = Keys.Space,
  65. },
  66. Health = 10,
  67. Score = new Models.Score()
  68. {
  69. PlayerName = "Player 1",
  70. Value = 0,
  71. },
  72. });
  73. }
  74. if (PlayerCount >= 2)
  75. {
  76. _sprites.Add(new Player(playerTexture)
  77. {
  78. Colour = Color.Green,
  79. Position = new Vector2(100, 200),
  80. Layer = 0.4f,
  81. Bullet = bulletPrefab,
  82. Input = new Models.Input()
  83. {
  84. Up = Keys.Up,
  85. Down = Keys.Down,
  86. Left = Keys.Left,
  87. Right = Keys.Right,
  88. Shoot = Keys.Enter,
  89. },
  90. Health = 10,
  91. Score = new Models.Score()
  92. {
  93. PlayerName = "Player 2",
  94. Value = 0,
  95. },
  96. });
  97. }
  98. _players = _sprites.Where(c => c is Player).Select(c => (Player)c).ToList();
  99. _enemyManager = new EnemyManager(_content)
  100. {
  101. Bullet = bulletPrefab,
  102. };
  103. }
  104. public override void Update(GameTime gameTime)
  105. {
  106. if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  107. _game.ChangeState(new MenuState(_game, _content));
  108. foreach (var sprite in _sprites)
  109. sprite.Update(gameTime);
  110. _enemyManager.Update(gameTime);
  111. if (_enemyManager.CanAdd && _sprites.Where(c => c is Enemy).Count() < _enemyManager.MaxEnemies)
  112. {
  113. _sprites.Add(_enemyManager.GetEnemy());
  114. }
  115. }
  116. public override void PostUpdate(GameTime gameTime)
  117. {
  118. var collidableSprites = _sprites.Where(c => c is ICollidable);
  119. foreach (var spriteA in collidableSprites)
  120. {
  121. foreach (var spriteB in collidableSprites)
  122. {
  123. // Don't do anything if they're the same sprite!
  124. if (spriteA == spriteB)
  125. continue;
  126. if (!spriteA.CollisionArea.Intersects(spriteB.CollisionArea))
  127. continue;
  128. if (spriteA.Intersects(spriteB))
  129. ((ICollidable)spriteA).OnCollide(spriteB);
  130. }
  131. }
  132. // Add the children sprites to the list of sprites (ie bullets)
  133. int spriteCount = _sprites.Count;
  134. for (int i = 0; i < spriteCount; i++)
  135. {
  136. var sprite = _sprites[i];
  137. foreach (var child in sprite.Children)
  138. _sprites.Add(child);
  139. sprite.Children = new List<Sprite>();
  140. }
  141. for (int i = 0; i < _sprites.Count; i++)
  142. {
  143. if (_sprites[i].IsRemoved)
  144. {
  145. _sprites.RemoveAt(i);
  146. i--;
  147. }
  148. }
  149. // If all the players are dead, we save the scores, and return to the highscore state
  150. if (_players.All(c => c.IsDead))
  151. {
  152. foreach (var player in _players)
  153. _scoreManager.Add(player.Score);
  154. ScoreManager.Save(_scoreManager);
  155. _game.ChangeState(new HighscoresState(_game, _content));
  156. }
  157. }
  158. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  159. {
  160. spriteBatch.Begin(SpriteSortMode.FrontToBack);
  161. foreach (var sprite in _sprites)
  162. sprite.Draw(gameTime, spriteBatch);
  163. spriteBatch.End();
  164. spriteBatch.Begin();
  165. float x = 10f;
  166. foreach (var player in _players)
  167. {
  168. spriteBatch.DrawString(_font, "Player: " + player.Score.PlayerName, new Vector2(x, 10f), Color.White);
  169. spriteBatch.DrawString(_font, "Health: " + player.Health, new Vector2(x, 30f), Color.White);
  170. spriteBatch.DrawString(_font, "Score: " + player.Score.Value, new Vector2(x, 50f), Color.White);
  171. x += 150;
  172. }
  173. spriteBatch.End();
  174. }
  175. }
  176. }