SprayProfile.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 Microsoft.Xna.Framework;
  6. namespace MonoGame.Extended.Particles.Profiles;
  7. /// <summary>
  8. /// A profile that emits particles from a single point in a directional cone pattern.
  9. /// </summary>
  10. /// <remarks>
  11. /// The <see cref="SprayProfile"/> positions all particles exactly at the emitter's position,
  12. /// like <see cref="PointProfile"/>, but instead of random directions in all directions, it constrains the particle
  13. /// headings to a cone-shaped area defined by a central direction and spread angle.
  14. /// </remarks>
  15. public sealed class SprayProfile : Profile
  16. {
  17. /// <summary>
  18. /// Gets or sets the central direction vector of the spray.
  19. /// </summary>
  20. public Vector2 Direction { get; set; }
  21. /// <summary>
  22. /// Gets or sets the angular spread of the spray cone (in radians).
  23. /// </summary>
  24. /// <remarks>
  25. /// This value determines how wide the spray cone is. For example:
  26. ///
  27. /// <list type="bullet">
  28. /// <item>A value of 0 will emit all particles in exactly the same direction.</item>
  29. /// <item>A value of π (Pi) will create a 180-degree fan.</item>
  30. /// <item>A value of 2π will emit in all directions (similar to <see cref="PointProfile"/>).</item>
  31. /// </list>
  32. /// </remarks>
  33. public float Spread { get; set; }
  34. /// <summary>
  35. /// Computes the offset and heading for a new particle.
  36. /// </summary>
  37. /// <param name="offset">A pointer to the Vector2 where the offset from the emitter position will be stored.</param>
  38. /// <param name="heading">A pointer to the Vector2 where the unit direction vector will be stored.</param>
  39. /// <remarks>
  40. /// The offset is always set to (0,0), meaning particles will spawn exactly at the emitter position.
  41. /// </remarks>
  42. public override unsafe void GetOffsetAndHeading(Vector2* offset, Vector2* heading)
  43. {
  44. offset->X = offset->Y = 0.0f;
  45. float angle = MathF.Atan2(Direction.Y, Direction.X);
  46. angle = FastRandom.Shared.NextSingle(angle - Spread * 0.5f, angle + Spread * 0.5f);
  47. heading->X = MathF.Cos(angle);
  48. heading->Y = MathF.Sin(angle);
  49. }
  50. }