ParticleEffectManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //-----------------------------------------------------------------------------
  2. // ParticleEffectManager.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Content;
  13. namespace NetRumble
  14. {
  15. public class ParticleEffectManager
  16. {
  17. /// <summary>
  18. /// Cache of registered particle effects.
  19. /// </summary>
  20. private Dictionary<ParticleEffectType, List<ParticleEffect>> particleEffectCache
  21. = new Dictionary<ParticleEffectType,List<ParticleEffect>>();
  22. /// <summary>
  23. /// Active particle effects.
  24. /// </summary>
  25. private BatchRemovalCollection<ParticleEffect> activeParticleEffects =
  26. new BatchRemovalCollection<ParticleEffect>();
  27. /// <summary>
  28. /// The content manager used to load textures in the particle systems.
  29. /// </summary>
  30. private ContentManager contentManager;
  31. /// <summary>
  32. /// Construct a new particle-effect manager.
  33. /// </summary>
  34. public ParticleEffectManager(ContentManager contentManager)
  35. {
  36. // safety-check the parameters
  37. if (contentManager == null)
  38. {
  39. throw new ArgumentNullException("contentManager");
  40. }
  41. this.contentManager = contentManager;
  42. }
  43. /// <summary>
  44. /// Update the particle-effect manager.
  45. /// </summary>
  46. /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param>
  47. public void Update(float elapsedTime)
  48. {
  49. for (int i = 0; i < activeParticleEffects.Count; ++i)
  50. {
  51. if (activeParticleEffects[i].Active)
  52. {
  53. activeParticleEffects[i].Update(elapsedTime);
  54. if (!activeParticleEffects[i].Active)
  55. {
  56. activeParticleEffects.QueuePendingRemoval(
  57. activeParticleEffects[i]);
  58. }
  59. }
  60. }
  61. activeParticleEffects.ApplyPendingRemovals();
  62. }
  63. /// <summary>
  64. /// Draw all of the particle effects in the manager.
  65. /// </summary>
  66. /// <param name="spriteBatch">The SpriteBatch object used to draw.</param>
  67. /// <param name="blendMode">Filters the systems drawn in this pass.</param>
  68. public virtual void Draw(SpriteBatch spriteBatch, SpriteBlendMode blendMode)
  69. {
  70. for (int i = 0; i < activeParticleEffects.Count; ++i)
  71. {
  72. if (activeParticleEffects[i].Active)
  73. {
  74. activeParticleEffects[i].Draw(spriteBatch, blendMode);
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// Spawn a new particle effect at a given location
  80. /// </summary>
  81. /// <param name="effectType">The effect in question.</param>
  82. /// <param name="position">The position of the effect.</param>
  83. /// <returns>The new particle effect.</returns>
  84. public ParticleEffect SpawnEffect(ParticleEffectType effectType,
  85. Vector2 position)
  86. {
  87. return SpawnEffect(effectType, position, null);
  88. }
  89. /// <summary>
  90. /// Spawn a new particle effect at a the position of a given gameplay object
  91. /// </summary>
  92. /// <param name="effectType">The effect in question.</param>
  93. /// <param name="actor">The gameplay object.</param>
  94. /// <returns>The new particle effect.</returns>
  95. public ParticleEffect SpawnEffect(ParticleEffectType effectType,
  96. GameplayObject gameplayObject)
  97. {
  98. // safety-check the parameter
  99. if (gameplayObject == null)
  100. {
  101. throw new ArgumentNullException("gameplayObject");
  102. }
  103. return SpawnEffect(effectType, gameplayObject.Position, gameplayObject);
  104. }
  105. /// <summary>
  106. /// Spawn a new particle effect at a given location and gameplay object
  107. /// </summary>
  108. /// <param name="effectType">The effect in question.</param>
  109. /// <param name="position">The position of the effect.</param>
  110. /// <param name="actor">The gameplay object.</param>
  111. /// <returns>The new particle effect.</returns>
  112. public ParticleEffect SpawnEffect(ParticleEffectType effectType,
  113. Vector2 position, GameplayObject gameplayObject)
  114. {
  115. ParticleEffect particleEffect = null;
  116. if (particleEffectCache.ContainsKey(effectType) == true)
  117. {
  118. List<ParticleEffect> availableSystems = particleEffectCache[effectType];
  119. for (int i = 0; i < availableSystems.Count; ++i)
  120. {
  121. if (availableSystems[i].Active == false)
  122. {
  123. particleEffect = availableSystems[i];
  124. break;
  125. }
  126. }
  127. if (particleEffect == null)
  128. {
  129. particleEffect = availableSystems[0].Clone();
  130. particleEffect.Initialize(contentManager);
  131. availableSystems.Add(particleEffect);
  132. }
  133. }
  134. if (particleEffect != null)
  135. {
  136. particleEffect.Reset();
  137. particleEffect.GameplayObject = gameplayObject;
  138. particleEffect.Position = position;
  139. activeParticleEffects.Add(particleEffect);
  140. }
  141. return particleEffect;
  142. }
  143. /// <summary>
  144. /// Register a new type of particle effect with the manager.
  145. /// </summary>
  146. /// <param name="effectType">The enumeration associated with this type.</param>
  147. /// <param name="filename">The path to the XML file to be deserialized.</param>
  148. /// <param name="initialCount">How many of these to pre-create.</param>
  149. public void RegisterParticleEffect(ParticleEffectType effectType,
  150. string filename, int initialCount)
  151. {
  152. if (!particleEffectCache.ContainsKey(effectType))
  153. {
  154. string filepath = Path.Combine(contentManager.RootDirectory, filename);
  155. ParticleEffect particleEffect = ParticleEffect.Load(filepath);
  156. particleEffect.Initialize(contentManager);
  157. particleEffect.Stop(true);
  158. particleEffectCache.Add(effectType, new List<ParticleEffect>());
  159. particleEffectCache[effectType].Add(particleEffect);
  160. for (int i = 1; i < initialCount; i++)
  161. {
  162. ParticleEffect cloneEffect = particleEffect.Clone();
  163. cloneEffect.Initialize(contentManager);
  164. cloneEffect.Stop(true);
  165. particleEffectCache[effectType].Add(cloneEffect);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// Remove the given particle-effect type from the maanger.
  171. /// </summary>
  172. /// <param name="effectType">The enumeration to be cleared against.</param>
  173. public void UnregisterParticleEffect(ParticleEffectType effectType)
  174. {
  175. if (particleEffectCache.ContainsKey(effectType) == true)
  176. {
  177. for (int i = 0; i < particleEffectCache[effectType].Count; ++i)
  178. {
  179. activeParticleEffects.Remove(particleEffectCache[effectType][i]);
  180. }
  181. particleEffectCache.Remove(effectType);
  182. }
  183. }
  184. }
  185. }