Projectile.cs 3.8 KB

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