2
0

Player.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. using Microsoft.Xna.Framework.Input;
  8. namespace Robot_Rampage
  9. {
  10. static class Player
  11. {
  12. #region Declarations
  13. public static Sprite BaseSprite;
  14. public static Sprite TurretSprite;
  15. private static Vector2 baseAngle = Vector2.Zero;
  16. private static Vector2 turretAngle = Vector2.Zero;
  17. private static float playerSpeed = 90f;
  18. private static Rectangle scrollArea = new Rectangle(150, 100, 500, 400);
  19. #endregion
  20. #region Properties
  21. public static Vector2 PathingNodePosition
  22. {
  23. get
  24. {
  25. return TileMap.GetSquareAtPixel(BaseSprite.WorldCenter);
  26. }
  27. }
  28. #endregion
  29. #region Initialization
  30. public static void Initialize(
  31. Texture2D texture,
  32. Rectangle baseInitialFrame,
  33. int baseFrameCount,
  34. Rectangle turretInitialFrame,
  35. int turretFrameCount,
  36. Vector2 worldLocation)
  37. {
  38. int frameWidth = baseInitialFrame.Width;
  39. int frameHeight = baseInitialFrame.Height;
  40. BaseSprite = new Sprite(
  41. worldLocation,
  42. texture,
  43. baseInitialFrame,
  44. Vector2.Zero);
  45. BaseSprite.BoundingXPadding = 4;
  46. BaseSprite.BoundingYPadding = 4;
  47. BaseSprite.AnimateWhenStopped = false;
  48. for (int x = 1; x < baseFrameCount; x++)
  49. {
  50. BaseSprite.AddFrame(
  51. new Rectangle(
  52. baseInitialFrame.X + (frameHeight * x),
  53. baseInitialFrame.Y,
  54. frameWidth,
  55. frameHeight));
  56. }
  57. TurretSprite = new Sprite(
  58. worldLocation,
  59. texture,
  60. turretInitialFrame,
  61. Vector2.Zero);
  62. TurretSprite.Animate = false;
  63. for (int x = 1; x < turretFrameCount; x++)
  64. {
  65. BaseSprite.AddFrame(
  66. new Rectangle(
  67. turretInitialFrame.X + (frameHeight * x),
  68. turretInitialFrame.Y,
  69. frameWidth,
  70. frameHeight));
  71. }
  72. }
  73. #endregion
  74. #region Input Handling
  75. private static Vector2 handleKeyboardMovement(KeyboardState keyState)
  76. {
  77. Vector2 keyMovement = Vector2.Zero;
  78. if (keyState.IsKeyDown(Keys.W))
  79. keyMovement.Y--;
  80. if (keyState.IsKeyDown(Keys.A))
  81. keyMovement.X--;
  82. if (keyState.IsKeyDown(Keys.S))
  83. keyMovement.Y++;
  84. if (keyState.IsKeyDown(Keys.D))
  85. keyMovement.X++;
  86. return keyMovement;
  87. }
  88. private static Vector2 handleGamePadMovement(GamePadState gamepadState)
  89. {
  90. return new Vector2(
  91. gamepadState.ThumbSticks.Left.X,
  92. -gamepadState.ThumbSticks.Left.Y);
  93. }
  94. private static Vector2 handleKeyboardShots(KeyboardState keyState)
  95. {
  96. Vector2 keyShots = Vector2.Zero;
  97. if (keyState.IsKeyDown(Keys.NumPad1))
  98. keyShots = new Vector2(-1, 1);
  99. if (keyState.IsKeyDown(Keys.NumPad2))
  100. keyShots = new Vector2(0, 1);
  101. if (keyState.IsKeyDown(Keys.NumPad3))
  102. keyShots = new Vector2(1, 1);
  103. if (keyState.IsKeyDown(Keys.NumPad4))
  104. keyShots = new Vector2(-1, 0);
  105. if (keyState.IsKeyDown(Keys.NumPad6))
  106. keyShots = new Vector2(1, 0);
  107. if (keyState.IsKeyDown(Keys.NumPad7))
  108. keyShots = new Vector2(-1, -1);
  109. if (keyState.IsKeyDown(Keys.NumPad8))
  110. keyShots = new Vector2(0, -1);
  111. if (keyState.IsKeyDown(Keys.NumPad9))
  112. keyShots = new Vector2(1, -1);
  113. return keyShots;
  114. }
  115. private static Vector2 handleGamePadShots(GamePadState gamepadState)
  116. {
  117. return new Vector2(
  118. gamepadState.ThumbSticks.Right.X,
  119. -gamepadState.ThumbSticks.Right.Y);
  120. }
  121. private static void handleInput(GameTime gameTime)
  122. {
  123. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  124. Vector2 moveAngle = Vector2.Zero;
  125. Vector2 fireAngle = Vector2.Zero;
  126. moveAngle += handleKeyboardMovement(Keyboard.GetState());
  127. moveAngle +=
  128. handleGamePadMovement(GamePad.GetState(PlayerIndex.One));
  129. fireAngle += handleKeyboardShots(Keyboard.GetState());
  130. fireAngle +=
  131. handleGamePadShots(GamePad.GetState(PlayerIndex.One));
  132. if (moveAngle != Vector2.Zero)
  133. {
  134. moveAngle.Normalize();
  135. baseAngle = moveAngle;
  136. moveAngle = checkTileObstacles(elapsed, moveAngle);
  137. }
  138. if (fireAngle != Vector2.Zero)
  139. {
  140. fireAngle.Normalize();
  141. turretAngle = fireAngle;
  142. if (WeaponManager.CanFireWeapon)
  143. {
  144. WeaponManager.FireWeapon(
  145. TurretSprite.WorldLocation,
  146. fireAngle * WeaponManager.WeaponSpeed);
  147. }
  148. }
  149. BaseSprite.RotateTo(baseAngle);
  150. TurretSprite.RotateTo(turretAngle);
  151. BaseSprite.Velocity = moveAngle * playerSpeed;
  152. repositionCamera(gameTime, moveAngle);
  153. }
  154. #endregion
  155. #region Movement Limitations
  156. private static void clampToWorld()
  157. {
  158. float currentX = BaseSprite.WorldLocation.X;
  159. float currentY = BaseSprite.WorldLocation.Y;
  160. currentX = MathHelper.Clamp(
  161. currentX,
  162. 0,
  163. Camera.WorldRectangle.Right - BaseSprite.FrameWidth);
  164. currentY = MathHelper.Clamp(
  165. currentY,
  166. 0,
  167. Camera.WorldRectangle.Bottom - BaseSprite.FrameHeight);
  168. BaseSprite.WorldLocation = new Vector2(currentX, currentY);
  169. }
  170. private static void repositionCamera(
  171. GameTime gameTime,
  172. Vector2 moveAngle)
  173. {
  174. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  175. float moveScale = playerSpeed * elapsed;
  176. if ((BaseSprite.ScreenRectangle.X < scrollArea.X) &&
  177. (moveAngle.X < 0))
  178. {
  179. Camera.Move(new Vector2(moveAngle.X, 0) * moveScale);
  180. }
  181. if ((BaseSprite.ScreenRectangle.Right > scrollArea.Right) &&
  182. (moveAngle.X > 0))
  183. {
  184. Camera.Move(new Vector2(moveAngle.X, 0) * moveScale);
  185. }
  186. if ((BaseSprite.ScreenRectangle.Y < scrollArea.Y) &&
  187. (moveAngle.Y < 0))
  188. {
  189. Camera.Move(new Vector2(0, moveAngle.Y) * moveScale);
  190. }
  191. if ((BaseSprite.ScreenRectangle.Bottom > scrollArea.Bottom) &&
  192. (moveAngle.Y > 0))
  193. {
  194. Camera.Move(new Vector2(0, moveAngle.Y) * moveScale);
  195. }
  196. }
  197. private static Vector2 checkTileObstacles(
  198. float elapsedTime,
  199. Vector2 moveAngle)
  200. {
  201. Vector2 newHorizontalLocation = BaseSprite.WorldLocation +
  202. (new Vector2(moveAngle.X, 0) * (playerSpeed * elapsedTime));
  203. Vector2 newVerticalLocation = BaseSprite.WorldLocation +
  204. (new Vector2(0, moveAngle.Y) * (playerSpeed * elapsedTime));
  205. Rectangle newHorizontalRect = new Rectangle(
  206. (int)newHorizontalLocation.X,
  207. (int)BaseSprite.WorldLocation.Y,
  208. BaseSprite.FrameWidth,
  209. BaseSprite.FrameHeight);
  210. Rectangle newVerticalRect = new Rectangle(
  211. (int)BaseSprite.WorldLocation.X,
  212. (int)newVerticalLocation.Y,
  213. BaseSprite.FrameWidth,
  214. BaseSprite.FrameHeight);
  215. int horizLeftPixel = 0;
  216. int horizRightPixel = 0;
  217. int vertTopPixel = 0;
  218. int vertBottomPixel = 0;
  219. if (moveAngle.X < 0)
  220. {
  221. horizLeftPixel = (int)newHorizontalRect.Left;
  222. horizRightPixel = (int)BaseSprite.WorldRectangle.Left;
  223. }
  224. if (moveAngle.X > 0)
  225. {
  226. horizLeftPixel = (int)BaseSprite.WorldRectangle.Right;
  227. horizRightPixel = (int)newHorizontalRect.Right;
  228. }
  229. if (moveAngle.Y < 0)
  230. {
  231. vertTopPixel = (int)newVerticalRect.Top;
  232. vertBottomPixel = (int)BaseSprite.WorldRectangle.Top;
  233. }
  234. if (moveAngle.Y > 0)
  235. {
  236. vertTopPixel = (int)BaseSprite.WorldRectangle.Bottom;
  237. vertBottomPixel = (int)newVerticalRect.Bottom;
  238. }
  239. if (moveAngle.X != 0)
  240. {
  241. for (int x = horizLeftPixel; x < horizRightPixel; x++)
  242. {
  243. for (int y = 0; y < BaseSprite.FrameHeight; y++)
  244. {
  245. if (TileMap.IsWallTileByPixel(
  246. new Vector2(x, newHorizontalLocation.Y + y)))
  247. {
  248. moveAngle.X = 0;
  249. break;
  250. }
  251. }
  252. if (moveAngle.X == 0)
  253. {
  254. break;
  255. }
  256. }
  257. }
  258. if (moveAngle.Y != 0)
  259. {
  260. for (int y = vertTopPixel; y < vertBottomPixel; y++)
  261. {
  262. for (int x = 0; x < BaseSprite.FrameWidth; x++)
  263. {
  264. if (TileMap.IsWallTileByPixel(
  265. new Vector2(newVerticalLocation.X + x, y)))
  266. {
  267. moveAngle.Y = 0;
  268. break;
  269. }
  270. }
  271. if (moveAngle.Y == 0)
  272. {
  273. break;
  274. }
  275. }
  276. }
  277. return moveAngle;
  278. }
  279. #endregion
  280. #region Update and Draw
  281. public static void Update(GameTime gameTime)
  282. {
  283. handleInput(gameTime);
  284. BaseSprite.Update(gameTime);
  285. clampToWorld();
  286. TurretSprite.WorldLocation = BaseSprite.WorldLocation;
  287. }
  288. public static void Draw(SpriteBatch spriteBatch)
  289. {
  290. BaseSprite.Draw(spriteBatch);
  291. TurretSprite.Draw(spriteBatch);
  292. }
  293. #endregion
  294. }
  295. }