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.

195 lines
6.1 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System.Collections.Generic;
  4. namespace SemiColinGames {
  5. class Player {
  6. // The player's Facing corresponds to the x-direction that they're looking.
  7. enum Facing {
  8. Left = -1,
  9. Right = 1
  10. };
  11. enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
  12. private const int moveSpeed = 180;
  13. private const int jumpSpeed = -600;
  14. private const int gravity = 2400;
  15. private const int spriteSize = 48;
  16. private const int spriteWidth = 7;
  17. private readonly Texture2D texture;
  18. private Point position = new Point(64, 16 * 14);
  19. private int jumps = 0;
  20. private Facing facing = Facing.Right;
  21. private Pose pose = Pose.Jumping;
  22. private double swordSwingTime = 0;
  23. private double jumpTime = 0;
  24. private float ySpeed = 0;
  25. public Player(Texture2D texture) {
  26. this.texture = texture;
  27. }
  28. public Point Position { get { return position; } }
  29. private Rectangle Bbox(Point position) {
  30. return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
  31. }
  32. private Aabb Box(Point position) {
  33. return Box(position, 0);
  34. }
  35. private Aabb Box(Point position, int yOffset) {
  36. return new Aabb(new Vector2(position.X, position.Y - 7 + 13 + yOffset),
  37. new Vector2(spriteWidth, 13));
  38. }
  39. public void Update(float modelTime, History<Input> input, Rectangle[] collisionTargets) {
  40. Point oldPosition = position;
  41. Vector2 movement = HandleInput(modelTime, input);
  42. position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y));
  43. Rectangle oldBbox = Bbox(oldPosition);
  44. Rectangle playerBbox = Bbox(position);
  45. bool standingOnGround = false;
  46. // TODO: we shouldn't hardcode the tile sizes here.
  47. Vector2 halfBoxSize = new Vector2(World.TileSize / 2, World.TileSize / 2);
  48. foreach (var rect in collisionTargets) {
  49. Aabb rectBox = new Aabb(
  50. new Vector2(rect.X + World.TileSize / 2, rect.Y + World.TileSize / 2), halfBoxSize);
  51. Aabb playerBox = Box(position);
  52. playerBbox = Bbox(position);
  53. // first we check for left-right collisions...
  54. if (playerBox.Intersect(rectBox) != null) {
  55. if (oldBbox.Right <= rect.Left && playerBbox.Right > rect.Left) {
  56. position.X = rect.Left - spriteWidth;
  57. }
  58. if (oldBbox.Left >= rect.Right && playerBbox.Left < rect.Right) {
  59. position.X = rect.Right + spriteWidth;
  60. }
  61. playerBox = Box(position);
  62. }
  63. // after fixing that, we check for hitting our head or hitting the ground.
  64. if (playerBox.Intersect(rectBox) != null) {
  65. if (oldPosition.Y > position.Y) {
  66. int diff = playerBbox.Top - rect.Bottom;
  67. position.Y -= diff;
  68. // TODO: set ySpeed = 0 here so that bonking our head actually reduces hangtime?
  69. } else {
  70. standingOnGround = true;
  71. int diff = playerBbox.Bottom - rect.Top;
  72. position.Y -= diff;
  73. }
  74. } else {
  75. playerBox = Box(position, 1);
  76. if (playerBox.Intersect(rectBox) != null) {
  77. standingOnGround = true;
  78. Debug.AddRect(rect, Color.Cyan);
  79. } else {
  80. Debug.AddRect(rect, Color.Green);
  81. }
  82. }
  83. }
  84. if (standingOnGround) {
  85. jumps = 1;
  86. ySpeed = 0;
  87. Debug.AddRect(playerBbox, Color.Red);
  88. } else {
  89. jumps = 0;
  90. Debug.AddRect(playerBbox, Color.Orange);
  91. }
  92. if (movement.X > 0) {
  93. facing = Facing.Right;
  94. } else if (movement.X < 0) {
  95. facing = Facing.Left;
  96. }
  97. if (swordSwingTime > 0) {
  98. pose = Pose.SwordSwing;
  99. } else if (jumps == 0) {
  100. pose = Pose.Jumping;
  101. } else if (movement.X != 0) {
  102. pose = Pose.Walking;
  103. } else if (input[0].Motion.Y > 0) {
  104. pose = Pose.Stretching;
  105. } else if (input[0].Motion.Y < 0) {
  106. pose = Pose.Crouching;
  107. } else {
  108. pose = Pose.Standing;
  109. }
  110. }
  111. // Returns the desired (dx, dy) for the player to move this frame.
  112. Vector2 HandleInput(float modelTime, History<Input> input) {
  113. Vector2 result = new Vector2() {
  114. X = (int) (input[0].Motion.X * moveSpeed * modelTime)
  115. };
  116. if (input[0].Jump && !input[1].Jump && jumps > 0) {
  117. jumpTime = 0.3;
  118. jumps--;
  119. ySpeed = jumpSpeed;
  120. }
  121. if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
  122. swordSwingTime = 0.3;
  123. }
  124. result.Y = ySpeed * modelTime;
  125. ySpeed += gravity * modelTime;
  126. jumpTime -= modelTime;
  127. swordSwingTime -= modelTime;
  128. return result;
  129. }
  130. private int SpriteIndex(Pose pose) {
  131. int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4;
  132. if (frameNum == 3) {
  133. frameNum = 1;
  134. }
  135. switch (pose) {
  136. case Pose.Walking:
  137. return 6 + frameNum;
  138. case Pose.Stretching:
  139. return 18 + frameNum;
  140. case Pose.Jumping:
  141. if (jumpTime > 0.2) {
  142. return 15;
  143. } else if (jumpTime > 0.1) {
  144. return 16;
  145. } else {
  146. return 17;
  147. }
  148. case Pose.SwordSwing:
  149. if (swordSwingTime > 0.2) {
  150. return 30;
  151. } else if (swordSwingTime > 0.1) {
  152. return 31;
  153. } else {
  154. return 32;
  155. }
  156. case Pose.Crouching:
  157. return 25;
  158. case Pose.Standing:
  159. default:
  160. return 7;
  161. }
  162. }
  163. public void Draw(SpriteBatch spriteBatch, Camera camera) {
  164. int index = SpriteIndex(pose);
  165. Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
  166. Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
  167. SpriteEffects effect = facing == Facing.Right ?
  168. SpriteEffects.FlipHorizontally : SpriteEffects.None;
  169. Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);
  170. spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
  171. Vector2.One, effect, 0f);
  172. }
  173. }
  174. }