ExplosionParticleSystem.cs 3.1 KB

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