Weapon.cs 3.0 KB

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