Modifier.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. namespace MonoGame.Extended.Particles.Modifiers;
  5. /// <summary>
  6. /// Represents a base class for all particle modifiers.
  7. /// </summary>
  8. /// <remarks>
  9. /// Particle modifiers are used to alter the behavior or properties of particles during their lifetime.
  10. /// Each modifier applies changes to particles at a configurable frequency, optimizing performance by
  11. /// spreading updates across frames when appropriate.
  12. /// Custom modifiers should inherit from this class and implement the <see cref="Update"/> method.
  13. /// </remarks>
  14. public abstract class Modifier
  15. {
  16. private const float DEFAULT_MODIFIER_FREQUENCY = 60.0f;
  17. /// <summary>
  18. /// Gets or sets the display name of this modifier.
  19. /// </summary>
  20. public string Name;
  21. /// <summary>
  22. /// Gets or sets the update frequency of this modifier.
  23. /// </summary>
  24. /// <remarks>
  25. /// This value defines how often, in times per second, the modifier attempts to update
  26. /// the entire particle buffer. For example, a value of 60.0f means that all particles
  27. /// will be updated collectively approximately 60 times per second.
  28. ///
  29. /// To improve performance, updates are distributed across frames. Rather than updating
  30. /// every particle in every frame, the modifier mathematically distributes updates by
  31. /// processing a portion of the particles each frame based on the elapsed time and the
  32. /// desired frequency. Over time, this results in all particles being updated at the
  33. /// specified frequency on average, regardless of the actual frame rate.
  34. ///
  35. /// Higher values result in more frequent updates and smoother particle behavior, at the
  36. /// cost of performance. Lower values reduce CPU usage but may make particle changes appear
  37. /// less fluid.
  38. /// </remarks>
  39. public float Frequency;
  40. /// <summary>
  41. /// Indicates whether this modifier is enabled.
  42. /// </summary>
  43. /// <remarks>
  44. /// This value determines if this modifier is enabled. When a modifier is disabled, the modifier is not applied
  45. /// to the particles.
  46. /// </remarks>
  47. public bool Enabled;
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="Modifier"/> class.
  50. /// </summary>
  51. /// <remarks>
  52. /// The default constructor sets the <see cref="Name"/> property to the name of the derived class
  53. /// and initializes <see cref="Frequency"/> to <see cref="DEFAULT_MODIFIER_FREQUENCY"/>.
  54. /// </remarks>
  55. protected Modifier()
  56. {
  57. Name = GetType().Name;
  58. Frequency = DEFAULT_MODIFIER_FREQUENCY;
  59. Enabled = true;
  60. }
  61. /// <summary>
  62. /// Updates the properties of particles according to this modifier's specific behavior.
  63. /// </summary>
  64. /// <param name="elapsedSeconds">The elapsed time, in seconds, since the last update.</param>
  65. /// <param name="iterator">The iterator used to iterate the particles ot update.</param>
  66. public abstract void Update(float elapsedSeconds, ParticleIterator iterator);
  67. }