Particle.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace Robot_Rampage
  8. {
  9. class Particle : Sprite
  10. {
  11. #region Declarations
  12. private Vector2 acceleration;
  13. private float maxSpeed;
  14. private int initialDuration;
  15. private int remainingDuration;
  16. private Color initialColor;
  17. private Color finalColor;
  18. #endregion
  19. #region Properties
  20. public int ElapsedDuration
  21. {
  22. get
  23. {
  24. return initialDuration - remainingDuration;
  25. }
  26. }
  27. public float DurationProgress
  28. {
  29. get
  30. {
  31. return (float)ElapsedDuration /
  32. (float)initialDuration;
  33. }
  34. }
  35. public bool IsActive
  36. {
  37. get
  38. {
  39. return (remainingDuration > 0);
  40. }
  41. }
  42. #endregion
  43. #region Constructor
  44. public Particle(
  45. Vector2 location,
  46. Texture2D texture,
  47. Rectangle initialFrame,
  48. Vector2 velocity,
  49. Vector2 acceleration,
  50. float maxSpeed,
  51. int duration,
  52. Color initialColor,
  53. Color finalColor)
  54. : base(location, texture, initialFrame, velocity)
  55. {
  56. initialDuration = duration;
  57. remainingDuration = duration;
  58. this.acceleration = acceleration;
  59. this.initialColor = initialColor;
  60. this.maxSpeed = maxSpeed;
  61. this.finalColor = finalColor;
  62. }
  63. #endregion
  64. #region Update and Draw
  65. public override void Update(GameTime gameTime)
  66. {
  67. if (remainingDuration <= 0)
  68. {
  69. Expired = true;
  70. }
  71. if (!Expired)
  72. {
  73. Velocity += acceleration;
  74. if (Velocity.Length() > maxSpeed)
  75. {
  76. Vector2 vel = Velocity;
  77. vel.Normalize();
  78. Velocity = vel * maxSpeed;
  79. }
  80. TintColor = Color.Lerp(
  81. initialColor,
  82. finalColor,
  83. DurationProgress);
  84. remainingDuration--;
  85. }
  86. base.Update(gameTime);
  87. }
  88. public override void Draw(SpriteBatch spriteBatch)
  89. {
  90. if (IsActive)
  91. {
  92. base.Draw(spriteBatch);
  93. }
  94. }
  95. #endregion
  96. }
  97. }