Particle.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //-----------------------------------------------------------------------------
  2. // Particle.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. namespace RockRain.Core
  10. {
  11. /// <summary>
  12. /// particles are the little bits that will make up an effect. each effect will
  13. /// be comprised of many of these particles. They have basic physical properties,
  14. /// such as position, velocity, acceleration, and rotation. They'll be drawn as
  15. /// sprites, all layered on top of one another, and will be very pretty.
  16. /// </summary>
  17. public class Particle
  18. {
  19. // Position, Velocity, and Acceleration represent exactly what their names
  20. // indicate. They are public fields rather than properties so that users
  21. // can directly access their .X and .Y properties.
  22. public Vector2 Position;
  23. public Vector2 Velocity;
  24. public Vector2 Acceleration;
  25. private Random random;
  26. // how long this particle will "live"
  27. private float lifetime;
  28. public float Lifetime
  29. {
  30. get { return lifetime; }
  31. set { lifetime = value; }
  32. }
  33. // how long it has been since initialize was called
  34. private float timeSinceStart;
  35. public float TimeSinceStart
  36. {
  37. get { return timeSinceStart; }
  38. set { timeSinceStart = value; }
  39. }
  40. // the scale of this particle
  41. private float scale;
  42. public float Scale
  43. {
  44. get { return scale; }
  45. set { scale = value; }
  46. }
  47. // its rotation, in radians
  48. private float rotation;
  49. public float Rotation
  50. {
  51. get { return rotation; }
  52. set { rotation = value; }
  53. }
  54. // how fast does it rotate?
  55. private float rotationSpeed;
  56. public float RotationSpeed
  57. {
  58. get { return rotationSpeed; }
  59. set { rotationSpeed = value; }
  60. }
  61. // is this particle still alive? once TimeSinceStart becomes greater than
  62. // Lifetime, the particle should no longer be drawn or updated.
  63. public bool Active
  64. {
  65. get { return TimeSinceStart < Lifetime; }
  66. }
  67. // initialize is called by ParticleSystem to set up the particle, and prepares
  68. // the particle for use.
  69. public void Initialize(Vector2 position, Vector2 velocity, Vector2 acceleration,
  70. float lifetime, float scale, float rotationSpeed)
  71. {
  72. // Initialize randomizer
  73. random = new Random(GetHashCode());
  74. // set the values to the requested values
  75. this.Position = position;
  76. this.Velocity = velocity;
  77. this.Acceleration = acceleration;
  78. this.Lifetime = lifetime;
  79. this.Scale = scale;
  80. this.RotationSpeed = rotationSpeed;
  81. // reset TimeSinceStart - we have to do this because particles will be
  82. // reused.
  83. this.TimeSinceStart = 0.0f;
  84. // set rotation to some random value between 0 and 360 degrees.
  85. this.Rotation = RandomBetween(0, MathHelper.TwoPi);
  86. }
  87. // a handy little function that gives a random float between two
  88. // values. This will be used in several places in the sample, in particilar in
  89. // ParticleSystem.InitializeParticle.
  90. public float RandomBetween(float min, float max)
  91. {
  92. return min + (float)random.NextDouble() * (max - min);
  93. }
  94. // update is called by the ParticleSystem on every frame. This is where the
  95. // particle's position and that kind of thing get updated.
  96. public void Update(float dt)
  97. {
  98. Velocity += Acceleration * dt;
  99. Position += Velocity * dt;
  100. Rotation += RotationSpeed * dt;
  101. TimeSinceStart += dt;
  102. }
  103. }
  104. }