Weapon.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Weapon.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. #endregion
  13. namespace NetRumble
  14. {
  15. /// <summary>
  16. /// Base public class for all weapons that exist in the game.
  17. /// </summary>
  18. abstract public class Weapon
  19. {
  20. #region Gameplay Data
  21. /// <summary>
  22. /// The ship that owns this weapon.
  23. /// </summary>
  24. protected Ship owner = null;
  25. /// <summary>
  26. /// The amount of time remaining before this weapon can fire again.
  27. /// </summary>
  28. protected float timeToNextFire = 0f;
  29. /// <summary>
  30. /// The minimum amount of time between each firing of this weapon.
  31. /// </summary>
  32. protected float fireDelay = 0f;
  33. #endregion
  34. #region Audio Data
  35. /// <summary>
  36. /// The name of the sound effect played when this weapon fires.
  37. /// </summary>
  38. protected string fireSoundEffect = String.Empty;
  39. #endregion
  40. #region Initialization Methods
  41. /// <summary>
  42. /// Constructs a new weapon.
  43. /// </summary>
  44. /// <param name="owner">The ship that owns this weapon.</param>
  45. protected Weapon(Ship owner)
  46. {
  47. if (owner == null)
  48. {
  49. throw new ArgumentNullException("owner");
  50. }
  51. this.owner = owner;
  52. }
  53. #endregion
  54. #region Updating Methods
  55. /// <summary>
  56. /// Update the weapon.
  57. /// </summary>
  58. /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param>
  59. public virtual void Update(float elapsedTime)
  60. {
  61. // count down to when the weapon can fire again
  62. if (timeToNextFire > 0f)
  63. {
  64. timeToNextFire = MathHelper.Max(timeToNextFire - elapsedTime, 0f);
  65. }
  66. }
  67. #endregion
  68. #region Interaction Methods
  69. /// <summary>
  70. /// Fire the weapon in the direction given.
  71. /// </summary>
  72. /// <param name="direction">The direction that the weapon is firing in.</param>
  73. public virtual void Fire(Vector2 direction)
  74. {
  75. // if we can't fire yet, then we're done
  76. if (timeToNextFire > 0f)
  77. {
  78. return;
  79. }
  80. // the owner is no longer safe from damage
  81. owner.Safe = false;
  82. // set the timer
  83. timeToNextFire = fireDelay;
  84. // create and spawn the projectile
  85. CreateProjectiles(direction);
  86. // play the sound effect for firing
  87. if (String.IsNullOrEmpty(fireSoundEffect) == false)
  88. {
  89. AudioManager.PlaySoundEffect(fireSoundEffect);
  90. }
  91. }
  92. /// <summary>
  93. /// Create and spawn the projectile(s) from a firing from this weapon.
  94. /// </summary>
  95. /// <param name="direction">The direction that the projectile will move.</param>
  96. protected abstract void CreateProjectiles(Vector2 direction);
  97. #endregion
  98. }
  99. }