SmokePlumeParticleSystem.cs 3.8 KB

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