Particle.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 Spacewar
  17. {
  18. /// <summary>
  19. /// Represents a single particle
  20. /// </summary>
  21. public class Particle : SceneItem
  22. {
  23. /// <summary>
  24. /// The color of this particle
  25. /// </summary>
  26. private Vector4 color;
  27. private Vector4 startColor;
  28. private Vector4 endColor;
  29. private TimeSpan endTime = TimeSpan.Zero;
  30. private TimeSpan lifetime;
  31. #region Properties
  32. public Vector4 Color
  33. {
  34. get
  35. {
  36. return color;
  37. }
  38. }
  39. #endregion
  40. /// <summary>
  41. /// Creates a new particle, with a start and end color and a velocity
  42. /// </summary>
  43. /// <param name="game">Instance of the game we are drawing fonts for</param>
  44. /// <param name="position">Start position</param>
  45. /// <param name="velocity">Velocity</param>
  46. /// <param name="startColor">Start Color including alpha</param>
  47. /// <param name="endColor">End Color including alpha</param>
  48. /// <param name="lifetime">How long in seconds before it fades and disappears. This time will transition through the start/end color cycle</param>
  49. public Particle(Game game, Vector2 position, Vector2 velocity, Vector4 startColor, Vector4 endColor, TimeSpan lifetime)
  50. : base(game, new Vector3(position, 0.0f))
  51. {
  52. Velocity = new Vector3(velocity, 0.0f);
  53. this.startColor = startColor;
  54. this.endColor = endColor;
  55. this.lifetime = lifetime;
  56. }
  57. /// <summary>
  58. /// Update all this particle
  59. /// </summary>
  60. /// <param name="time">Current game time</param>
  61. /// <param name="elapsedTime">Elapsed time since last update</param>
  62. public override void Update(TimeSpan time, TimeSpan elapsedTime)
  63. {
  64. //Start the animation 1st time round
  65. if (endTime == TimeSpan.Zero)
  66. {
  67. endTime = time + lifetime;
  68. }
  69. //End the animation when its time is due as long as lifet
  70. if (time > endTime)
  71. {
  72. Delete = true;
  73. }
  74. //Fade between the colors
  75. float percentLife = (float)((endTime.TotalSeconds - time.TotalSeconds) / lifetime.TotalSeconds);
  76. color = Vector4.Lerp(endColor, startColor, percentLife);
  77. //Do any velocity moving
  78. base.Update(time, elapsedTime);
  79. }
  80. }
  81. }