private void MakeButterflyFaceProperDirectionAndAdjustAnimationSpeed(DefaultAnimatedSpriteParticle cParticle) { // If the butterfly should face left if (cParticle.Velocity.X < 0) { cParticle.FlipMode = SpriteEffects.FlipVertically; } // Else it should face right (the image default) else { cParticle.FlipMode = SpriteEffects.None; } // Make the Butterfly animation go faster based on how fast it is moving upwards float fNormalizedYVelocity = (cParticle.Velocity.Y + miButterflyMaxSpeed) / (miButterflyMaxSpeed * 2); float fNewAnimationTime = MathHelper.Lerp(mfMinTimeBetweenAnimationImages, mfMaxTimeBetweenAnimationImages, fNormalizedYVelocity); cParticle.Animation.CurrentAnimationsPictureRotationTime = fNewAnimationTime; }
protected void ChangeDirectionRandomly(DefaultAnimatedSpriteParticle cParticle, float fElapsedTimeInSeconds) { // If this is the Mouse butterfly if (ActiveParticles.Last != null && cParticle == ActiveParticles.Last.Value) { // Exit without doing anything return; } // If we should pick a new Direction (happens randomly) (-1 is specified when initializing new particles) float fClamped = MathHelper.Clamp(fElapsedTimeInSeconds, -1f, 0.01f); if (RandomNumber.NextFloat() < fClamped || fElapsedTimeInSeconds < 0) { // Calculate a new Velocity direction cParticle.Velocity = new Vector3(RandomNumber.Next(-miButterflyMaxSpeed, miButterflyMaxSpeed), RandomNumber.Next(-miButterflyMaxSpeed, miButterflyMaxSpeed), 0); // Calculate the new Direction the Butterfly should face MakeButterflyFaceProperDirectionAndAdjustAnimationSpeed(cParticle); } }
public void InitializeParticleAnimatedButterfly(DefaultAnimatedSpriteParticle cParticle) { InitializeParticleUsingInitialProperties(cParticle); cParticle.Animation.CopyFrom(mcButterflyAnimation); // If this is the first Particle to be added if (NumberOfActiveParticles == 0) { // Set the Particle to live forever cParticle.Lifetime = -1.0f; // Place the Particle in the middle of the screen to begin with cParticle.Position = new Vector3(miScreenWidth / 2, miScreenHeight / 2, 0); // Make sure it does not move on it's own cParticle.Velocity = Vector3.Zero; cParticle.Acceleration = Vector3.Zero; cParticle.ExternalForce = Vector3.Zero; // Set the Animation to play forever (since the particles die when the animation ends) cParticle.Animation.CurrentAnimationsNumberOfTimesToPlay = 0; } else { // Set this new Particle to start from the same position in the Animation as the Mouse Butterfly current is cParticle.Animation.CurrentAnimationsPictureRotationOrderIndex = ActiveParticles.Last.Value.Animation.CurrentAnimationsPictureRotationOrderIndex; // Have this butterfly go in a random direction ChangeDirectionRandomly(cParticle, -1); } }
//=========================================================== // Particle Update Functions //=========================================================== protected void BounceOffScreenEdges(DefaultAnimatedSpriteParticle cParticle, float fElapsedTimeInSeconds) { int iLeft = (int)(cParticle.Position.X - (cParticle.Width / 4)); int iRight = (int)(cParticle.Position.X + (cParticle.Width / 4)); int iTop = (int)(cParticle.Position.Y - (cParticle.Height / 4)); int iBottom = (int)(cParticle.Position.Y + (cParticle.Height / 4)); // If the Particle is heading off the left or right side of the screen if ((iLeft < 0 && cParticle.Velocity.X < 0) || (iRight > miScreenWidth && cParticle.Velocity.X > 0)) { // Reverse it's horizontal direction cParticle.Velocity.X *= -1; } // If the Particle is heading off the top or bottom of the screen if ((iTop < 0 && cParticle.Velocity.Y < 0) || (iBottom > miScreenHeight && cParticle.Velocity.Y > 0)) { // Reverse it's vertical direction cParticle.Velocity.Y *= -1; } // Make sure the Butterfly is still orientated properly MakeButterflyFaceProperDirectionAndAdjustAnimationSpeed(cParticle); }
public void InitializeParticleAnimatedExplosion(DefaultAnimatedSpriteParticle cParticle) { InitializeParticleUsingInitialProperties(cParticle); cParticle.Animation.CopyFrom(mcExplosionAnimation); }