PowerUp.cs 4.6 KB

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