#region File Description
//-----------------------------------------------------------------------------
// ParticleCache.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
#endregion
namespace NetRumble
{
///
/// Cache of Particle objects.
///
public class ParticleCache
{
#region Fields
///
/// Container of the particles in the cache.
///
public Particle[] Particles;
///
/// The particles available to be spawned.
///
private Queue freeParticles;
#endregion
#region Statistics Properties
///
/// The total of all particles in the cache.
///
public int TotalCount
{
get { return Particles.Length; }
}
///
/// The number of particles remaining in the cache.
///
public int FreeCount
{
get { return freeParticles.Count; }
}
///
/// The number of particles in use.
///
public int UsedCount
{
get { return TotalCount - FreeCount; }
}
#endregion
#region Initialization Methods
///
/// Construct a new particle cache.
///
/// The number of particles to be allocated.
public ParticleCache(int count)
{
// safety-check the parameter
if (count <= 0)
{
throw new ArgumentOutOfRangeException("count");
}
// create the particles
Particles = new Particle[count];
for (int i = 0; i < count; i++)
{
Particles[i] = new Particle();
}
// create the freed list, which initially contains all particles
freeParticles = new Queue(Particles);
}
///
/// Reset the particle cache to a freed state.
///
public void Reset()
{
// reset the time on particles
for (int i = 0; i < Particles.Length; ++i)
{
Particles[i].TimeRemaining = 0.0f;
}
// recreate the freed list, containing all particles
freeParticles = new Queue(Particles);
}
#endregion
#region Membership Methods
///
/// Gets the new particle to be used out of the cache.
///
/// The new particle.
public Particle GetNextParticle()
{
return (freeParticles.Count > 0) ? freeParticles.Dequeue() : null;
}
///
/// Releases a particle back to the cache.
///
/// The particle to be released.
public void ReleaseParticle(Particle particle)
{
if (particle != null)
{
freeParticles.Enqueue(particle);
}
}
#endregion
}
}