123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #region File Description
- //-----------------------------------------------------------------------------
- // Projectile.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using Microsoft.Xna.Framework;
- #endregion
- namespace Particle3DSample
- {
- /// <summary>
- /// This class demonstrates how to combine several different particle systems
- /// to build up a more sophisticated composite effect. It implements a rocket
- /// projectile, which arcs up into the sky using a ParticleEmitter to leave a
- /// steady stream of trail particles behind it. After a while it explodes,
- /// creating a sudden burst of explosion and smoke particles.
- /// </summary>
- class Projectile
- {
- #region Constants
- const float trailParticlesPerSecond = 200;
- const int numExplosionParticles = 30;
- const int numExplosionSmokeParticles = 50;
- const float projectileLifespan = 1.5f;
- const float sidewaysVelocityRange = 60;
- const float verticalVelocityRange = 40;
- const float gravity = 15;
- #endregion
- #region Fields
- ParticleSystem explosionParticles;
- ParticleSystem explosionSmokeParticles;
- ParticleEmitter trailEmitter;
- Vector3 position;
- Vector3 velocity;
- float age;
- static Random random = new Random();
- #endregion
- /// <summary>
- /// Constructs a new projectile.
- /// </summary>
- public Projectile(ParticleSystem explosionParticles,
- ParticleSystem explosionSmokeParticles,
- ParticleSystem projectileTrailParticles)
- {
- this.explosionParticles = explosionParticles;
- this.explosionSmokeParticles = explosionSmokeParticles;
- // Start at the origin, firing in a random (but roughly upward) direction.
- position = Vector3.Zero;
- velocity.X = (float)(random.NextDouble() - 0.5) * sidewaysVelocityRange;
- velocity.Y = (float)(random.NextDouble() + 0.5) * verticalVelocityRange;
- velocity.Z = (float)(random.NextDouble() - 0.5) * sidewaysVelocityRange;
- // Use the particle emitter helper to output our trail particles.
- trailEmitter = new ParticleEmitter(projectileTrailParticles,
- trailParticlesPerSecond, position);
- }
- /// <summary>
- /// Updates the projectile.
- /// </summary>
- public bool Update(GameTime gameTime)
- {
- 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.Update(gameTime, position);
- // 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);
- return false;
- }
-
- return true;
- }
- }
- }
|