RocketProjectile.cs 5.5 KB

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