PlayingState.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Tutorial030.Interfaces;
  10. using Tutorial030.Models;
  11. using Tutorial030.Sprites;
  12. namespace Tutorial030.States
  13. {
  14. public class PlayingState : State
  15. {
  16. private LevelModel _level;
  17. private ObservableCollection<Component> _components;
  18. private IEnumerable<IMoveable> _worldObjects;
  19. private IEnumerable<Sprite> _sprites;
  20. private List<string> _map
  21. {
  22. get
  23. {
  24. return new List<string>()
  25. {
  26. "00000000000000000000000000000000000000",
  27. "00000000000000000000000000000000000000",
  28. "00000000000000000000000000000000000000",
  29. "00000000000000000000000000000000000000",
  30. "00000000000000000000000000000000000000",
  31. "00000000000000000000000000000000000000",
  32. "00000000000000000000000000000000000000",
  33. "00000000000000000000000000000000000000",
  34. "00000000000000000000000000000000000000",
  35. "00000000000000000000000000000000000000",
  36. "00000000000000000000000000000000000000",
  37. "00000000000000000000000000000000000000",
  38. "00000000000000000000000000000000000000",
  39. "00000000000000000000000000000000000000",
  40. "00000000000000000000000110000000000000",
  41. "00000000000000000000011000000000000000",
  42. "00000000000200000001100000000000000000",
  43. "11111111111111111110000000000111111111"
  44. };
  45. }
  46. }
  47. public PlayingState(GameModel gameModel, LevelModel level)
  48. : base(gameModel)
  49. {
  50. _level = level;
  51. }
  52. public override void LoadContent()
  53. {
  54. _components = new ObservableCollection<Component>();
  55. _components.CollectionChanged += UpdateWorldObjects;
  56. _components.Add(_level.Player);
  57. foreach (var sb in _level.ScrollingBackgrounds)
  58. _components.Add(sb);
  59. int y = 0;
  60. foreach (var line in _map)
  61. {
  62. int x = 0;
  63. foreach (var character in line)
  64. {
  65. var texture = _content.Load<Texture2D>("Block");
  66. var platform = new Platform(texture)
  67. {
  68. Position = new Vector2(x * texture.Width, y * texture.Height),
  69. Layer = 0.999f,
  70. };
  71. x++;
  72. if (character == '1')
  73. {
  74. platform.PlatformType = PlatformTypes.Safe;
  75. }
  76. else if (character == '2')
  77. {
  78. platform.PlatformType = PlatformTypes.Dangerous;
  79. platform.Colour = Color.Red;
  80. }
  81. else
  82. {
  83. continue;
  84. }
  85. _components.Add(platform);
  86. }
  87. y++;
  88. }
  89. }
  90. private void UpdateWorldObjects(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  91. {
  92. _worldObjects = _components.Where(c => c is IMoveable).Cast<IMoveable>();
  93. _sprites = _components.Where(c => c is Sprite).Cast<Sprite>();
  94. }
  95. public override void Update(GameTime gameTime)
  96. {
  97. foreach (var component in _components)
  98. component.Update(gameTime);
  99. _level.Emitter?.Update(gameTime);
  100. foreach (var worldObject in _worldObjects)
  101. {
  102. worldObject.Velocity = new Vector2(-_level.Player.Velocity.X, worldObject.Velocity.Y);
  103. }
  104. PostUpdate(gameTime);
  105. }
  106. public void PostUpdate(GameTime gameTime)
  107. {
  108. foreach (var spriteA in _sprites)
  109. {
  110. // Don't do anything if they're the same sprite!
  111. if (spriteA == _level.Player)
  112. continue;
  113. if (_level.Player.IsTouching(spriteA))
  114. {
  115. if (spriteA is Platform)
  116. {
  117. var platform = (Platform)spriteA;
  118. if (platform.PlatformType == PlatformTypes.Dangerous)
  119. {
  120. LoadContent();
  121. break;
  122. }
  123. }
  124. _level.Player.OnCollide(spriteA);
  125. }
  126. }
  127. _level.Player.ApplyVelocity(gameTime);
  128. }
  129. public override void Draw(GameTime gameTime)
  130. {
  131. _spriteBatch.Begin(SpriteSortMode.FrontToBack);
  132. foreach (var component in _components)
  133. component.Draw(gameTime, _spriteBatch);
  134. _spriteBatch.End();
  135. DrawEmitter(gameTime);
  136. }
  137. private void DrawEmitter(GameTime gameTime)
  138. {
  139. if (_level.Emitter == null)
  140. return;
  141. _spriteBatch.Begin();
  142. _level.Emitter.Draw(gameTime, _spriteBatch);
  143. _spriteBatch.End();
  144. }
  145. }
  146. }