whee, now you can jump!

GitOrigin-RevId: e8c408b0ffcbb3f2ddfa2346fc9823fea86dfe00
This commit is contained in:
Colin McMillen 2019-12-08 10:44:43 -05:00
parent d9ed88a9ef
commit 44874f781d

View File

@ -6,27 +6,41 @@ using System;
namespace Jumpy { namespace Jumpy {
class Player { class Player {
enum Facing { Left, Right }; 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 spriteSize = 144;
private const int spriteWidth = 20; private const int spriteWidth = 20;
private const int moveSpeed = 600; private const int moveSpeed = 600;
private const int jumpSpeed = 1800;
private const int gravity = 6000;
private Texture2D texture; private Texture2D texture;
// TODO: stop assuming 1920x1080. // 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 Facing facing = Facing.Right;
private Pose pose = Pose.Standing; private Pose pose = Pose.Standing;
private AirState airState = AirState.Ground;
private double swordSwingTime = 0; private double swordSwingTime = 0;
private double jumpTime = 0;
private double ySpeed = 0;
public Player(Texture2D texture) { public Player(Texture2D texture) {
this.texture = texture; this.texture = texture;
} }
public void Update(GameTime gameTime, GamePadState gamePad) { 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) { if (gamePad.Buttons.X == ButtonState.Pressed && swordSwingTime <= 0) {
swordSwingTime = 0.3;
pose = Pose.SwordSwing; pose = Pose.SwordSwing;
swordSwingTime = 0.3;
return; return;
} }
Vector2 leftStick = gamePad.ThumbSticks.Left; Vector2 leftStick = gamePad.ThumbSticks.Left;
@ -46,11 +60,27 @@ namespace Jumpy {
pose = Pose.Standing; pose = Pose.Standing;
} }
if (jumpTime > 0) {
jumpTime -= gameTime.ElapsedGameTime.TotalSeconds;
}
if (swordSwingTime > 0) { if (swordSwingTime > 0) {
swordSwingTime -= gameTime.ElapsedGameTime.TotalSeconds; swordSwingTime -= gameTime.ElapsedGameTime.TotalSeconds;
pose = Pose.SwordSwing; 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); 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); return new Point(spriteSize * 7, spriteSize * 2);
case Pose.Stretching: case Pose.Stretching:
return new Point(spriteSize * 1, spriteSize * 2); 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: case Pose.SwordSwing:
if (swordSwingTime > 0.2) { if (swordSwingTime > 0.2) {
return new Point(spriteSize * 3, 0); return new Point(spriteSize * 3, 0);