PlayerManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.Input;
  7. using Microsoft.Xna.Framework.Graphics;
  8. namespace Asteroid_Belt_Assault
  9. {
  10. class PlayerManager
  11. {
  12. public Sprite playerSprite;
  13. private float playerSpeed = 160.0f;
  14. private Rectangle playerAreaLimit;
  15. public long PlayerScore = 0;
  16. public int LivesRemaining = 3;
  17. public bool Destroyed = false;
  18. private Vector2 gunOffset = new Vector2(25, 10);
  19. private float shotTimer = 0.0f;
  20. private float minShotTimer = 0.2f;
  21. private int playerRadius = 15;
  22. public ShotManager PlayerShotManager;
  23. public PlayerManager(
  24. Texture2D texture,
  25. Rectangle initialFrame,
  26. int frameCount,
  27. Rectangle screenBounds)
  28. {
  29. playerSprite = new Sprite(
  30. new Vector2(500, 500),
  31. texture,
  32. initialFrame,
  33. Vector2.Zero);
  34. PlayerShotManager = new ShotManager(
  35. texture,
  36. new Rectangle(0, 300, 5, 5),
  37. 4,
  38. 2,
  39. 250f,
  40. screenBounds);
  41. playerAreaLimit =
  42. new Rectangle(
  43. 0,
  44. screenBounds.Height / 2,
  45. screenBounds.Width,
  46. screenBounds.Height / 2);
  47. for (int x = 1; x < frameCount; x++)
  48. {
  49. playerSprite.AddFrame(
  50. new Rectangle(
  51. initialFrame.X + (initialFrame.Width * x),
  52. initialFrame.Y,
  53. initialFrame.Width,
  54. initialFrame.Height));
  55. }
  56. playerSprite.CollisionRadius = playerRadius;
  57. }
  58. private void FireShot()
  59. {
  60. if (shotTimer >= minShotTimer)
  61. {
  62. PlayerShotManager.FireShot(
  63. playerSprite.Location + gunOffset,
  64. new Vector2(0, -1),
  65. true);
  66. shotTimer = 0.0f;
  67. }
  68. }
  69. private void HandleKeyboardInput(KeyboardState keyState)
  70. {
  71. if (keyState.IsKeyDown(Keys.Up))
  72. {
  73. playerSprite.Velocity += new Vector2(0, -1);
  74. }
  75. if (keyState.IsKeyDown(Keys.Down))
  76. {
  77. playerSprite.Velocity += new Vector2(0, 1);
  78. }
  79. if (keyState.IsKeyDown(Keys.Left))
  80. {
  81. playerSprite.Velocity += new Vector2(-1, 0);
  82. }
  83. if (keyState.IsKeyDown(Keys.Right))
  84. {
  85. playerSprite.Velocity += new Vector2(1, 0);
  86. }
  87. if (keyState.IsKeyDown(Keys.Space))
  88. {
  89. FireShot();
  90. }
  91. }
  92. private void HandleGamepadInput(GamePadState gamePadState)
  93. {
  94. playerSprite.Velocity +=
  95. new Vector2(
  96. gamePadState.ThumbSticks.Left.X,
  97. -gamePadState.ThumbSticks.Left.Y);
  98. if (gamePadState.Buttons.A == ButtonState.Pressed)
  99. {
  100. FireShot();
  101. }
  102. }
  103. private void imposeMovementLimits()
  104. {
  105. Vector2 location = playerSprite.Location;
  106. if (location.X < playerAreaLimit.X)
  107. location.X = playerAreaLimit.X;
  108. if (location.X >
  109. (playerAreaLimit.Right - playerSprite.Source.Width))
  110. location.X =
  111. (playerAreaLimit.Right - playerSprite.Source.Width);
  112. if (location.Y < playerAreaLimit.Y)
  113. location.Y = playerAreaLimit.Y;
  114. if (location.Y >
  115. (playerAreaLimit.Bottom - playerSprite.Source.Height))
  116. location.Y =
  117. (playerAreaLimit.Bottom - playerSprite.Source.Height);
  118. playerSprite.Location = location;
  119. }
  120. public void Update(GameTime gameTime)
  121. {
  122. PlayerShotManager.Update(gameTime);
  123. if (!Destroyed)
  124. {
  125. playerSprite.Velocity = Vector2.Zero;
  126. shotTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  127. HandleKeyboardInput(Keyboard.GetState());
  128. HandleGamepadInput(GamePad.GetState(PlayerIndex.One));
  129. playerSprite.Velocity.Normalize();
  130. playerSprite.Velocity *= playerSpeed;
  131. playerSprite.Update(gameTime);
  132. imposeMovementLimits();
  133. }
  134. }
  135. public void Draw(SpriteBatch spriteBatch)
  136. {
  137. PlayerShotManager.Draw(spriteBatch);
  138. if (!Destroyed)
  139. {
  140. playerSprite.Draw(spriteBatch);
  141. }
  142. }
  143. }
  144. }