ParticleCache.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ParticleCache.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. #endregion
  13. namespace NetRumble
  14. {
  15. /// <summary>
  16. /// Cache of Particle objects.
  17. /// </summary>
  18. public class ParticleCache
  19. {
  20. #region Fields
  21. /// <summary>
  22. /// Container of the particles in the cache.
  23. /// </summary>
  24. public Particle[] Particles;
  25. /// <summary>
  26. /// The particles available to be spawned.
  27. /// </summary>
  28. private Queue<Particle> freeParticles;
  29. #endregion
  30. #region Statistics Properties
  31. /// <summary>
  32. /// The total of all particles in the cache.
  33. /// </summary>
  34. public int TotalCount
  35. {
  36. get { return Particles.Length; }
  37. }
  38. /// <summary>
  39. /// The number of particles remaining in the cache.
  40. /// </summary>
  41. public int FreeCount
  42. {
  43. get { return freeParticles.Count; }
  44. }
  45. /// <summary>
  46. /// The number of particles in use.
  47. /// </summary>
  48. public int UsedCount
  49. {
  50. get { return TotalCount - FreeCount; }
  51. }
  52. #endregion
  53. #region Initialization Methods
  54. /// <summary>
  55. /// Construct a new particle cache.
  56. /// </summary>
  57. /// <param name="count">The number of particles to be allocated.</param>
  58. public ParticleCache(int count)
  59. {
  60. // safety-check the parameter
  61. if (count <= 0)
  62. {
  63. throw new ArgumentOutOfRangeException("count");
  64. }
  65. // create the particles
  66. Particles = new Particle[count];
  67. for (int i = 0; i < count; i++)
  68. {
  69. Particles[i] = new Particle();
  70. }
  71. // create the freed list, which initially contains all particles
  72. freeParticles = new Queue<Particle>(Particles);
  73. }
  74. /// <summary>
  75. /// Reset the particle cache to a freed state.
  76. /// </summary>
  77. public void Reset()
  78. {
  79. // reset the time on particles
  80. for (int i = 0; i < Particles.Length; ++i)
  81. {
  82. Particles[i].TimeRemaining = 0.0f;
  83. }
  84. // recreate the freed list, containing all particles
  85. freeParticles = new Queue<Particle>(Particles);
  86. }
  87. #endregion
  88. #region Membership Methods
  89. /// <summary>
  90. /// Gets the new particle to be used out of the cache.
  91. /// </summary>
  92. /// <returns>The new particle.</returns>
  93. public Particle GetNextParticle()
  94. {
  95. return (freeParticles.Count > 0) ? freeParticles.Dequeue() : null;
  96. }
  97. /// <summary>
  98. /// Releases a particle back to the cache.
  99. /// </summary>
  100. /// <param name="particle">The particle to be released.</param>
  101. public void ReleaseParticle(Particle particle)
  102. {
  103. if (particle != null)
  104. {
  105. freeParticles.Enqueue(particle);
  106. }
  107. }
  108. #endregion
  109. }
  110. }