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.

211 lines
6.9 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace SemiColinGames {
  6. class Player {
  7. // The player's Facing corresponds to the x-direction that they're looking.
  8. enum Facing {
  9. Left = -1,
  10. Right = 1
  11. };
  12. enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
  13. private const int moveSpeed = 180;
  14. private const int jumpSpeed = -600;
  15. private const int gravity = 2400;
  16. // Details of the sprite image.
  17. // player_1x is 48 x 48, yOffset=5, halfSize=(7, 14)
  18. // Ninja_Female is 96 x 64, yOffset=1, halfSize=(11, 24)
  19. private const int spriteWidth = 96;
  20. private const int spriteHeight = 64;
  21. private const int spriteCenterYOffset = 1;
  22. private readonly Texture2D texture;
  23. // Details of the actual Player model.
  24. // Position is tracked at the Player's center. The Player's bounding box is a rectangle
  25. // centered at that point and extending out by halfSize.X and halfSize.Y.
  26. private Point position = new Point(64, 16 * 13);
  27. private Vector2 halfSize = new Vector2(11, 24);
  28. private int jumps = 0;
  29. private Facing facing = Facing.Right;
  30. private Pose pose = Pose.Jumping;
  31. private double swordSwingTime = 0;
  32. private int swordSwingNum = 0;
  33. private int swordSwingMax = 6;
  34. private float ySpeed = 0;
  35. public Player(Texture2D texture) {
  36. this.texture = texture;
  37. }
  38. public Point Position { get { return position; } }
  39. public void Update(float modelTime, History<Input> input, AABB[] collisionTargets) {
  40. AABB BoxOffset(Point position, int yOffset) {
  41. return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize);
  42. }
  43. AABB Box(Point position) {
  44. return BoxOffset(position, 0);
  45. }
  46. Vector2 movement = HandleInput(modelTime, input);
  47. // Broad test: remove all collision targets nowhere near the player.
  48. // TODO: don't allocate a list here.
  49. var candidates = new List<AABB>();
  50. // Expand the box in the direction of movement. The center is the midpoint of the line
  51. // between the player's current position and their desired movement. The width increases by
  52. // the magnitude of the movement in each direction. We add 1 to each dimension just to be
  53. // sure (the only downside is a small number of false-positive AABBs, which should be
  54. // discarded by later tests anyhow.)
  55. AABB largeBox = new AABB(
  56. new Vector2(position.X + movement.X / 2, position.Y + movement.Y / 2),
  57. new Vector2(halfSize.X + Math.Abs(movement.X) + 1, halfSize.Y + Math.Abs(movement.Y) + 1));
  58. foreach (var box in collisionTargets) {
  59. if (box.Intersect(largeBox) != null) {
  60. Debug.AddRect(box, Color.Green);
  61. candidates.Add(box);
  62. }
  63. }
  64. Point[] movePoints = Line.Rasterize(0, 0, (int) movement.X, (int) movement.Y);
  65. for (int i = 1; i < movePoints.Length; i++) {
  66. int dx = movePoints[i].X - movePoints[i - 1].X;
  67. int dy = movePoints[i].Y - movePoints[i - 1].Y;
  68. if (dy != 0) {
  69. Point newPosition = new Point(position.X, position.Y + dy);
  70. AABB player = Box(newPosition);
  71. bool reject = false;
  72. foreach (var box in candidates) {
  73. if (box.Intersect(player) != null) {
  74. reject = true;
  75. break;
  76. }
  77. }
  78. if (!reject) {
  79. position = newPosition;
  80. }
  81. }
  82. if (dx != 0) {
  83. Point newPosition = new Point(position.X + dx, position.Y);
  84. AABB player = Box(newPosition);
  85. bool reject = false;
  86. foreach (var box in candidates) {
  87. if (box.Intersect(player) != null) {
  88. reject = true;
  89. break;
  90. }
  91. }
  92. if (!reject) {
  93. position = newPosition;
  94. }
  95. }
  96. }
  97. bool standingOnGround = false;
  98. AABB groundIntersect = BoxOffset(position, 1);
  99. foreach (var box in candidates) {
  100. if (groundIntersect.Intersect(box) != null) {
  101. standingOnGround = true;
  102. break;
  103. }
  104. }
  105. if (standingOnGround) {
  106. jumps = 1;
  107. ySpeed = -0.0001f;
  108. Debug.AddRect(Box(position), Color.Cyan);
  109. } else {
  110. jumps = 0;
  111. Debug.AddRect(Box(position), Color.Orange);
  112. }
  113. if (movement.X > 0) {
  114. facing = Facing.Right;
  115. } else if (movement.X < 0) {
  116. facing = Facing.Left;
  117. }
  118. if (swordSwingTime > 0) {
  119. pose = Pose.SwordSwing;
  120. } else if (jumps == 0) {
  121. pose = Pose.Jumping;
  122. } else if (movement.X != 0) {
  123. pose = Pose.Walking;
  124. } else if (input[0].Motion.Y > 0) {
  125. pose = Pose.Stretching;
  126. } else if (input[0].Motion.Y < 0) {
  127. pose = Pose.Crouching;
  128. } else {
  129. pose = Pose.Standing;
  130. }
  131. }
  132. // Returns the desired (dx, dy) for the player to move this frame.
  133. Vector2 HandleInput(float modelTime, History<Input> input) {
  134. Vector2 result = new Vector2() {
  135. X = (int) (input[0].Motion.X * moveSpeed * modelTime)
  136. };
  137. if (input[0].Jump && !input[1].Jump && jumps > 0) {
  138. jumps--;
  139. ySpeed = jumpSpeed;
  140. }
  141. if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
  142. swordSwingTime = 0.3;
  143. swordSwingNum = (swordSwingNum + 1) % swordSwingMax;
  144. }
  145. result.Y = ySpeed * modelTime;
  146. ySpeed += gravity * modelTime;
  147. swordSwingTime -= modelTime;
  148. return result;
  149. }
  150. private int SpriteIndex(Pose pose) {
  151. int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4;
  152. if (frameNum == 3 && pose == Pose.Standing) {
  153. frameNum = 1;
  154. }
  155. switch (pose) {
  156. case Pose.Walking:
  157. return 35 + frameNum;
  158. case Pose.Jumping:
  159. return 35 + frameNum;
  160. case Pose.Stretching:
  161. return (int) Clock.ModelTime.TotalMilliseconds / 125 % 2;
  162. case Pose.SwordSwing:
  163. if (swordSwingTime > 0.2) {
  164. return 0 + swordSwingNum * 3;
  165. } else if (swordSwingTime > 0.1) {
  166. return 1 + swordSwingNum * 3;
  167. } else {
  168. return 2 + swordSwingNum * 3;
  169. }
  170. case Pose.Crouching:
  171. return 26;
  172. case Pose.Standing:
  173. default:
  174. return 29 + frameNum;
  175. }
  176. }
  177. public void Draw(SpriteBatch spriteBatch, Camera camera) {
  178. int index = SpriteIndex(pose);
  179. Rectangle textureSource = new Rectangle(index * spriteWidth, 0, spriteWidth, spriteHeight);
  180. Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
  181. SpriteEffects effect = facing == Facing.Right ?
  182. SpriteEffects.FlipHorizontally : SpriteEffects.None;
  183. Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);
  184. spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
  185. Vector2.One, effect, 0f);
  186. }
  187. }
  188. }