/// <summary> /// Updates the emitter, creating the appropriate number of particles /// in the appropriate positions. /// </summary> public override void Update(ref GameTime gameTime)//, Vector3 newPosition) { //are we dead?? if (dead) { //Check particle system to see it its dead if (particleSystem.dead) {//yes its dead so disable it and then disable myself (XParticleEmitter) particleSystem.Disable(); this.Disable(); } return; //particlesystem is not dead yet so return here to avoid any other processing/new particles and check again next update } // Work out how much time has passed since the previous update. float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds; if (elapsedTime > 0) { // Work out how fast we are moving. Vector3 velocity = (newPosition - previousPosition) / 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 = timeLeftOver + elapsedTime; // Counter for looping over the time interval. float currentTime = -timeLeftOver; // Create particles as long as we have a big enough time interval. while (timeToSpend > timeBetweenParticles) { currentTime += timeBetweenParticles; timeToSpend -= timeBetweenParticles; // 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(previousPosition, newPosition, mu); // Create the particle. particleSystem.AddParticle(position, velocity); } // Store any time we didn't use, so it can be part of the next update. timeLeftOver = timeToSpend; } previousPosition = newPosition; }
/// <summary> /// Updates the projectile. /// </summary> public override void Update(ref GameTime gameTime) { //if dead then disable all XComponents related to this projectile //but we can't do it right away because the explosion is not finished yet! if (dead) { //set flag trailemitter that its dead (it will disable itself and its particle system automatically) trailEmitter.dead = true; //check if our explosion and explosion smoke particle systems are dead if so disable them and then disable ourself! if (explosionParticles.dead && explosionSmokeParticles.dead) { explosionParticles.Disable(); explosionSmokeParticles.Disable(); this.Disable(); } return; } float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds; // Simple projectile physics. position += velocity * elapsedTime; velocity.Y -= elapsedTime * gravity; age += elapsedTime; // Update the particle emitter, which will create our particle trail. trailEmitter.newPosition = position; //trailEmitter.Update(gameTime, position); //changed XRenderer will call update on this class automatically // If enough time has passed, explode! Note how we pass our velocity // in to the AddParticle method: this lets the explosion be influenced // by the speed and direction of the projectile which created it. if (age > projectileLifespan) { for (int i = 0; i < numExplosionParticles; i++) { explosionParticles.AddParticle(position, velocity); } for (int i = 0; i < numExplosionSmokeParticles; i++) { explosionSmokeParticles.AddParticle(position, velocity); } //flag as dead dead = true; } }