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.

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