Particle.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Asteroid_Belt_Assault
  8. {
  9. class Particle : Sprite
  10. {
  11. private Vector2 acceleration;
  12. private float maxSpeed;
  13. private int initialDuration;
  14. private int remainingDuration;
  15. private Color initialColor;
  16. private Color finalColor;
  17. public int ElapsedDuration
  18. {
  19. get
  20. {
  21. return initialDuration - remainingDuration;
  22. }
  23. }
  24. public float DurationProgress
  25. {
  26. get
  27. {
  28. return (float)ElapsedDuration /
  29. (float)initialDuration;
  30. }
  31. }
  32. public bool IsActive
  33. {
  34. get
  35. {
  36. return (remainingDuration > 0);
  37. }
  38. }
  39. public Particle(
  40. Vector2 location,
  41. Texture2D texture,
  42. Rectangle initialFrame,
  43. Vector2 velocity,
  44. Vector2 acceleration,
  45. float maxSpeed,
  46. int duration,
  47. Color initialColor,
  48. Color finalColor)
  49. : base(location, texture, initialFrame, velocity)
  50. {
  51. initialDuration = duration;
  52. remainingDuration = duration;
  53. this.acceleration = acceleration;
  54. this.initialColor = initialColor;
  55. this.maxSpeed = maxSpeed;
  56. this.finalColor = finalColor;
  57. }
  58. public override void Update(GameTime gameTime)
  59. {
  60. if (IsActive)
  61. {
  62. velocity += acceleration;
  63. if (velocity.Length() > maxSpeed)
  64. {
  65. velocity.Normalize();
  66. velocity *= maxSpeed;
  67. }
  68. TintColor = Color.Lerp(
  69. initialColor,
  70. finalColor,
  71. DurationProgress);
  72. remainingDuration--;
  73. base.Update(gameTime);
  74. }
  75. }
  76. public override void Draw(SpriteBatch spriteBatch)
  77. {
  78. if (IsActive)
  79. {
  80. base.Draw(spriteBatch);
  81. }
  82. }
  83. }
  84. }