ExplosionParticleSystem.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ExplosionParticleSystem.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 System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. #endregion
  16. namespace ParticleSample
  17. {
  18. /// <summary>
  19. /// ExplosionParticleSystem is a specialization of ParticleSystem which creates a
  20. /// fiery explosion. It should be combined with ExplosionSmokeParticleSystem for
  21. /// best effect.
  22. /// </summary>
  23. public class ExplosionParticleSystem : ParticleSystem
  24. {
  25. public ExplosionParticleSystem(ParticleSampleGame game, int howManyEffects)
  26. : base(game, howManyEffects)
  27. {
  28. }
  29. /// <summary>
  30. /// Set up the constants that will give this particle system its behavior and
  31. /// properties.
  32. /// </summary>
  33. protected override void InitializeConstants()
  34. {
  35. textureFilename = "explosion";
  36. // high initial speed with lots of variance. make the values closer
  37. // together to have more consistently circular explosions.
  38. minInitialSpeed = 40;
  39. maxInitialSpeed = 500;
  40. // doesn't matter what these values are set to, acceleration is tweaked in
  41. // the override of InitializeParticle.
  42. minAcceleration = 0;
  43. maxAcceleration = 0;
  44. // explosions should be relatively short lived
  45. minLifetime = .5f;
  46. maxLifetime = 1.0f;
  47. minScale = .3f;
  48. maxScale = 1.0f;
  49. // we need to reduce the number of particles on Windows Phone in order to keep
  50. // a good framerate
  51. #if WINDOWS_PHONE
  52. minNumParticles = 10;
  53. maxNumParticles = 12;
  54. #else
  55. minNumParticles = 20;
  56. maxNumParticles = 25;
  57. #endif
  58. minRotationSpeed = -MathHelper.PiOver4;
  59. maxRotationSpeed = MathHelper.PiOver4;
  60. // additive blending is very good at creating fiery effects.
  61. blendState = BlendState.Additive;
  62. DrawOrder = AdditiveDrawOrder;
  63. }
  64. protected override void InitializeParticle(Particle p, Vector2 where)
  65. {
  66. base.InitializeParticle(p, where);
  67. // The base works fine except for acceleration. Explosions move outwards,
  68. // then slow down and stop because of air resistance. Let's change
  69. // acceleration so that when the particle is at max lifetime, the velocity
  70. // will be zero.
  71. // We'll use the equation vt = v0 + (a0 * t). (If you're not familar with
  72. // this, it's one of the basic kinematics equations for constant
  73. // acceleration, and basically says:
  74. // velocity at time t = initial velocity + acceleration * t)
  75. // We'll solve the equation for a0, using t = p.Lifetime and vt = 0.
  76. p.Acceleration = -p.Velocity / p.Lifetime;
  77. }
  78. }
  79. }