PowerUp.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //-----------------------------------------------------------------------------
  2. // PowerUp.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Graphics;
  10. namespace NetRumble
  11. {
  12. /// <summary>
  13. /// Base public class for all power-ups that exist in the game.
  14. /// </summary>
  15. abstract public class PowerUp : GameplayObject
  16. {
  17. /// <summary>
  18. /// The speed of the rotation of the power-up, in radians/sec.
  19. /// </summary>
  20. const float rotationSpeed = 2f;
  21. /// <summary>
  22. /// The amplitude of the pulse
  23. /// </summary>
  24. const float pulseAmplitude = 0.1f;
  25. /// <summary>
  26. /// The rate of the pulse.
  27. /// </summary>
  28. const float pulseRate = 0.1f;
  29. public const float PowerUpRadius = 20f;
  30. /// <summary>
  31. /// The time accumulator for the power-up pulse.
  32. /// </summary>
  33. private float pulseTime = 0f;
  34. /// <summary>
  35. /// Constructs a new power-up.
  36. /// </summary>
  37. protected PowerUp()
  38. : base()
  39. {
  40. // set the collision data
  41. this.radius = PowerUpRadius;
  42. this.mass = Int32.MaxValue;
  43. }
  44. /// <summary>
  45. /// Initialize the power-up to it's default gameplay states.
  46. /// </summary>
  47. public override void Initialize()
  48. {
  49. if (!active)
  50. {
  51. // play the spawn sound effect
  52. AudioManager.PlaySoundEffect("powerup_spawn");
  53. }
  54. base.Initialize();
  55. }
  56. /// <summary>
  57. /// Draw the triple-laser power-up.
  58. /// </summary>
  59. /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param>
  60. /// <param name="spriteBatch">The SpriteBatch object used to draw.</param>
  61. public abstract void Draw(float elapsedTime, SpriteBatch spriteBatch);
  62. /// <summary>
  63. /// Draw the power-up.
  64. /// </summary>
  65. /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param>
  66. /// <param name="spriteBatch">The SpriteBatch object used to draw.</param>
  67. /// <param name="sprite">The texture used to draw this object.</param>
  68. /// <param name="sourceRectangle">The source rectangle in the texture.</param>
  69. /// <param name="color">The color of the sprite, ignored here.</param>
  70. public override void Draw(float elapsedTime, SpriteBatch spriteBatch,
  71. Texture2D sprite, Rectangle? sourceRectangle, Color color)
  72. {
  73. // update the rotation
  74. rotation += rotationSpeed * elapsedTime;
  75. // adjust the radius to affect the scale
  76. float oldRadius = radius;
  77. pulseTime += elapsedTime;
  78. radius *= 1f + pulseAmplitude * (float)Math.Sin(pulseTime / pulseRate);
  79. base.Draw(elapsedTime, spriteBatch, sprite, sourceRectangle, color);
  80. radius = oldRadius;
  81. }
  82. /// <summary>
  83. /// Defines the interaction between this power-up and a target GameplayObject
  84. /// when they touch.
  85. /// </summary>
  86. /// <param name="target">The GameplayObject that is touching this one.</param>
  87. /// <returns>True if the objects meaningfully interacted.</returns>
  88. public override bool Touch(GameplayObject target)
  89. {
  90. // if it touched a ship, then create a particle system and play a sound
  91. Ship ship = target as Ship;
  92. if (ship != null)
  93. {
  94. // play the "power-up picked up" sound effect
  95. AudioManager.PlaySoundEffect("powerup_touch");
  96. // kill the power-up
  97. Die(target, false);
  98. // the ship keeps going as if it didn't hit anything
  99. return false;
  100. }
  101. return base.Touch(target);
  102. }
  103. }
  104. }