2
0

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