RocketProjectile.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // RocketProjectile.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. using Microsoft.Xna.Framework.Audio;
  14. using Microsoft.Xna.Framework.Content;
  15. #endregion
  16. namespace NetRumble
  17. {
  18. /// <summary>
  19. /// A rocket projectile.
  20. /// </summary>
  21. public class RocketProjectile : Projectile
  22. {
  23. #region Constants
  24. /// <summary>
  25. /// The initial speed of the rocket.
  26. /// </summary>
  27. const float initialSpeed = 650f;
  28. #endregion
  29. #region Static Graphics Data
  30. /// <summary>
  31. /// Texture for all rocket projectiles.
  32. /// </summary>
  33. private static Texture2D texture;
  34. /// <summary>
  35. /// The particle-effect manager which recieves the effects from rockets.
  36. /// </summary>
  37. public static ParticleEffectManager ParticleEffectManager;
  38. #endregion
  39. #region Graphics Data
  40. /// <summary>
  41. /// The trailing effect behind a rocket.
  42. /// </summary>
  43. protected ParticleEffect rocketTrailEffect = null;
  44. #endregion
  45. #region Audio Data
  46. /// <summary>
  47. /// The sound effect of the rocket as it flies.
  48. /// </summary>
  49. protected SoundEffectInstance rocketSound = null;
  50. #endregion
  51. #region Initialization
  52. /// <summary>
  53. /// Constructs a new rocket projectile.
  54. /// </summary>
  55. /// <param name="owner">The ship that fired this projectile, if any.</param>
  56. /// <param name="direction">The initial direction for this projectile.</param>
  57. public RocketProjectile(Ship owner, Vector2 direction)
  58. : base(owner, direction)
  59. {
  60. // set the gameplay data
  61. this.velocity = initialSpeed * direction;
  62. // set the collision data
  63. this.radius = 14f;
  64. this.mass = 10f;
  65. // set the projectile data
  66. this.duration = 4f;
  67. this.damageAmount = 150f;
  68. this.damageOwner = false;
  69. this.damageRadius = 128f;
  70. this.rotation += MathHelper.Pi;
  71. }
  72. /// <summary>
  73. /// Initialize the rocket projectile to it's default gameplay states.
  74. /// </summary>
  75. public override void Initialize()
  76. {
  77. if (!active)
  78. {
  79. // get and play the rocket-flying sound effect
  80. AudioManager.PlaySoundEffect("rocket", true, out rocketSound);
  81. // start the rocket-trail effect
  82. if (ParticleEffectManager != null)
  83. {
  84. rocketTrailEffect = ParticleEffectManager.SpawnEffect(
  85. ParticleEffectType.RocketTrail, this);
  86. }
  87. }
  88. base.Initialize();
  89. }
  90. #endregion
  91. #region Drawing Methods
  92. /// <summary>
  93. /// Draw the rocket.
  94. /// </summary>
  95. /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param>
  96. /// <param name="spriteBatch">The SpriteBatch object used to draw.</param>
  97. public override void Draw(float elapsedTime, SpriteBatch spriteBatch)
  98. {
  99. // ignore the parameter color if we have an owner
  100. base.Draw(elapsedTime, spriteBatch, texture, null, Color.White);
  101. }
  102. #endregion
  103. #region Interaction Methods
  104. /// <summary>
  105. /// Kills this object, in response to the given GameplayObject.
  106. /// </summary>
  107. /// <param name="source">The GameplayObject responsible for the kill.</param>
  108. /// <param name="cleanupOnly">
  109. /// If true, the object dies without any further effects.
  110. /// </param>
  111. public override void Die(GameplayObject source, bool cleanupOnly)
  112. {
  113. if (active)
  114. {
  115. if (!cleanupOnly)
  116. {
  117. // play the explosion sound
  118. AudioManager.PlaySoundEffect("explosion_medium");
  119. // display the rocket explosion
  120. if (ParticleEffectManager != null)
  121. {
  122. ParticleEffectManager.SpawnEffect(
  123. ParticleEffectType.RocketExplosion, Position);
  124. }
  125. }
  126. // stop the rocket-flying sound effect
  127. if (rocketSound != null)
  128. {
  129. rocketSound.Stop(true);
  130. rocketSound.Dispose();
  131. rocketSound = null;
  132. }
  133. // stop the rocket-trail effect
  134. if (rocketTrailEffect != null)
  135. {
  136. rocketTrailEffect.Stop(false);
  137. }
  138. }
  139. base.Die(source, cleanupOnly);
  140. }
  141. #endregion
  142. #region Static Graphics Methods
  143. /// <summary>
  144. /// Load all of the static graphics content for this class.
  145. /// </summary>
  146. /// <param name="contentManager">The content manager to load with.</param>
  147. public static void LoadContent(ContentManager contentManager)
  148. {
  149. // safety-check the parameters
  150. if (contentManager == null)
  151. {
  152. throw new ArgumentNullException("contentManager");
  153. }
  154. // load the texture
  155. texture = contentManager.Load<Texture2D>("Textures/rocket");
  156. }
  157. /// <summary>
  158. /// Unload all of the static graphics content for this class.
  159. /// </summary>
  160. public static void UnloadContent()
  161. {
  162. texture = null;
  163. }
  164. #endregion
  165. }
  166. }