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.

241 lines
7.7 KiB

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