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.

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