A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.1 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace Jumpy {
  7. class Player {
  8. enum Facing { Left, Right };
  9. enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
  10. enum AirState { Jumping, Ground, Falling };
  11. private Texture2D texture;
  12. private const int spriteSize = 48;
  13. private const int spriteWidth = 7;
  14. private const int moveSpeed = 180;
  15. private const int jumpSpeed = 600;
  16. private const int gravity = 2400;
  17. private Point position = new Point(64, 16);
  18. private Facing facing = Facing.Right;
  19. private Pose pose = Pose.Standing;
  20. private AirState airState = AirState.Ground;
  21. private double swordSwingTime = 0;
  22. private double jumpTime = 0;
  23. private double ySpeed = 0;
  24. public Player(Texture2D texture) {
  25. this.texture = texture;
  26. }
  27. public Point Position { get { return position; } }
  28. private Rectangle Bbox(Point position) {
  29. return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
  30. }
  31. public void Update(
  32. GameTime time, History<GamePadState> gamePad, List<Rectangle> collisionTargets) {
  33. Point oldPosition = position;
  34. AirState oldAirState = airState;
  35. UpdateFromGamePad(time, gamePad);
  36. Rectangle oldBbox = Bbox(oldPosition);
  37. Rectangle playerBbox = Bbox(position);
  38. bool standingOnGround = false;
  39. // TODO: implement https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
  40. // e.g. http://members.chello.at/~easyfilter/bresenham.html
  41. // TODO: currently player doesn't fall through a gap one tile wide; presumably this will
  42. // be fixed by switching to a line-rasterization approach.
  43. foreach (var rect in collisionTargets) {
  44. playerBbox = Bbox(position);
  45. // first we check for left-right collisions...
  46. if (playerBbox.Intersects(rect)) {
  47. if (oldBbox.Right <= rect.Left && playerBbox.Right > rect.Left) {
  48. position.X = rect.Left - spriteWidth;
  49. }
  50. if (oldBbox.Left >= rect.Right && playerBbox.Left < rect.Right) {
  51. position.X = rect.Right + spriteWidth;
  52. }
  53. playerBbox = Bbox(position);
  54. }
  55. // after fixing that, we check for hitting our head or hitting the ground.
  56. if (playerBbox.Intersects(rect)) {
  57. if (oldPosition.Y > position.Y) {
  58. int diff = playerBbox.Top - rect.Bottom;
  59. position.Y -= diff;
  60. } else {
  61. airState = AirState.Ground;
  62. int diff = playerBbox.Bottom - rect.Top;
  63. position.Y -= diff;
  64. }
  65. } else {
  66. playerBbox.Height += 1;
  67. if (playerBbox.Intersects(rect)) {
  68. standingOnGround = true;
  69. Debug.AddRect(rect, Color.Cyan);
  70. } else {
  71. Debug.AddRect(rect, Color.Green);
  72. }
  73. }
  74. }
  75. if (oldAirState != AirState.Ground && standingOnGround) {
  76. airState = AirState.Ground;
  77. ySpeed = 0.0;
  78. }
  79. if (airState == AirState.Ground && !standingOnGround) {
  80. airState = AirState.Falling;
  81. ySpeed = 0.0;
  82. }
  83. if (airState == AirState.Ground) {
  84. Debug.AddRect(playerBbox, Color.Red);
  85. } else if (airState == AirState.Jumping) {
  86. Debug.AddRect(playerBbox, Color.Orange);
  87. } else {
  88. Debug.AddRect(playerBbox, Color.Yellow);
  89. }
  90. }
  91. // TODO: refactor input to have a virtual "which directions & buttons were being pressed"
  92. // instead of complicated if-statements in this function.
  93. // TODO: refactor to use a state-machine.
  94. void UpdateFromGamePad(GameTime time, History<GamePadState> gamePad) {
  95. if (gamePad[0].IsButtonDown(Buttons.A) && gamePad[1].IsButtonUp(Buttons.A) &&
  96. airState == AirState.Ground) {
  97. pose = Pose.Jumping;
  98. airState = AirState.Jumping;
  99. jumpTime = 0.5;
  100. ySpeed = -jumpSpeed;
  101. return;
  102. }
  103. if (gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X)
  104. && swordSwingTime <= 0) {
  105. pose = Pose.SwordSwing;
  106. swordSwingTime = 0.3;
  107. return;
  108. }
  109. Vector2 leftStick = gamePad[0].ThumbSticks.Left;
  110. if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5) {
  111. facing = Facing.Left;
  112. pose = Pose.Walking;
  113. position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
  114. } else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5) {
  115. facing = Facing.Right;
  116. pose = Pose.Walking;
  117. position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
  118. } else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5) {
  119. pose = Pose.Crouching;
  120. } else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5) {
  121. pose = Pose.Stretching;
  122. } else {
  123. pose = Pose.Standing;
  124. }
  125. if (jumpTime > 0) {
  126. jumpTime -= time.ElapsedGameTime.TotalSeconds;
  127. }
  128. if (swordSwingTime > 0) {
  129. swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
  130. pose = Pose.SwordSwing;
  131. }
  132. if (airState == AirState.Jumping || airState == AirState.Falling) {
  133. position.Y += (int) (ySpeed * time.ElapsedGameTime.TotalSeconds);
  134. ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
  135. }
  136. if (airState == AirState.Jumping && pose != Pose.SwordSwing) {
  137. pose = Pose.Jumping;
  138. }
  139. // TODO: also bound player position by the right edge of the World?
  140. position.X = Math.Max(position.X, 0 + spriteWidth);
  141. }
  142. private int spritePosition(Pose pose, GameTime time) {
  143. int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
  144. if (frameNum == 3) {
  145. frameNum = 1;
  146. }
  147. switch (pose) {
  148. case Pose.Walking:
  149. return 6 + frameNum;
  150. case Pose.Stretching:
  151. return 18 + frameNum;
  152. case Pose.Jumping:
  153. if (jumpTime > 0.25) {
  154. return 15;
  155. } else if (jumpTime > 0) {
  156. return 16;
  157. } else {
  158. return 17;
  159. }
  160. case Pose.SwordSwing:
  161. if (swordSwingTime > 0.2) {
  162. return 30;
  163. } else if (swordSwingTime > 0.1) {
  164. return 31;
  165. } else {
  166. return 32;
  167. }
  168. case Pose.Crouching:
  169. return 25;
  170. case Pose.Standing:
  171. default:
  172. return 7;
  173. }
  174. }
  175. public void Draw(SpriteBatch spriteBatch, Camera camera, GameTime time) {
  176. // TODO: don't create so many "new" things that could be cached / precomputed.
  177. int index = spritePosition(pose, time);
  178. Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
  179. Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
  180. SpriteEffects effect = facing == Facing.Right ?
  181. SpriteEffects.FlipHorizontally : SpriteEffects.None;
  182. Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);
  183. spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
  184. Vector2.One, effect, 0f);
  185. }
  186. }
  187. }