CircleProfile.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 distributes particles throughout a circular area with controllable radiation patterns.
  9. /// </summary>
  10. /// <remarks>
  11. /// The <see cref="CircleProfile"/> randomly positions new particles within a circle centered at the emitter's position.
  12. /// The movement direction (heading) of each particle can be configured to radiate inward toward the center, outward
  13. /// from the center, or in random directions.
  14. /// </remarks>
  15. public sealed class CircleProfile : Profile
  16. {
  17. /// <summary>
  18. /// Gets or sets the radius of the circular area.
  19. /// </summary>
  20. public float Radius { get; set; }
  21. /// <summary>
  22. /// Gets or sets the radiation mode that determines how particle headings are calculated.
  23. /// </summary>
  24. public CircleRadiation Radiate { get; set; }
  25. /// <summary>
  26. /// Computes the offset and heading for a new particle.
  27. /// </summary>
  28. /// <param name="offset">A pointer to the Vector2 where the offset from the emitter position will be stored.</param>
  29. /// <param name="heading">A pointer to the Vector2 where the unit direction vector will be stored.</param>
  30. /// <exception cref="ArgumentOutOfRangeException">
  31. /// Thrown when <see cref="Radiate"/> contains an unsupported value.
  32. /// </exception>
  33. public override unsafe void GetOffsetAndHeading(Vector2* offset, Vector2* heading)
  34. {
  35. float distance = FastRandom.Shared.NextSingle(0f, Radius);
  36. FastRandom.Shared.NextUnitVector(heading);
  37. switch (Radiate)
  38. {
  39. case CircleRadiation.In:
  40. offset->X = -heading->X * distance;
  41. offset->Y = -heading->Y * distance;
  42. break;
  43. case CircleRadiation.Out:
  44. offset->X = heading->X * distance;
  45. offset->Y = heading->Y * distance;
  46. break;
  47. case CircleRadiation.None:
  48. offset->X = heading->X * distance;
  49. offset->Y = heading->Y * distance;
  50. FastRandom.Shared.NextUnitVector(heading);
  51. break;
  52. default:
  53. throw new ArgumentOutOfRangeException(nameof(Radiate), Radiate, "Unsupported radiation mode");
  54. }
  55. }
  56. }