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.

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