BoxProfile.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 Microsoft.Xna.Framework;
  5. namespace MonoGame.Extended.Particles.Profiles;
  6. /// <summary>
  7. /// A profile that distributes particles along the edges of a rectangular boundary.
  8. /// </summary>
  9. /// <remarks>
  10. /// The <see cref="BoxProfile"/> randomly positions new particles on one of the four sides of a rectangular area
  11. /// centered at the emitter's position. Each side has an equal probability of being selected. Particles are given random
  12. /// unit vector headings, allowing them to move in any direction regardless of their starting edge.
  13. /// </remarks>
  14. public sealed class BoxProfile : Profile
  15. {
  16. /// <summary>
  17. /// Gets or sets the width of the rectangular perimeter.
  18. /// </summary>
  19. public float Width { get; set; }
  20. /// <summary>
  21. /// Gets or sets the height of the rectangular perimeter.
  22. /// </summary>
  23. public float Height { get; set; }
  24. /// <summary>
  25. /// Computes the offset and heading for a new particle.
  26. /// </summary>
  27. /// <param name="offset">A pointer to the Vector2 where the offset from the emitter position will be stored.</param>
  28. /// <param name="heading">A pointer to the Vector2 where the unit direction vector will be stored.</param>
  29. public override unsafe void GetOffsetAndHeading(Vector2* offset, Vector2* heading)
  30. {
  31. switch (FastRandom.Shared.Next(4))
  32. {
  33. case 0: // Left
  34. offset->X = Width * -0.5f;
  35. offset->Y = FastRandom.Shared.NextSingle(Height * -0.5f, Height * 0.5f);
  36. break;
  37. case 1: // Top
  38. offset->X = FastRandom.Shared.NextSingle(Width * -0.5f, Width * 0.5f);
  39. offset->Y = Height * -0.5f;
  40. break;
  41. case 2: // Right
  42. offset->X = Width * 0.5f;
  43. offset->Y = FastRandom.Shared.NextSingle(Height * -0.5f, Height * 0.5f);
  44. break;
  45. default: // Bottom
  46. offset->X = FastRandom.Shared.NextSingle(Width * -0.5f, Width * 0.5f);
  47. offset->Y = Height * 0.5f;
  48. break;
  49. }
  50. FastRandom.Shared.NextUnitVector(heading);
  51. }
  52. }