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.

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