EffectsManager.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 Robot_Rampage
  8. {
  9. static class EffectsManager
  10. {
  11. #region Declarations
  12. static public List<Particle> Effects = new List<Particle>();
  13. static Random rand = new Random();
  14. static public Texture2D Texture;
  15. static public Rectangle ParticleFrame = new Rectangle(0, 288, 2, 2);
  16. static public List<Rectangle> ExplosionFrames =
  17. new List<Rectangle>();
  18. #endregion
  19. #region Initialization
  20. public static void Initialize(
  21. Texture2D texture,
  22. Rectangle particleFrame,
  23. Rectangle explosionFrame,
  24. int explosionFrameCount)
  25. {
  26. Texture = texture;
  27. ParticleFrame = particleFrame;
  28. ExplosionFrames.Clear();
  29. ExplosionFrames.Add(explosionFrame);
  30. for (int x = 1; x < explosionFrameCount; x++)
  31. {
  32. explosionFrame.Offset(explosionFrame.Width, 0);
  33. ExplosionFrames.Add(explosionFrame);
  34. }
  35. }
  36. #endregion
  37. #region Helper Methods
  38. public static Vector2 randomDirection(float scale)
  39. {
  40. Vector2 direction;
  41. do
  42. {
  43. direction = new Vector2(
  44. rand.Next(0, 100) - 50,
  45. rand.Next(0, 100) - 50);
  46. } while (direction.Length() == 0);
  47. direction.Normalize();
  48. direction *= scale;
  49. return direction;
  50. }
  51. #endregion
  52. #region Public Methods
  53. static public void Update(GameTime gameTime)
  54. {
  55. for (int x = Effects.Count - 1; x >= 0; x--)
  56. {
  57. Effects[x].Update(gameTime);
  58. if (Effects[x].Expired)
  59. {
  60. Effects.RemoveAt(x);
  61. }
  62. }
  63. }
  64. static public void Draw(SpriteBatch spriteBatch)
  65. {
  66. foreach (Sprite sprite in Effects)
  67. {
  68. sprite.Draw(spriteBatch);
  69. }
  70. }
  71. public static void AddExplosion(
  72. Vector2 location,
  73. Vector2 momentum,
  74. int minPointCount,
  75. int maxPointCount,
  76. int minPieceCount,
  77. int maxPieceCount,
  78. float pieceSpeedScale,
  79. int duration,
  80. Color initialColor,
  81. Color finalColor)
  82. {
  83. float explosionMaxSpeed = 30f;
  84. int pointSpeedMin = (int)pieceSpeedScale * 2;
  85. int pointSpeedMax = (int)pieceSpeedScale * 3;
  86. Vector2 pieceLocation = location -
  87. new Vector2(ExplosionFrames[0].Width / 2,
  88. ExplosionFrames[0].Height / 2);
  89. int pieces = rand.Next(minPieceCount, maxPieceCount + 1);
  90. for (int x = 0; x < pieces; x++)
  91. {
  92. Effects.Add(new Particle(
  93. pieceLocation,
  94. Texture,
  95. ExplosionFrames[rand.Next(0, ExplosionFrames.Count)],
  96. randomDirection(pieceSpeedScale) + momentum,
  97. Vector2.Zero,
  98. explosionMaxSpeed,
  99. duration,
  100. initialColor,
  101. finalColor));
  102. }
  103. int points = rand.Next(minPointCount, maxPointCount + 1);
  104. for (int x = 0; x < points; x++)
  105. {
  106. Effects.Add(new Particle(
  107. location,
  108. Texture,
  109. ParticleFrame,
  110. randomDirection((float)rand.Next(
  111. pointSpeedMin, pointSpeedMax)) + momentum,
  112. Vector2.Zero,
  113. explosionMaxSpeed,
  114. duration,
  115. initialColor,
  116. finalColor));
  117. }
  118. }
  119. public static void AddExplosion(Vector2 location, Vector2 momentum)
  120. {
  121. AddExplosion(
  122. location,
  123. momentum,
  124. 15,
  125. 20,
  126. 2,
  127. 4,
  128. 6.0f,
  129. 90,
  130. new Color(1.0f, 0.3f, 0f, 0.5f),
  131. new Color(0.0f, 0.0f, 0f, 0f));
  132. }
  133. public static void AddLargeExplosion(Vector2 location)
  134. {
  135. AddExplosion(
  136. location,
  137. Vector2.Zero,
  138. 15,
  139. 20,
  140. 4,
  141. 6,
  142. 30f,
  143. 90,
  144. new Color(1.0f, 0.3f, 0f, 0.5f),
  145. new Color(0.0f, 0.0f, 0f, 0f));
  146. }
  147. public static void AddSparksEffect(
  148. Vector2 location,
  149. Vector2 impactVelocity)
  150. {
  151. int particleCount = rand.Next(10, 20);
  152. for (int x = 0; x < particleCount; x++)
  153. {
  154. Particle particle = new Particle(
  155. location - (impactVelocity / 60),
  156. Texture,
  157. ParticleFrame,
  158. randomDirection((float)rand.Next(10, 20)),
  159. Vector2.Zero,
  160. 60,
  161. 20,
  162. Color.Yellow,
  163. Color.Orange);
  164. Effects.Add(particle);
  165. }
  166. }
  167. #endregion
  168. }
  169. }