ParticleCache.cs 3.1 KB

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