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.

174 lines
5.5 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. } else {
  53. standingOnGround = true;
  54. int diff = playerBbox.Bottom - rect.Top;
  55. position.Y -= diff;
  56. }
  57. } else {
  58. playerBbox.Height += 1;
  59. if (playerBbox.Intersects(rect)) {
  60. standingOnGround = true;
  61. Debug.AddRect(rect, Color.Cyan);
  62. } else {
  63. Debug.AddRect(rect, Color.Green);
  64. }
  65. }
  66. }
  67. if (standingOnGround) {
  68. jumps = 1;
  69. ySpeed = 0;
  70. Debug.AddRect(playerBbox, Color.Red);
  71. } else {
  72. jumps = 0;
  73. Debug.AddRect(playerBbox, Color.Orange);
  74. }
  75. if (movement.X > 0) {
  76. facing = Facing.Right;
  77. } else if (movement.X < 0) {
  78. facing = Facing.Left;
  79. }
  80. if (swordSwingTime > 0) {
  81. pose = Pose.SwordSwing;
  82. } else if (jumps == 0) {
  83. pose = Pose.Jumping;
  84. } else if (movement.X != 0) {
  85. pose = Pose.Walking;
  86. } else if (input[0].Motion.Y > 0) {
  87. pose = Pose.Stretching;
  88. } else if (input[0].Motion.Y < 0) {
  89. pose = Pose.Crouching;
  90. } else {
  91. pose = Pose.Standing;
  92. }
  93. }
  94. // Returns the desired (dx, dy) for the player to move this frame.
  95. Vector2 HandleInput(GameTime time, History<Input> input) {
  96. Vector2 result = new Vector2();
  97. result.X = (int) (input[0].Motion.X * moveSpeed * time.ElapsedGameTime.TotalSeconds);
  98. if (input[0].Jump && !input[1].Jump && jumps > 0) {
  99. jumpTime = 0.3;
  100. jumps--;
  101. ySpeed = jumpSpeed;
  102. }
  103. if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
  104. swordSwingTime = 0.3;
  105. }
  106. result.Y = ySpeed * (float) time.ElapsedGameTime.TotalSeconds;
  107. ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
  108. jumpTime -= time.ElapsedGameTime.TotalSeconds;
  109. swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
  110. return result;
  111. }
  112. private int SpriteIndex(Pose pose, GameTime time) {
  113. int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
  114. if (frameNum == 3) {
  115. frameNum = 1;
  116. }
  117. switch (pose) {
  118. case Pose.Walking:
  119. return 6 + frameNum;
  120. case Pose.Stretching:
  121. return 18 + frameNum;
  122. case Pose.Jumping:
  123. if (jumpTime > 0.2) {
  124. return 15;
  125. } else if (jumpTime > 0.1) {
  126. return 16;
  127. } else {
  128. return 17;
  129. }
  130. case Pose.SwordSwing:
  131. if (swordSwingTime > 0.2) {
  132. return 30;
  133. } else if (swordSwingTime > 0.1) {
  134. return 31;
  135. } else {
  136. return 32;
  137. }
  138. case Pose.Crouching:
  139. return 25;
  140. case Pose.Standing:
  141. default:
  142. return 7;
  143. }
  144. }
  145. public void Draw(SpriteBatch spriteBatch, Camera camera, GameTime time) {
  146. int index = SpriteIndex(pose, time);
  147. Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
  148. Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
  149. SpriteEffects effect = facing == Facing.Right ?
  150. SpriteEffects.FlipHorizontally : SpriteEffects.None;
  151. Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);
  152. spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
  153. Vector2.One, effect, 0f);
  154. }
  155. }
  156. }