Projectile.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //-----------------------------------------------------------------------------
  2. // Projectile.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. namespace Particle3DSample
  10. {
  11. /// <summary>
  12. /// This class demonstrates how to combine several different particle systems
  13. /// to build up a more sophisticated composite effect. It implements a rocket
  14. /// projectile, which arcs up into the sky using a ParticleEmitter to leave a
  15. /// steady stream of trail particles behind it. After a while it explodes,
  16. /// creating a sudden burst of explosion and smoke particles.
  17. /// </summary>
  18. class Projectile
  19. {
  20. const float trailParticlesPerSecond = 200;
  21. const int numExplosionParticles = 30;
  22. const int numExplosionSmokeParticles = 50;
  23. const float projectileLifespan = 1.5f;
  24. const float sidewaysVelocityRange = 60;
  25. const float verticalVelocityRange = 40;
  26. const float gravity = 15;
  27. ParticleSystem explosionParticles;
  28. ParticleSystem explosionSmokeParticles;
  29. ParticleEmitter trailEmitter;
  30. Vector3 position;
  31. Vector3 velocity;
  32. float age;
  33. static Random random = new Random();
  34. /// <summary>
  35. /// Constructs a new projectile.
  36. /// </summary>
  37. public Projectile(ParticleSystem explosionParticles,
  38. ParticleSystem explosionSmokeParticles,
  39. ParticleSystem projectileTrailParticles)
  40. {
  41. this.explosionParticles = explosionParticles;
  42. this.explosionSmokeParticles = explosionSmokeParticles;
  43. // Start at the origin, firing in a random (but roughly upward) direction.
  44. position = Vector3.Zero;
  45. velocity.X = (float)(random.NextDouble() - 0.5) * sidewaysVelocityRange;
  46. velocity.Y = (float)(random.NextDouble() + 0.5) * verticalVelocityRange;
  47. velocity.Z = (float)(random.NextDouble() - 0.5) * sidewaysVelocityRange;
  48. // Use the particle emitter helper to output our trail particles.
  49. trailEmitter = new ParticleEmitter(projectileTrailParticles,
  50. trailParticlesPerSecond, position);
  51. }
  52. /// <summary>
  53. /// Updates the projectile.
  54. /// </summary>
  55. public bool Update(GameTime gameTime)
  56. {
  57. float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
  58. // Simple projectile physics.
  59. position += velocity * elapsedTime;
  60. velocity.Y -= elapsedTime * gravity;
  61. age += elapsedTime;
  62. // Update the particle emitter, which will create our particle trail.
  63. trailEmitter.Update(gameTime, position);
  64. // If enough time has passed, explode! Note how we pass our velocity
  65. // in to the AddParticle method: this lets the explosion be influenced
  66. // by the speed and direction of the projectile which created it.
  67. if (age > projectileLifespan)
  68. {
  69. for (int i = 0; i < numExplosionParticles; i++)
  70. explosionParticles.AddParticle(position, velocity);
  71. for (int i = 0; i < numExplosionSmokeParticles; i++)
  72. explosionSmokeParticles.AddParticle(position, velocity);
  73. return false;
  74. }
  75. return true;
  76. }
  77. }
  78. }