ExplosionParticleSystem.cs 3.0 KB

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