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.

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