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.

240 lines
8.1 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace SemiColinGames {
  6. public class Player {
  7. private enum Pose { Walking, Standing, 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 Vector2 position;
  21. private Vector2 halfSize = new Vector2(11, 24);
  22. // Fractional-pixel movement that was left over from a previous frame's movement.
  23. // Useful so that we can run at a slow time-step and still get non-zero motion.
  24. private Vector2 residual = Vector2.Zero;
  25. private int jumps = 0;
  26. private Pose pose = Pose.Jumping;
  27. private double swordSwingTime = 0;
  28. private int swordSwingNum = 0;
  29. private float ySpeed = 0;
  30. private float invincibilityTime = 0;
  31. // For passing into Line.Rasterize() during movement updates.
  32. private readonly IList<Point> movePoints = new ProfilingList<Point>(32, "Player.movePoints");
  33. // Possible hitboxes for player <-> obstacles.
  34. private readonly IList<AABB> candidates = new ProfilingList<AABB>(8, "Player.candidates");
  35. public Player(Vector2 position, int facing) {
  36. this.position = position;
  37. Facing = facing;
  38. Health = MaxHealth;
  39. StandingOnGround = false;
  40. }
  41. public bool StandingOnGround { get; private set; }
  42. public int MaxHealth { get; private set; } = 3;
  43. public int Health { get; private set; }
  44. public int Facing { get; private set; }
  45. public Vector2 Position { get { return position; } }
  46. public void Update(float modelTime, World world, History<Input> input) {
  47. AABB BoxOffset(Vector2 position, int yOffset) {
  48. return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize);
  49. }
  50. AABB Box(Vector2 position) {
  51. return BoxOffset(position, 0);
  52. }
  53. invincibilityTime -= modelTime;
  54. Vector2 inputMovement = HandleInput(modelTime, input);
  55. Vector2 movement = Vector2.Add(residual, inputMovement);
  56. residual = new Vector2(movement.X - (int) movement.X, movement.Y - (int) movement.Y);
  57. // Broad test: remove all collision targets nowhere near the player.
  58. candidates.Clear();
  59. // Expand the box in the direction of movement. The center is the midpoint of the line
  60. // between the player's current position and their desired movement. The width increases by
  61. // the magnitude of the movement in each direction. We add 1 to each dimension just to be
  62. // sure (the only downside is a small number of false-positive AABBs, which should be
  63. // discarded by later tests anyhow.)
  64. AABB largeBox = new AABB(
  65. Vector2.Add(position, Vector2.Divide(movement, 2)),
  66. Vector2.Add(halfSize, new Vector2(Math.Abs(movement.X) + 1, Math.Abs(movement.Y) + 1)));
  67. foreach (var box in world.CollisionTargets) {
  68. if (box.Intersect(largeBox) != null) {
  69. // Debug.AddRect(box, Color.Green);
  70. candidates.Add(box);
  71. }
  72. }
  73. bool harmedByCollision = false;
  74. Line.Rasterize(0, 0, (int) movement.X, (int) movement.Y, movePoints);
  75. for (int i = 1; i < movePoints.Count; i++) {
  76. int dx = movePoints[i].X - movePoints[i - 1].X;
  77. int dy = movePoints[i].Y - movePoints[i - 1].Y;
  78. if (dy != 0) {
  79. Vector2 newPosition = new Vector2(position.X, position.Y + dy);
  80. AABB player = Box(newPosition);
  81. bool reject = false;
  82. foreach (var box in candidates) {
  83. if (box.Intersect(player) != null) {
  84. Debug.AddRect(box, Color.Cyan);
  85. reject = true;
  86. if (box.Tile?.IsHazard ?? false) {
  87. Debug.AddRect(box, Color.Red);
  88. harmedByCollision = true;
  89. }
  90. }
  91. }
  92. if (!reject) {
  93. position = newPosition;
  94. }
  95. }
  96. if (dx != 0) {
  97. Vector2 newPosition = new Vector2(position.X + dx, position.Y);
  98. AABB player = Box(newPosition);
  99. bool reject = false;
  100. foreach (var box in candidates) {
  101. if (box.Intersect(player) != null) {
  102. Debug.AddRect(box, Color.Cyan);
  103. reject = true;
  104. if (box.Tile?.IsHazard ?? false) {
  105. Debug.AddRect(box, Color.Red);
  106. harmedByCollision = true;
  107. }
  108. }
  109. }
  110. if (!reject) {
  111. position = newPosition;
  112. }
  113. }
  114. }
  115. StandingOnGround = false;
  116. AABB groundIntersect = BoxOffset(position, 1);
  117. foreach (var box in candidates) {
  118. if (groundIntersect.Intersect(box) != null) {
  119. Debug.AddRect(box, Color.Cyan);
  120. StandingOnGround = true;
  121. if (box.Tile?.IsHazard ?? false) {
  122. Debug.AddRect(box, Color.Red);
  123. harmedByCollision = true;
  124. }
  125. }
  126. }
  127. if (StandingOnGround) {
  128. jumps = 1;
  129. ySpeed = -0.0001f;
  130. Debug.AddRect(Box(position), Color.Cyan);
  131. } else {
  132. jumps = 0;
  133. Debug.AddRect(Box(position), Color.Orange);
  134. }
  135. if (harmedByCollision && invincibilityTime <= 0) {
  136. world.ScreenShake();
  137. Health -= 1;
  138. invincibilityTime = 0.6f;
  139. }
  140. if (inputMovement.X > 0) {
  141. Facing = 1;
  142. } else if (inputMovement.X < 0) {
  143. Facing = -1;
  144. }
  145. if (swordSwingTime > 0) {
  146. pose = Pose.SwordSwing;
  147. } else if (jumps == 0) {
  148. pose = Pose.Jumping;
  149. } else if (inputMovement.X != 0) {
  150. pose = Pose.Walking;
  151. } else {
  152. pose = Pose.Standing;
  153. }
  154. }
  155. // Returns the desired (dx, dy) for the player to move this frame.
  156. Vector2 HandleInput(float modelTime, History<Input> input) {
  157. Vector2 result = new Vector2() {
  158. X = input[0].Motion.X * moveSpeed * modelTime
  159. };
  160. if (input[0].Jump && !input[1].Jump && jumps > 0) {
  161. jumps--;
  162. ySpeed = jumpSpeed;
  163. }
  164. if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
  165. swordSwingTime = 0.3;
  166. swordSwingNum++;
  167. SoundEffects.SwordSwings[swordSwingNum % SoundEffects.SwordSwings.Length].Play();
  168. }
  169. result.Y = ySpeed * modelTime;
  170. ySpeed += gravity * modelTime;
  171. swordSwingTime -= modelTime;
  172. if (input[0].IsAbsoluteMotion) {
  173. if (input[1].Motion.X == 0) {
  174. result.X = input[0].Motion.X;
  175. } else {
  176. result.X = 0;
  177. }
  178. }
  179. return result;
  180. }
  181. private Rectangle GetTextureSource(Pose pose) {
  182. double time = Clock.ModelTime.TotalSeconds;
  183. switch (pose) {
  184. case Pose.Walking:
  185. case Pose.Jumping:
  186. return Sprites.Ninja.GetTextureSource("run", time);
  187. case Pose.SwordSwing:
  188. // TODO: make a proper animation class & FSM-driven animations.
  189. return Sprites.Ninja.GetTextureSource(
  190. "attack_sword", 0.3 - swordSwingTime);
  191. case Pose.Standing:
  192. default:
  193. return Sprites.Ninja.GetTextureSource("idle", time);
  194. }
  195. }
  196. public void Draw(SpriteBatch spriteBatch) {
  197. Rectangle textureSource = GetTextureSource(pose);
  198. Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
  199. SpriteEffects effect = Facing == 1 ?
  200. SpriteEffects.None : SpriteEffects.FlipHorizontally;
  201. Color color = Color.White;
  202. if (invincibilityTime > 0 && invincibilityTime % 0.2f > 0.1f) {
  203. color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
  204. }
  205. spriteBatch.Draw(Textures.Ninja.Get, Vector2.Floor(position), textureSource, color, 0f,
  206. spriteCenter, Vector2.One, effect, 0f);
  207. }
  208. }
  209. }