Particle.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 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. /// particles are the little bits that will make up an effect. each effect will
  20. /// be comprised of many of these particles. They have basic physical properties,
  21. /// such as position, velocity, acceleration, and rotation. They'll be drawn as
  22. /// sprites, all layered on top of one another, and will be very pretty.
  23. /// </summary>
  24. public class Particle
  25. {
  26. // Position, Velocity, and Acceleration represent exactly what their names
  27. // indicate. They are public fields rather than properties so that users
  28. // can directly access their .X and .Y properties.
  29. public Vector2 Position;
  30. public Vector2 Velocity;
  31. public Vector2 Acceleration;
  32. // how long this particle will "live"
  33. private float lifetime;
  34. public float Lifetime
  35. {
  36. get { return lifetime; }
  37. set { lifetime = value; }
  38. }
  39. // how long it has been since initialize was called
  40. private float timeSinceStart;
  41. public float TimeSinceStart
  42. {
  43. get { return timeSinceStart; }
  44. set { timeSinceStart = value; }
  45. }
  46. // the scale of this particle
  47. private float scale;
  48. public float Scale
  49. {
  50. get { return scale; }
  51. set { scale = value; }
  52. }
  53. // its rotation, in radians
  54. private float rotation;
  55. public float Rotation
  56. {
  57. get { return rotation; }
  58. set { rotation = value; }
  59. }
  60. // how fast does it rotate?
  61. private float rotationSpeed;
  62. public float RotationSpeed
  63. {
  64. get { return rotationSpeed; }
  65. set { rotationSpeed = value; }
  66. }
  67. // is this particle still alive? once TimeSinceStart becomes greater than
  68. // Lifetime, the particle should no longer be drawn or updated.
  69. public bool Active
  70. {
  71. get { return TimeSinceStart < Lifetime; }
  72. }
  73. // initialize is called by ParticleSystem to set up the particle, and prepares
  74. // the particle for use.
  75. public void Initialize(Vector2 position, Vector2 velocity, Vector2 acceleration,
  76. float lifetime, float scale, float rotationSpeed)
  77. {
  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 = ParticleSampleGame.RandomBetween(0, MathHelper.TwoPi);
  90. }
  91. // update is called by the ParticleSystem on every frame. This is where the
  92. // particle's position and that kind of thing get updated.
  93. public void Update(float dt)
  94. {
  95. Velocity += Acceleration * dt;
  96. Position += Velocity * dt;
  97. Rotation += RotationSpeed * dt;
  98. TimeSinceStart += dt;
  99. }
  100. }
  101. }