Example #1
0
 /// <summary>
 /// Releases a particle back to the cache.
 /// </summary>
 /// <param name="particle">The particle to be released.</param>
 public void ReleaseParticle(Particle particle)
 {
     if (particle != null)
     {
         freeParticles.Enqueue(particle);
     }
 }
Example #2
0
        /// <summary>
        /// Construct a new particle cache.
        /// </summary>
        /// <param name="count">The number of particles to be allocated.</param>
        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<Particle>(Particles);
        }
Example #3
0
        /// <summary>
        /// Initialize a new particle using the values in this system.
        /// </summary>
        /// <param name="particle">The particle to be initialized.</param>
        private void InitializeParticle(Particle particle)
        {
            // safety-check the parameter
            if (particle == null)
            {
                throw new ArgumentNullException("particle");
            }

            // set the time remaining on the new particle
            particle.TimeRemaining = RandomMath.RandomBetween(durationMinimum, 
                durationMaximum);

            // generate a random direction
            Vector2 direction = RandomMath.RandomDirection(releaseAngleMinimum, 
                releaseAngleMaximum);

            // set the graphics data on the new particle
            particle.Position = position + direction * 
                RandomMath.RandomBetween(releaseDistanceMinimum, 
                releaseDistanceMaximum);
            particle.Velocity = direction * RandomMath.RandomBetween(velocityMinimum, 
                velocityMaximum);
            if (particle.Velocity.LengthSquared() > 0f)
            {
                particle.Acceleration = direction * 
                    RandomMath.RandomBetween(accelerationMinimum, accelerationMaximum);
            }
            else
            {
                particle.Acceleration = Vector2.Zero;
            }
            particle.Rotation = RandomMath.RandomBetween(0f, MathHelper.TwoPi);
            particle.Scale = RandomMath.RandomBetween(scaleMinimum, scaleMaximum);
            particle.Opacity = RandomMath.RandomBetween(opacityMinimum, opacityMaximum);
        }