Projectiles.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Projectiles.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 System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using System.Diagnostics;
  16. #endregion
  17. namespace Spacewar
  18. {
  19. /// <summary>
  20. /// Projectiles handles collections of projectiles shot from the retro or evolved ship.
  21. /// Its useful to have a a parent group because in Retro mode we want to batch render them
  22. /// </summary>
  23. public class Projectiles : SceneItem
  24. {
  25. public Projectiles(Game game)
  26. : base(game)
  27. {
  28. Projectile.ProjectileCount[0] = 0;
  29. Projectile.ProjectileCount[1] = 0;
  30. Projectile.ProjectileCount[2] = 0;
  31. }
  32. /// <summary>
  33. /// Creates a group of projectiles
  34. /// </summary>
  35. /// <param name="player">Which player shot the bullet</param>
  36. /// <param name="position">Start position of projectile</param>
  37. /// <param name="velocity">Initial velocity of projectile</param>
  38. /// <param name="angle">Direction projectile is facing</param>
  39. /// <param name="time">Game time that this projectile was shot</param>
  40. /// <param name="particles">The particles to add to for effects</param>
  41. public virtual void Add(PlayerIndex player, Vector3 position, Vector3 velocity, float angle, TimeSpan time, Particles particles)
  42. {
  43. ProjectileType projectileType = SpacewarGame.Players[(int)player].ProjectileType;
  44. Vector3 offset = Vector3.Zero;
  45. if (SpacewarGame.Players[(int)player].ProjectileType == ProjectileType.DoubleMachineGun)
  46. {
  47. //Get a perpendicular vector to the direction of fire to offset the double shot
  48. offset.X = -velocity.Y;
  49. offset.Y = velocity.X;
  50. offset.Normalize();
  51. offset *= 10.0f;
  52. }
  53. for (int i = 0; i < SpacewarGame.Settings.Weapons[(int)projectileType].Burst; i++)
  54. {
  55. //If we are not up to max then we can add bullets
  56. if (Projectile.ProjectileCount[(int)player] < SpacewarGame.Settings.Weapons[(int)projectileType].Max)
  57. {
  58. Add(new Projectile(GameInstance, player, position + velocity * i * .1f + offset, velocity, angle, time, particles));
  59. if (offset != Vector3.Zero)
  60. {
  61. Add(new Projectile(GameInstance, player, position + velocity * i * .1f - offset, velocity, angle, time, particles));
  62. }
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// Removes all elements from the <see cref="T:System.Collections.Generic.List`1"></see>.
  68. /// This method hides the real method and just marks the items as ready to be deleted
  69. /// to avoid updating the collection while in a loop and to allow us to maintain the correct
  70. /// bullet count
  71. /// </summary>
  72. public new void Clear()
  73. {
  74. foreach (Projectile projectile in this)
  75. {
  76. projectile.DeleteProjectile();
  77. }
  78. }
  79. }
  80. }