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.

175 lines
5.6 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System.Collections.Generic;
  4. namespace SemiColinGames {
  5. class Player {
  6. enum Facing { Left, Right };
  7. 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 = 2400;
  11. private Texture2D texture;
  12. private const int spriteSize = 48;
  13. private const int spriteWidth = 7;
  14. private Point position = new Point(64, 16);
  15. private int jumps = 0;
  16. private Facing facing = Facing.Right;
  17. private Pose pose = Pose.Jumping;
  18. private double swordSwingTime = 0;
  19. private double jumpTime = 0;
  20. private float ySpeed = 0;
  21. public Player(Texture2D texture) {
  22. this.texture = texture;
  23. }
  24. public Point Position { get { return position; } }
  25. private Rectangle Bbox(Point position) {
  26. return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
  27. }
  28. public void Update(GameTime time, History<Input> input, List<Rectangle> collisionTargets) {
  29. Point oldPosition = position;
  30. Vector2 movement = HandleInput(time, input);
  31. position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y));
  32. Rectangle oldBbox = Bbox(oldPosition);
  33. Rectangle playerBbox = Bbox(position);
  34. bool standingOnGround = false;
  35. foreach (var rect in collisionTargets) {
  36. playerBbox = Bbox(position);
  37. // first we check for left-right collisions...
  38. if (playerBbox.Intersects(rect)) {
  39. if (oldBbox.Right <= rect.Left && playerBbox.Right > rect.Left) {
  40. position.X = rect.Left - spriteWidth;
  41. }
  42. if (oldBbox.Left >= rect.Right && playerBbox.Left < rect.Right) {
  43. position.X = rect.Right + spriteWidth;
  44. }
  45. playerBbox = Bbox(position);
  46. }
  47. // after fixing that, we check for hitting our head or hitting the ground.
  48. if (playerBbox.Intersects(rect)) {
  49. if (oldPosition.Y > position.Y) {
  50. int diff = playerBbox.Top - rect.Bottom;
  51. position.Y -= diff;
  52. // TODO: set ySpeed = 0 here so that bonking our head actually reduces hangtime?
  53. } else {
  54. standingOnGround = true;
  55. int diff = playerBbox.Bottom - rect.Top;
  56. position.Y -= diff;
  57. }
  58. } else {
  59. playerBbox.Height += 1;
  60. if (playerBbox.Intersects(rect)) {
  61. standingOnGround = true;
  62. Debug.AddRect(rect, Color.Cyan);
  63. } else {
  64. Debug.AddRect(rect, Color.Green);
  65. }
  66. }
  67. }
  68. if (standingOnGround) {
  69. jumps = 1;
  70. ySpeed = 0;
  71. Debug.AddRect(playerBbox, Color.Red);
  72. } else {
  73. jumps = 0;
  74. Debug.AddRect(playerBbox, Color.Orange);
  75. }
  76. if (movement.X > 0) {
  77. facing = Facing.Right;
  78. } else if (movement.X < 0) {
  79. facing = Facing.Left;
  80. }
  81. if (swordSwingTime > 0) {
  82. pose = Pose.SwordSwing;
  83. } else if (jumps == 0) {
  84. pose = Pose.Jumping;
  85. } else if (movement.X != 0) {
  86. pose = Pose.Walking;
  87. } else if (input[0].Motion.Y > 0) {
  88. pose = Pose.Stretching;
  89. } else if (input[0].Motion.Y < 0) {
  90. pose = Pose.Crouching;
  91. } else {
  92. pose = Pose.Standing;
  93. }
  94. }
  95. // Returns the desired (dx, dy) for the player to move this frame.
  96. Vector2 HandleInput(GameTime time, History<Input> input) {
  97. Vector2 result = new Vector2();
  98. result.X = (int) (input[0].Motion.X * moveSpeed * time.ElapsedGameTime.TotalSeconds);
  99. if (input[0].Jump && !input[1].Jump && jumps > 0) {
  100. jumpTime = 0.3;
  101. jumps--;
  102. ySpeed = jumpSpeed;
  103. }
  104. if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
  105. swordSwingTime = 0.3;
  106. }
  107. result.Y = ySpeed * (float) time.ElapsedGameTime.TotalSeconds;
  108. ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
  109. jumpTime -= time.ElapsedGameTime.TotalSeconds;
  110. swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
  111. return result;
  112. }
  113. private int SpriteIndex(Pose pose, GameTime time) {
  114. int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
  115. if (frameNum == 3) {
  116. frameNum = 1;
  117. }
  118. switch (pose) {
  119. case Pose.Walking:
  120. return 6 + frameNum;
  121. case Pose.Stretching:
  122. return 18 + frameNum;
  123. case Pose.Jumping:
  124. if (jumpTime > 0.2) {
  125. return 15;
  126. } else if (jumpTime > 0.1) {
  127. return 16;
  128. } else {
  129. return 17;
  130. }
  131. case Pose.SwordSwing:
  132. if (swordSwingTime > 0.2) {
  133. return 30;
  134. } else if (swordSwingTime > 0.1) {
  135. return 31;
  136. } else {
  137. return 32;
  138. }
  139. case Pose.Crouching:
  140. return 25;
  141. case Pose.Standing:
  142. default:
  143. return 7;
  144. }
  145. }
  146. public void Draw(SpriteBatch spriteBatch, Camera camera, GameTime time) {
  147. int index = SpriteIndex(pose, time);
  148. Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
  149. Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
  150. SpriteEffects effect = facing == Facing.Right ?
  151. SpriteEffects.FlipHorizontally : SpriteEffects.None;
  152. Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);
  153. spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
  154. Vector2.One, effect, 0f);
  155. }
  156. }
  157. }