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.

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