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.

155 lines
5.5 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 Jumpy {
  7. class Player {
  8. enum Facing { Left, Right };
  9. enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
  10. enum AirState { Jumping, Ground };
  11. private Texture2D texture;
  12. private const int spriteSize = 48;
  13. private const int spriteWidth = 7;
  14. private const int bottomPadding = 1;
  15. private const int moveSpeed = 200;
  16. private const int jumpSpeed = 600;
  17. private const int gravity = 2000;
  18. private const int groundLevel = Camera.Height - spriteSize / 2 + bottomPadding - 32;
  19. private Point position = new Point(Camera.Width / 2, groundLevel);
  20. private Facing facing = Facing.Right;
  21. private Pose pose = Pose.Standing;
  22. private AirState airState = AirState.Ground;
  23. private double swordSwingTime = 0;
  24. private double jumpTime = 0;
  25. private double ySpeed = 0;
  26. public Player(Texture2D texture) {
  27. this.texture = texture;
  28. }
  29. // TODO: refactor input to have a virtual "which directions & buttons were being pressed"
  30. // instead of complicated if-statements in this function.
  31. public void Update(
  32. GameTime time, History<GamePadState> gamePad, List<Rectangle> collisionTargets) {
  33. UpdateFromGamePad(time, gamePad);
  34. Rectangle playerBbox =
  35. new Rectangle(position.X - spriteWidth, position.Y - 9, spriteWidth * 2, 28);
  36. Debug.AddRect(playerBbox, Color.Red);
  37. foreach (var rect in collisionTargets) {
  38. if (playerBbox.Intersects(rect)) {
  39. Debug.AddRect(rect, Color.Yellow);
  40. } else {
  41. Debug.AddRect(rect, Color.Green);
  42. }
  43. }
  44. }
  45. void UpdateFromGamePad(GameTime time, History<GamePadState> gamePad) {
  46. if (gamePad[0].IsButtonDown(Buttons.A) && airState == AirState.Ground) {
  47. pose = Pose.Jumping;
  48. airState = AirState.Jumping;
  49. jumpTime = 0.5;
  50. ySpeed = -jumpSpeed;
  51. return;
  52. }
  53. if (gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X)
  54. && swordSwingTime <= 0) {
  55. pose = Pose.SwordSwing;
  56. swordSwingTime = 0.3;
  57. return;
  58. }
  59. Vector2 leftStick = gamePad[0].ThumbSticks.Left;
  60. if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5) {
  61. facing = Facing.Left;
  62. pose = Pose.Walking;
  63. position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
  64. } else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5) {
  65. facing = Facing.Right;
  66. pose = Pose.Walking;
  67. position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
  68. } else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5) {
  69. pose = Pose.Crouching;
  70. } else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5) {
  71. pose = Pose.Stretching;
  72. } else {
  73. pose = Pose.Standing;
  74. }
  75. if (jumpTime > 0) {
  76. jumpTime -= time.ElapsedGameTime.TotalSeconds;
  77. }
  78. if (swordSwingTime > 0) {
  79. swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
  80. pose = Pose.SwordSwing;
  81. }
  82. if (airState == AirState.Jumping) {
  83. position.Y += (int) (ySpeed * time.ElapsedGameTime.TotalSeconds);
  84. ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
  85. if (position.Y > groundLevel) {
  86. position.Y = groundLevel;
  87. ySpeed = 0;
  88. airState = AirState.Ground;
  89. }
  90. }
  91. if (airState == AirState.Jumping && pose != Pose.SwordSwing) {
  92. pose = Pose.Jumping;
  93. }
  94. position.X = Math.Min(Math.Max(position.X, 0 + spriteWidth), Camera.Width - spriteWidth);
  95. }
  96. private Point spritePosition(Pose pose, GameTime time) {
  97. int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
  98. if (frameNum == 3) {
  99. frameNum = 1;
  100. }
  101. switch (pose) {
  102. case Pose.Walking:
  103. return new Point(spriteSize * frameNum + spriteSize * 6, 0);
  104. case Pose.Crouching:
  105. return new Point(spriteSize * 7, spriteSize * 2);
  106. case Pose.Stretching:
  107. return new Point(spriteSize * frameNum, spriteSize * 2);
  108. case Pose.Jumping:
  109. if (jumpTime > 0.25) {
  110. return new Point(spriteSize * 6, spriteSize);
  111. } else if (jumpTime > 0) {
  112. return new Point(spriteSize * 7, spriteSize);
  113. } else {
  114. return new Point(spriteSize * 8, spriteSize);
  115. }
  116. case Pose.SwordSwing:
  117. if (swordSwingTime > 0.2) {
  118. return new Point(spriteSize * 3, spriteSize * 0);
  119. } else if (swordSwingTime > 0.1) {
  120. return new Point(spriteSize * 4, spriteSize * 0);
  121. } else {
  122. return new Point(spriteSize * 5, spriteSize * 0);
  123. }
  124. case Pose.Standing:
  125. default:
  126. return new Point(spriteSize * 7, 0);
  127. }
  128. }
  129. public void Draw(GameTime time, SpriteBatch spriteBatch) {
  130. Point source = spritePosition(pose, time);
  131. Rectangle textureSource = new Rectangle(source.X, source.Y, spriteSize, spriteSize);
  132. Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
  133. SpriteEffects effect = facing == Facing.Right ?
  134. SpriteEffects.FlipHorizontally : SpriteEffects.None;
  135. Vector2 drawPos = new Vector2(position.X, position.Y);
  136. spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
  137. Vector2.One, effect, 0f);
  138. }
  139. }
  140. }