ExplosionManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace Asteroid_Belt_Assault
  8. {
  9. class ExplosionManager
  10. {
  11. private Texture2D texture;
  12. private List<Rectangle> pieceRectangles = new List<Rectangle>();
  13. private Rectangle pointRectangle;
  14. private int minPieceCount = 3;
  15. private int maxPieceCount = 6;
  16. private int minPointCount = 20;
  17. private int maxPointCount = 30;
  18. private int durationCount = 90;
  19. private float explosionMaxSpeed = 30f;
  20. private float pieceSpeedScale = 6f;
  21. private int pointSpeedMin = 15;
  22. private int pointSpeedMax = 30;
  23. private Color initialColor = new Color(1.0f, 0.3f, 0f) * 0.5f;
  24. private Color finalColor = new Color(0f, 0f, 0f, 0f);
  25. Random rand = new Random();
  26. private List<Particle> ExplosionParticles = new List<Particle>();
  27. public ExplosionManager(
  28. Texture2D texture,
  29. Rectangle initialFrame,
  30. int pieceCount,
  31. Rectangle pointRectangle)
  32. {
  33. this.texture = texture;
  34. for (int x = 0; x < pieceCount; x++)
  35. {
  36. pieceRectangles.Add(new Rectangle(
  37. initialFrame.X + (initialFrame.Width * x),
  38. initialFrame.Y,
  39. initialFrame.Width,
  40. initialFrame.Height));
  41. }
  42. this.pointRectangle = pointRectangle;
  43. }
  44. public Vector2 randomDirection(float scale)
  45. {
  46. Vector2 direction;
  47. do
  48. {
  49. direction = new Vector2(
  50. rand.Next(0, 101) - 50,
  51. rand.Next(0, 101) - 50);
  52. } while (direction.Length() == 0);
  53. direction.Normalize();
  54. direction *= scale;
  55. return direction;
  56. }
  57. public void AddExplosion(Vector2 location, Vector2 momentum)
  58. {
  59. Vector2 pieceLocation = location -
  60. new Vector2(pieceRectangles[0].Width / 2,
  61. pieceRectangles[0].Height / 2);
  62. int pieces = rand.Next(minPieceCount, maxPieceCount + 1);
  63. for (int x = 0; x < pieces; x++)
  64. {
  65. ExplosionParticles.Add(new Particle(
  66. pieceLocation,
  67. texture,
  68. pieceRectangles[rand.Next(0, pieceRectangles.Count)],
  69. randomDirection(pieceSpeedScale) + momentum,
  70. Vector2.Zero,
  71. explosionMaxSpeed,
  72. durationCount,
  73. initialColor,
  74. finalColor));
  75. }
  76. int points = rand.Next(minPointCount, maxPointCount + 1);
  77. for (int x = 0; x < points; x++)
  78. {
  79. ExplosionParticles.Add(new Particle(
  80. location,
  81. texture,
  82. pointRectangle,
  83. randomDirection((float)rand.Next(
  84. pointSpeedMin, pointSpeedMax)) + momentum,
  85. Vector2.Zero,
  86. explosionMaxSpeed,
  87. durationCount,
  88. initialColor,
  89. finalColor));
  90. }
  91. SoundManager.PlayExplosion();
  92. }
  93. public void Update(GameTime gameTime)
  94. {
  95. for (int x = ExplosionParticles.Count - 1; x >= 0; x--)
  96. {
  97. if (ExplosionParticles[x].IsActive)
  98. {
  99. ExplosionParticles[x].Update(gameTime);
  100. }
  101. else
  102. {
  103. ExplosionParticles.RemoveAt(x);
  104. }
  105. }
  106. }
  107. public void Draw(SpriteBatch spriteBatch)
  108. {
  109. foreach (Particle particle in ExplosionParticles)
  110. {
  111. particle.Draw(spriteBatch);
  112. }
  113. }
  114. }
  115. }