Player.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.Content;
  8. using Microsoft.Xna.Framework.Input;
  9. using Tile_Engine;
  10. namespace Gemstone_Hunter
  11. {
  12. public class Player : GameObject
  13. {
  14. private Vector2 fallSpeed = new Vector2(0, 20);
  15. private float moveScale = 180.0f;
  16. private bool dead = false;
  17. private int score = 0;
  18. private int livesRemaining = 3;
  19. public bool Dead
  20. {
  21. get { return dead; }
  22. }
  23. public int Score
  24. {
  25. get { return score; }
  26. set { score = value; }
  27. }
  28. public int LivesRemaining
  29. {
  30. get { return livesRemaining; }
  31. set { livesRemaining = value; }
  32. }
  33. #region Constructor
  34. public Player(ContentManager content)
  35. {
  36. animations.Add("idle",
  37. new AnimationStrip(
  38. content.Load<Texture2D>(@"Textures\Sprites\Player\Idle"),
  39. 48,
  40. "idle"));
  41. animations["idle"].LoopAnimation = true;
  42. animations.Add("run",
  43. new AnimationStrip(
  44. content.Load<Texture2D>(@"Textures\Sprites\Player\Run"),
  45. 48,
  46. "run"));
  47. animations["run"].LoopAnimation = true;
  48. animations.Add("jump",
  49. new AnimationStrip(
  50. content.Load<Texture2D>(@"Textures\Sprites\Player\Jump"),
  51. 48,
  52. "jump"));
  53. animations["jump"].LoopAnimation = false;
  54. animations["jump"].FrameLength = 0.08f;
  55. animations["jump"].NextAnimation = "idle";
  56. animations.Add("die",
  57. new AnimationStrip(
  58. content.Load<Texture2D>(@"Textures\Sprites\Player\Die"),
  59. 48,
  60. "die"));
  61. animations["die"].LoopAnimation = false;
  62. frameWidth = 48;
  63. frameHeight = 48;
  64. CollisionRectangle = new Rectangle(9, 1, 30, 46);
  65. drawDepth = 0.825f;
  66. enabled = true;
  67. codeBasedBlocks = false;
  68. PlayAnimation("idle");
  69. }
  70. #endregion
  71. #region Public Methods
  72. public override void Update(GameTime gameTime)
  73. {
  74. if (!Dead)
  75. {
  76. string newAnimation = "idle";
  77. velocity = new Vector2(0, velocity.Y);
  78. GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
  79. KeyboardState keyState = Keyboard.GetState();
  80. if (keyState.IsKeyDown(Keys.Left) ||
  81. (gamePad.ThumbSticks.Left.X < -0.3f))
  82. {
  83. flipped = false;
  84. newAnimation = "run";
  85. velocity = new Vector2(-moveScale, velocity.Y);
  86. }
  87. if (keyState.IsKeyDown(Keys.Right) ||
  88. (gamePad.ThumbSticks.Left.X > 0.3f))
  89. {
  90. flipped = true;
  91. newAnimation = "run";
  92. velocity = new Vector2(moveScale, velocity.Y);
  93. }
  94. if (keyState.IsKeyDown(Keys.Space) ||
  95. (gamePad.Buttons.A == ButtonState.Pressed))
  96. {
  97. if (onGround)
  98. {
  99. Jump();
  100. newAnimation = "jump";
  101. }
  102. }
  103. if (keyState.IsKeyDown(Keys.Up) ||
  104. gamePad.ThumbSticks.Left.Y > 0.3f)
  105. {
  106. checkLevelTransition();
  107. }
  108. if (currentAnimation == "jump")
  109. newAnimation = "jump";
  110. if (newAnimation != currentAnimation)
  111. {
  112. PlayAnimation(newAnimation);
  113. }
  114. }
  115. velocity += fallSpeed;
  116. repositionCamera();
  117. base.Update(gameTime);
  118. }
  119. public void Jump()
  120. {
  121. velocity.Y = -500;
  122. }
  123. public void Kill()
  124. {
  125. PlayAnimation("die");
  126. LivesRemaining--;
  127. velocity.X = 0;
  128. dead = true;
  129. }
  130. public void Revive()
  131. {
  132. PlayAnimation("idle");
  133. dead = false;
  134. }
  135. #endregion
  136. #region Helper Methods
  137. private void repositionCamera()
  138. {
  139. int screenLocX = (int)Camera.WorldToScreen(worldLocation).X;
  140. if (screenLocX > 500)
  141. {
  142. Camera.Move(new Vector2(screenLocX - 500, 0));
  143. }
  144. if (screenLocX < 200)
  145. {
  146. Camera.Move(new Vector2(screenLocX - 200, 0));
  147. }
  148. }
  149. private void checkLevelTransition()
  150. {
  151. Vector2 centerCell = TileMap.GetCellByPixel(WorldCenter);
  152. if (TileMap.CellCodeValue(centerCell).StartsWith("T_"))
  153. {
  154. string[] code = TileMap.CellCodeValue(centerCell).Split('_');
  155. if (code.Length != 4)
  156. return;
  157. LevelManager.LoadLevel(int.Parse(code[1]));
  158. WorldLocation = new Vector2(
  159. int.Parse(code[2]) * TileMap.TileWidth,
  160. int.Parse(code[3]) * TileMap.TileHeight);
  161. LevelManager.RespawnLocation = WorldLocation;
  162. velocity = Vector2.Zero;
  163. }
  164. }
  165. #endregion
  166. }
  167. }