Particle.cs 4.2 KB

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