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.

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