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