Particle.cs 3.7 KB

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