2
0

ExplosionParticleSystem.cs 3.3 KB

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