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.

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