ParticleEffect.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using Microsoft.Xna.Framework;
  9. using MonoGame.Extended.Particles.Primitives;
  10. namespace MonoGame.Extended.Particles;
  11. /// <summary>
  12. /// Represents a complete particle effect composed of multiple emitters.
  13. /// </summary>
  14. /// <remarks>
  15. /// The <see cref="ParticleEffect"/> class serves as a container for one or more <see cref="ParticleEmitter"/> instances,
  16. /// allowing for complex visual effects that combine different types of particle behaviors and appearances.
  17. ///
  18. /// Effects can be positioned, rotated, and scaled as a single unit, with all contained emitters being affected
  19. /// by these transformations. Effects also provide methods for triggering all emitters simultaneously.
  20. /// </remarks>
  21. public class ParticleEffect : IDisposable
  22. {
  23. private float _nextAutoTrigger;
  24. /// <summary>
  25. /// Gets or sets the name of this effect, used for identification and debugging.
  26. /// </summary>
  27. public string Name;
  28. /// <summary>
  29. /// Gets or sets the position of this effect in 2D space.
  30. /// </summary>
  31. /// <remarks>
  32. /// This position is used as the reference point for all emitters in the effect.
  33. /// When the effect is updated, this position is passed to each emitter's update method.
  34. /// </remarks>
  35. public Vector2 Position;
  36. /// <summary>
  37. /// Gets or sets the rotation of this effect, in radians.
  38. /// </summary>
  39. /// <remarks>
  40. /// This property can be used to rotate the entire effect around its position.
  41. /// Note that rotation is not automatically applied to emitters and must be handled by the rendering system.
  42. /// </remarks>
  43. public float Rotation;
  44. /// <summary>
  45. /// Gets or sets the scale factor of this effect.
  46. /// </summary>
  47. /// <remarks>
  48. /// This property can be used to uniformly or non-uniformly scale the entire effect.
  49. /// Note that scaling is not automatically applied to emitters and must be handled by the rendering system.
  50. /// </remarks>
  51. public Vector2 Scale;
  52. /// <summary>
  53. /// A value indicating whether this particle effect should automatically trigger its particle emitters.
  54. /// </summary>
  55. /// <remarks>
  56. /// When <see langword="true"/>, all emitters of this <see cref="ParticleEffect"/> will be triggered at the same
  57. /// based on the <see cref="AutoTriggerFrequency"/>. When <see langword="false"/>, users will need to manually call
  58. /// the <see cref="Trigger()"/> method to trigger emitters.
  59. /// </remarks>
  60. public bool AutoTrigger;
  61. /// <summary>
  62. /// The frequency, in seconds, at which this <see cref="ParticleEffect"/> automatically triggers emitters.
  63. /// </summary>
  64. /// <remarks>
  65. /// If <see cref="AutoTrigger"/> is <see langword="false"/>, this value is ignored.
  66. /// </remarks>
  67. public float AutoTriggerFrequency;
  68. /// <summary>
  69. /// Gets or sets the collection of emitters that compose this effect.
  70. /// </summary>
  71. /// <remarks>
  72. /// Each emitter in this collection contributes to the overall visual appearance of the effect,
  73. /// potentially with different behaviors, textures, and particle properties.
  74. /// </remarks>
  75. public List<ParticleEmitter> Emitters;
  76. /// <summary>
  77. /// Gets a value indicating whether this <see cref="ParticleEffect"/> has been disposed.
  78. /// </summary>
  79. /// <value><see langword="true"/> if the effect has been disposed; otherwise, <see langword="false"/>.</value>
  80. public bool IsDisposed { get; private set; }
  81. /// <summary>
  82. /// Gets the total number of active particles across all emitters in this effect.
  83. /// </summary>
  84. /// <value>The sum of <see cref="ParticleEmitter.ActiveParticles"/> for all emitters in the effect.</value>
  85. public int ActiveParticles
  86. {
  87. get
  88. {
  89. return Emitters.Sum(t => t.ActiveParticles);
  90. }
  91. }
  92. /// <summary>
  93. /// Initializes a new instance of the <see cref="ParticleEffect"/> class with the specified name.
  94. /// </summary>
  95. /// <param name="name">The name of the effect, used for identification and debugging.</param>
  96. /// <remarks>
  97. /// This constructor initializes the effect with default position, rotation, and scale,
  98. /// and creates an empty collection of emitters.
  99. /// </remarks>
  100. public ParticleEffect(string name)
  101. {
  102. Name = name;
  103. Position = Vector2.Zero;
  104. Rotation = 0.0f;
  105. Scale = Vector2.One;
  106. Emitters = new List<ParticleEmitter>();
  107. AutoTrigger = true;
  108. AutoTriggerFrequency = 1.0f;
  109. }
  110. /// <summary>
  111. /// Finalizes an instance of the <see cref="ParticleEffect"/> class.
  112. /// </summary>
  113. ~ParticleEffect()
  114. {
  115. Dispose(false);
  116. }
  117. /// <summary>
  118. /// Advances the effect's state rapidly to simulate it having been active for a period of time.
  119. /// </summary>
  120. /// <param name="position">The position at which to simulate the effect.</param>
  121. /// <param name="seconds">The total time, in seconds, to simulate.</param>
  122. /// <param name="triggerPeriod">The time interval, in seconds, between simulated triggers.</param>
  123. /// <remarks>
  124. /// This method is useful for pre-filling a scene with particles that appear to have been emitted
  125. /// over time, rather than starting with an empty effect.
  126. /// </remarks>
  127. public void FastForward(Vector2 position, float seconds, float triggerPeriod)
  128. {
  129. float time = 0.0f;
  130. while (time < seconds)
  131. {
  132. Update(triggerPeriod);
  133. Trigger(position);
  134. time += triggerPeriod;
  135. }
  136. }
  137. /// <summary>
  138. /// Updates the state of all emitters in this effect.
  139. /// </summary>
  140. /// <param name="gameTime">The timing values for the current update cycle.</param>
  141. /// <exception cref="ObjectDisposedException">
  142. /// Thrown if this method is called after the effect has been disposed.
  143. /// </exception>
  144. public void Update(GameTime gameTime)
  145. {
  146. Update((float)gameTime.ElapsedGameTime.TotalSeconds);
  147. }
  148. /// <summary>
  149. /// Updates the state of all emitters in this effect.
  150. /// </summary>
  151. /// <param name="elapsedSeconds">The elapsed time, in seconds, since the last update.</param>
  152. /// <exception cref="ObjectDisposedException">
  153. /// Thrown if this method is called after the effect has been disposed.
  154. /// </exception>
  155. public void Update(float elapsedSeconds)
  156. {
  157. ObjectDisposedException.ThrowIf(IsDisposed, typeof(ParticleBuffer));
  158. if (AutoTrigger)
  159. {
  160. _nextAutoTrigger -= elapsedSeconds;
  161. if (_nextAutoTrigger <= 0)
  162. {
  163. Trigger();
  164. _nextAutoTrigger += AutoTriggerFrequency;
  165. }
  166. }
  167. for (int i = 0; i < Emitters.Count; i++)
  168. {
  169. Emitters[i].Update(elapsedSeconds, Position);
  170. }
  171. }
  172. /// <summary>
  173. /// Triggers all emitters in this effect at the effect's current position.
  174. /// </summary>
  175. public void Trigger()
  176. {
  177. Trigger(Position);
  178. }
  179. /// <summary>
  180. /// Triggers all emitters in this effect at the specified position.
  181. /// </summary>
  182. /// <param name="position">The position in 2D space at which to trigger the emitters.</param>
  183. /// <param name="layerDepth">The layer depth at which to render the emitted particles.</param>
  184. /// <exception cref="ObjectDisposedException">
  185. /// Thrown if this method is called after the effect has been disposed.
  186. /// </exception>
  187. public void Trigger(Vector2 position, float layerDepth = 0)
  188. {
  189. ObjectDisposedException.ThrowIf(IsDisposed, typeof(ParticleBuffer));
  190. for (int i = 0; i < Emitters.Count; i++)
  191. {
  192. Emitters[i].Trigger(position, layerDepth);
  193. }
  194. }
  195. /// <summary>
  196. /// Triggers all emitters in this effect along a line segment.
  197. /// </summary>
  198. /// <param name="line">The line segment along which to distribute triggered particles.</param>
  199. /// <param name="layerDepth">The layer depth at which to render the emitted particles.</param>
  200. /// <exception cref="ObjectDisposedException">
  201. /// Thrown if this method is called after the effect has been disposed.
  202. /// </exception>
  203. public void Trigger(LineSegment line, float layerDepth)
  204. {
  205. ObjectDisposedException.ThrowIf(IsDisposed, typeof(ParticleBuffer));
  206. for (int i = 0; i < Emitters.Count; i++)
  207. {
  208. Emitters[i].Trigger(line, layerDepth);
  209. }
  210. }
  211. /// <summary>
  212. /// Creates a new instance of the <see cref="ParticleEffect"/> class from a file.
  213. /// </summary>
  214. /// <param name="path">The path to the file containing the serialized effect data.</param>
  215. /// <returns>A new <see cref="ParticleEffect"/> instance with properties and emitters as defined in the file.</returns>
  216. /// <exception cref="NotImplementedException">
  217. /// This method is not yet implemented.
  218. /// </exception>
  219. /// <remarks>
  220. /// This method is intended to deserialize effect data from a file, but has not been implemented in this version.
  221. /// </remarks>
  222. public static ParticleEffect FromFile(string path)
  223. {
  224. throw new NotImplementedException();
  225. }
  226. /// <summary>
  227. /// Creates a new instance of the <see cref="ParticleEffect"/> class from a stream.
  228. /// </summary>
  229. /// <param name="stream">The stream containing the serialized effect data.</param>
  230. /// <returns>A new <see cref="ParticleEffect"/> instance with properties and emitters as defined in the stream.</returns>
  231. /// <exception cref="NotImplementedException">
  232. /// This method is not yet implemented.
  233. /// </exception>
  234. /// <remarks>
  235. /// This method is intended to deserialize effect data from a stream, but has not been implemented in this version.
  236. /// </remarks>
  237. public static ParticleEffect FromStream(Stream stream)
  238. {
  239. throw new NotImplementedException();
  240. }
  241. /// <summary>
  242. /// Releases all resources used by the <see cref="ParticleEffect"/>.
  243. /// </summary>
  244. public void Dispose()
  245. {
  246. Dispose(true);
  247. GC.SuppressFinalize(this);
  248. }
  249. /// <summary>
  250. /// Releases the resources used by the <see cref="ParticleEffect"/>.
  251. /// </summary>
  252. /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources;
  253. /// <see langword="false"/> to release only unmanaged resources.</param>
  254. private void Dispose(bool disposing)
  255. {
  256. if (IsDisposed) { return; }
  257. if (disposing)
  258. {
  259. for (int i = 0; i < Emitters.Count; i++)
  260. {
  261. Emitters[i].Dispose();
  262. }
  263. }
  264. IsDisposed = true;
  265. }
  266. /// <summary>
  267. /// Returns a string that represents the current effect.
  268. /// </summary>
  269. /// <returns>The <see cref="Name"/> of this effect.</returns>
  270. public override string ToString()
  271. {
  272. return Name;
  273. }
  274. }