public void Emit() { TransformComponent myXForm = Owner.GetComponent <TransformComponent>(ActorComponent.ComponentType.Transform); for (int i = 0; i < 20; ++i) { mParticleSystem.AddParticle(myXForm.Translation, Vector3.Zero); } GameResources.ActorManager.PreAnimationUpdateStep += PreAnimationUpdateHandler; }
private void PreAnimationUpdateHandler(object sender, UpdateStepEventArgs e) { if (mTargetActorId == Actor.INVALID_ACTOR_ID) { if (!mParticleSystem.HasActiveParticles) { Owner.Despawn(); } return; } // Work out how much time has passed since the previous update. float elapsedTime = (float)(e.GameTime.ElapsedGameTime.TotalSeconds); Vector3 newPosition = GetPosition(); if (elapsedTime > 0) { // Work out how fast we are moving. Vector3 velocity = (newPosition - mPreviousPosition) / elapsedTime; // If we had any time left over that we didn't use during the // previous update, add that to the current elapsed time. float timeToSpend = mTimeLeftOver + elapsedTime; // Counter for looping over the time interval. float currentTime = -mTimeLeftOver; // Create particles as long as we have a big enough time interval. while (timeToSpend > mTimeBetweenParticles) { currentTime += mTimeBetweenParticles; timeToSpend -= mTimeBetweenParticles; // Work out the optimal position for this particle. This will produce // evenly spaced particles regardless of the object speed, particle // creation frequency, or game update rate. float mu = currentTime / elapsedTime; Vector3 position = Vector3.Lerp(mPreviousPosition, newPosition, mu); // Create the particle. mParticleSystem.AddParticle(position, velocity); } // Store any time we didn't use, so it can be part of the next update. mTimeLeftOver = timeToSpend; } mPreviousPosition = newPosition; }
private void PreAnimationUpdateHandler(object sender, UpdateStepEventArgs e) { mParticleSystem.AddParticle(mTransform.Translation, Vector3.Zero); }