2
0

Particle.cs 4.1 KB

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