AgeModifier.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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.Collections.Generic;
  5. using MonoGame.Extended.Particles.Data;
  6. using MonoGame.Extended.Particles.Modifiers.Interpolators;
  7. namespace MonoGame.Extended.Particles.Modifiers;
  8. /// <summary>
  9. /// A modifier that applies multiple interpolators to particles based on their age.
  10. /// </summary>
  11. /// <remarks>
  12. /// The <see cref="AgeModifier"/> controls how particle properties change over their lifetime
  13. /// by applying a collection of <see cref="Interpolator"/> objects to each particle. Each interpolator
  14. /// in the collection operates on a different property of the particle (such as color, scale, or opacity),
  15. /// creating complex, time-based transformations.
  16. ///
  17. /// Unlike other modifiers that apply incremental changes each frame, interpolators directly compute
  18. /// the target property values based on the particle's current age as a fraction of its total lifespan.
  19. /// This provides more predictable and consistent results regardless of frame rate.
  20. /// </remarks>
  21. public class AgeModifier : Modifier
  22. {
  23. /// <summary>
  24. /// Gets or sets the collection of interpolators that will be applied to particles.
  25. /// </summary>
  26. public List<Interpolator> Interpolators { get; set; } = new List<Interpolator>();
  27. /// <summary>
  28. /// Updates all particles by applying each interpolator in the collection to each particle.
  29. /// </summary>
  30. /// <inheritdoc/>
  31. public override unsafe void Update(float elapsedSeconds, ParticleIterator iterator)
  32. {
  33. if (!Enabled) { return; }
  34. while (iterator.HasNext)
  35. {
  36. Particle* particle = iterator.Next();
  37. for (int i = 0; i < Interpolators.Count; i++)
  38. {
  39. Interpolators[i].Update(particle->Age, particle);
  40. }
  41. }
  42. }
  43. }