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.

205 lines
6.9 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace SemiColinGames {
  7. class Player {
  8. enum Facing { Left, Right };
  9. enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
  10. enum AirState { Jumping, Ground, Falling };
  11. private Texture2D texture;
  12. private const int spriteSize = 48;
  13. private const int spriteWidth = 7;
  14. private const int moveSpeed = 180;
  15. private const int jumpSpeed = 600;
  16. private const int gravity = 2400;
  17. private Point position = new Point(64, 16);
  18. private Facing facing = Facing.Right;
  19. private Pose pose = Pose.Standing;
  20. private AirState airState = AirState.Ground;
  21. private double swordSwingTime = 0;
  22. private double jumpTime = 0;
  23. private double ySpeed = 0;
  24. public Player(Texture2D texture) {
  25. this.texture = texture;
  26. }
  27. public Point Position { get { return position; } }
  28. private Rectangle Bbox(Point position) {
  29. return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
  30. }
  31. public void Update(
  32. GameTime time, History<GamePadState> gamePad, History<KeyboardState> keyboard,
  33. List<Rectangle> collisionTargets) {
  34. Point oldPosition = position;
  35. AirState oldAirState = airState;
  36. UpdateFromInput(time, gamePad, keyboard);
  37. Rectangle oldBbox = Bbox(oldPosition);
  38. Rectangle playerBbox = Bbox(position);
  39. bool standingOnGround = false;
  40. foreach (var rect in collisionTargets) {
  41. playerBbox = Bbox(position);
  42. // first we check for left-right collisions...
  43. if (playerBbox.Intersects(rect)) {
  44. if (oldBbox.Right <= rect.Left && playerBbox.Right > rect.Left) {
  45. position.X = rect.Left - spriteWidth;
  46. }
  47. if (oldBbox.Left >= rect.Right && playerBbox.Left < rect.Right) {
  48. position.X = rect.Right + spriteWidth;
  49. }
  50. playerBbox = Bbox(position);
  51. }
  52. // after fixing that, we check for hitting our head or hitting the ground.
  53. if (playerBbox.Intersects(rect)) {
  54. if (oldPosition.Y > position.Y) {
  55. int diff = playerBbox.Top - rect.Bottom;
  56. position.Y -= diff;
  57. } else {
  58. airState = AirState.Ground;
  59. int diff = playerBbox.Bottom - rect.Top;
  60. position.Y -= diff;
  61. }
  62. } else {
  63. playerBbox.Height += 1;
  64. if (playerBbox.Intersects(rect)) {
  65. standingOnGround = true;
  66. Debug.AddRect(rect, Color.Cyan);
  67. } else {
  68. Debug.AddRect(rect, Color.Green);
  69. }
  70. }
  71. }
  72. if (oldAirState != AirState.Ground && standingOnGround) {
  73. airState = AirState.Ground;
  74. ySpeed = 0.0;
  75. }
  76. if (airState == AirState.Ground && !standingOnGround) {
  77. airState = AirState.Falling;
  78. ySpeed = 0.0;
  79. }
  80. if (airState == AirState.Ground) {
  81. Debug.AddRect(playerBbox, Color.Red);
  82. } else if (airState == AirState.Jumping) {
  83. Debug.AddRect(playerBbox, Color.Orange);
  84. } else {
  85. Debug.AddRect(playerBbox, Color.Yellow);
  86. }
  87. }
  88. void UpdateFromInput(
  89. GameTime time, History<GamePadState> gamePad, History<KeyboardState> keyboard) {
  90. if ((gamePad[0].IsButtonDown(Buttons.A) && gamePad[1].IsButtonUp(Buttons.A) ||
  91. keyboard[0].IsKeyDown(Keys.J) && keyboard[1].IsKeyUp(Keys.J)) &&
  92. airState == AirState.Ground) {
  93. pose = Pose.Jumping;
  94. airState = AirState.Jumping;
  95. jumpTime = 0.5;
  96. ySpeed = -jumpSpeed;
  97. return;
  98. }
  99. if ((gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X) ||
  100. keyboard[0].IsKeyDown(Keys.K) && keyboard[1].IsKeyUp(Keys.K))
  101. && swordSwingTime <= 0) {
  102. pose = Pose.SwordSwing;
  103. swordSwingTime = 0.3;
  104. return;
  105. }
  106. Vector2 leftStick = gamePad[0].ThumbSticks.Left;
  107. if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5 ||
  108. keyboard[0].IsKeyDown(Keys.A)) {
  109. facing = Facing.Left;
  110. pose = Pose.Walking;
  111. position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
  112. } else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5 ||
  113. keyboard[0].IsKeyDown(Keys.D)) {
  114. facing = Facing.Right;
  115. pose = Pose.Walking;
  116. position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
  117. } else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5 ||
  118. keyboard[0].IsKeyDown(Keys.S)) {
  119. pose = Pose.Crouching;
  120. } else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5 ||
  121. keyboard[0].IsKeyDown(Keys.W)) {
  122. pose = Pose.Stretching;
  123. } else {
  124. pose = Pose.Standing;
  125. }
  126. if (jumpTime > 0) {
  127. jumpTime -= time.ElapsedGameTime.TotalSeconds;
  128. }
  129. if (swordSwingTime > 0) {
  130. swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
  131. pose = Pose.SwordSwing;
  132. }
  133. if (airState == AirState.Jumping || airState == AirState.Falling) {
  134. position.Y += (int) (ySpeed * time.ElapsedGameTime.TotalSeconds);
  135. ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
  136. }
  137. if (airState == AirState.Jumping && pose != Pose.SwordSwing) {
  138. pose = Pose.Jumping;
  139. }
  140. position.X = Math.Max(position.X, 0 + spriteWidth);
  141. }
  142. private int SpritePosition(Pose pose, GameTime time) {
  143. int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
  144. if (frameNum == 3) {
  145. frameNum = 1;
  146. }
  147. switch (pose) {
  148. case Pose.Walking:
  149. return 6 + frameNum;
  150. case Pose.Stretching:
  151. return 18 + frameNum;
  152. case Pose.Jumping:
  153. if (jumpTime > 0.25) {
  154. return 15;
  155. } else if (jumpTime > 0) {
  156. return 16;
  157. } else {
  158. return 17;
  159. }
  160. case Pose.SwordSwing:
  161. if (swordSwingTime > 0.2) {
  162. return 30;
  163. } else if (swordSwingTime > 0.1) {
  164. return 31;
  165. } else {
  166. return 32;
  167. }
  168. case Pose.Crouching:
  169. return 25;
  170. case Pose.Standing:
  171. default:
  172. return 7;
  173. }
  174. }
  175. public void Draw(SpriteBatch spriteBatch, Camera camera, GameTime time) {
  176. int index = SpritePosition(pose, time);
  177. Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
  178. Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
  179. SpriteEffects effect = facing == Facing.Right ?
  180. SpriteEffects.FlipHorizontally : SpriteEffects.None;
  181. Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);
  182. spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
  183. Vector2.One, effect, 0f);
  184. }
  185. }
  186. }