2
0

ExplosionParticleSystem.cs 3.0 KB

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