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.

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