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.

138 lines
5.0 KiB

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