diff --git a/Jumpy.Shared/Player.cs b/Jumpy.Shared/Player.cs index 97d9196..b6cb801 100644 --- a/Jumpy.Shared/Player.cs +++ b/Jumpy.Shared/Player.cs @@ -6,27 +6,41 @@ using System; namespace Jumpy { class Player { enum Facing { Left, Right }; - enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing }; + enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping }; + enum AirState { Jumping, Ground }; private const int spriteSize = 144; private const int spriteWidth = 20; private const int moveSpeed = 600; + private const int jumpSpeed = 1800; + private const int gravity = 6000; private Texture2D texture; // TODO: stop assuming 1920x1080. - private Vector2 position = new Vector2(200, 1080 - spriteSize / 2); + private const int groundLevel = 1080 - spriteSize / 2; + private Vector2 position = new Vector2(200, groundLevel); private Facing facing = Facing.Right; private Pose pose = Pose.Standing; + private AirState airState = AirState.Ground; private double swordSwingTime = 0; + private double jumpTime = 0; + private double ySpeed = 0; public Player(Texture2D texture) { this.texture = texture; } public void Update(GameTime gameTime, GamePadState gamePad) { + if (gamePad.Buttons.A == ButtonState.Pressed && airState == AirState.Ground) { + pose = Pose.Jumping; + airState = AirState.Jumping; + jumpTime = 0.5; + ySpeed = -jumpSpeed; + return; + } if (gamePad.Buttons.X == ButtonState.Pressed && swordSwingTime <= 0) { - swordSwingTime = 0.3; pose = Pose.SwordSwing; + swordSwingTime = 0.3; return; } Vector2 leftStick = gamePad.ThumbSticks.Left; @@ -46,11 +60,27 @@ namespace Jumpy { pose = Pose.Standing; } + if (jumpTime > 0) { + jumpTime -= gameTime.ElapsedGameTime.TotalSeconds; + } if (swordSwingTime > 0) { swordSwingTime -= gameTime.ElapsedGameTime.TotalSeconds; pose = Pose.SwordSwing; } + if (airState == AirState.Jumping) { + position.Y += (float)(ySpeed * gameTime.ElapsedGameTime.TotalSeconds); + ySpeed += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds; + if (position.Y > groundLevel) { + position.Y = groundLevel; + ySpeed = 0; + airState = AirState.Ground; + } + } + if (airState == AirState.Jumping && pose != Pose.SwordSwing) { + pose = Pose.Jumping; + } + position.X = Math.Min(Math.Max(position.X, 0 + spriteWidth), 1920 - spriteWidth); } @@ -66,6 +96,14 @@ namespace Jumpy { return new Point(spriteSize * 7, spriteSize * 2); case Pose.Stretching: return new Point(spriteSize * 1, spriteSize * 2); + case Pose.Jumping: + if (jumpTime > 0.25) { + return new Point(spriteSize * 6, spriteSize); + } else if (jumpTime > 0) { + return new Point(spriteSize * 7, spriteSize); + } else { + return new Point(spriteSize * 8, spriteSize); + } case Pose.SwordSwing: if (swordSwingTime > 0.2) { return new Point(spriteSize * 3, 0);