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.

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