ShotManager.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace Asteroid_Belt_Assault
  8. {
  9. class ShotManager
  10. {
  11. public List<Sprite> Shots = new List<Sprite>();
  12. private Rectangle screenBounds;
  13. private static Texture2D Texture;
  14. private static Rectangle InitialFrame;
  15. private static int FrameCount;
  16. private float shotSpeed;
  17. private static int CollisionRadius;
  18. public ShotManager(
  19. Texture2D texture,
  20. Rectangle initialFrame,
  21. int frameCount,
  22. int collisionRadius,
  23. float shotSpeed,
  24. Rectangle screenBounds)
  25. {
  26. Texture = texture;
  27. InitialFrame = initialFrame;
  28. FrameCount = frameCount;
  29. CollisionRadius = collisionRadius;
  30. this.shotSpeed = shotSpeed;
  31. this.screenBounds = screenBounds;
  32. }
  33. public void FireShot(
  34. Vector2 location,
  35. Vector2 velocity,
  36. bool playerFired)
  37. {
  38. Sprite thisShot = new Sprite(
  39. location,
  40. Texture,
  41. InitialFrame,
  42. velocity);
  43. thisShot.Velocity *= shotSpeed;
  44. for (int x = 1; x < FrameCount; x++)
  45. {
  46. thisShot.AddFrame(new Rectangle(
  47. InitialFrame.X + (InitialFrame.Width * x),
  48. InitialFrame.Y,
  49. InitialFrame.Width,
  50. InitialFrame.Height));
  51. }
  52. thisShot.CollisionRadius = CollisionRadius;
  53. Shots.Add(thisShot);
  54. if (playerFired)
  55. {
  56. SoundManager.PlayPlayerShot();
  57. }
  58. else
  59. {
  60. SoundManager.PlayEnemyShot();
  61. }
  62. }
  63. public void Update(GameTime gameTime)
  64. {
  65. for (int x = Shots.Count - 1; x >= 0; x--)
  66. {
  67. Shots[x].Update(gameTime);
  68. if (!screenBounds.Intersects(Shots[x].Destination))
  69. {
  70. Shots.RemoveAt(x);
  71. }
  72. }
  73. }
  74. public void Draw(SpriteBatch spriteBatch)
  75. {
  76. foreach (Sprite shot in Shots)
  77. {
  78. shot.Draw(spriteBatch);
  79. }
  80. }
  81. }
  82. }