SmokePlumeParticleSystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SmokePlumeParticleSystem.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. /// SmokePlumeParticleSystem is a specialization of ParticleSystem which sends up a
  26. /// plume of smoke. The smoke is blown to the right by the wind.
  27. /// </summary>
  28. public class SmokePlumeParticleSystem : ParticleSystem
  29. {
  30. public SmokePlumeParticleSystem(ParticleSampleGame game, int howManyEffects)
  31. : base(game,howManyEffects)
  32. {
  33. }
  34. /// <summary>
  35. /// Set up the constants that will give this particle system its behavior and
  36. /// properties.
  37. /// </summary>
  38. protected override void InitializeConstants()
  39. {
  40. textureFilename = "smoke";
  41. minInitialSpeed = 20;
  42. maxInitialSpeed = 100;
  43. // we don't want the particles to accelerate at all, aside from what we
  44. // do in our overriden InitializeParticle.
  45. minAcceleration = 0;
  46. maxAcceleration = 0;
  47. // long lifetime, this can be changed to create thinner or thicker smoke.
  48. // tweak minNumParticles and maxNumParticles to complement the effect.
  49. minLifetime = 5.0f;
  50. maxLifetime = 7.0f;
  51. minScale = .5f;
  52. maxScale = 1.0f;
  53. minNumParticles = 7;
  54. maxNumParticles = 15;
  55. // rotate slowly, we want a fairly relaxed effect
  56. minRotationSpeed = -MathHelper.PiOver4 / 2.0f;
  57. maxRotationSpeed = MathHelper.PiOver4 / 2.0f;
  58. spriteBlendMode = BlendState.AlphaBlend;
  59. DrawOrder = AlphaBlendDrawOrder;
  60. }
  61. /// <summary>
  62. /// PickRandomDirection is overriden so that we can make the particles always
  63. /// move have an initial velocity pointing up.
  64. /// </summary>
  65. /// <returns>a random direction which points basically up.</returns>
  66. protected override Vector2 PickRandomDirection()
  67. {
  68. // Point the particles somewhere between 80 and 100 degrees.
  69. // tweak this to make the smoke have more or less spread.
  70. float radians = ParticleSampleGame.RandomBetween(
  71. MathHelper.ToRadians(80), MathHelper.ToRadians(100));
  72. Vector2 direction = Vector2.Zero;
  73. // from the unit circle, cosine is the x coordinate and sine is the
  74. // y coordinate. We're negating y because on the screen increasing y moves
  75. // down the monitor.
  76. direction.X = (float)Math.Cos(radians);
  77. direction.Y = -(float)Math.Sin(radians);
  78. return direction;
  79. }
  80. /// <summary>
  81. /// InitializeParticle is overridden to add the appearance of wind.
  82. /// </summary>
  83. /// <param name="p">the particle to set up</param>
  84. /// <param name="where">where the particle should be placed</param>
  85. protected override void InitializeParticle(Particle p, Vector2 where)
  86. {
  87. base.InitializeParticle(p, where);
  88. // the base is mostly good, but we want to simulate a little bit of wind
  89. // heading to the right.
  90. p.Acceleration.X += ParticleSampleGame.RandomBetween(10, 50);
  91. }
  92. }
  93. }