2019-12-08 14:38:26 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Jumpy {
|
|
|
|
|
class Player {
|
|
|
|
|
enum Facing { Left, Right };
|
2019-12-08 15:44:43 +00:00
|
|
|
|
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
|
|
|
|
|
enum AirState { Jumping, Ground };
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
2019-12-09 01:04:22 +00:00
|
|
|
|
private Texture2D texture;
|
2019-12-08 23:51:23 +00:00
|
|
|
|
private const int spriteSize = 48;
|
2019-12-09 01:04:22 +00:00
|
|
|
|
private const int spriteWidth = 7;
|
2019-12-11 21:57:30 +00:00
|
|
|
|
private const int bottomPadding = 1;
|
2019-12-08 23:51:23 +00:00
|
|
|
|
private const int moveSpeed = 200;
|
|
|
|
|
private const int jumpSpeed = 600;
|
|
|
|
|
private const int gravity = 2000;
|
2019-12-10 21:49:27 +00:00
|
|
|
|
private const int groundLevel = Camera.Height - spriteSize / 2 + bottomPadding - 32;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
2019-12-09 01:04:22 +00:00
|
|
|
|
private Point position = new Point(Camera.Width / 2, groundLevel);
|
2019-12-08 14:38:26 +00:00
|
|
|
|
private Facing facing = Facing.Right;
|
|
|
|
|
private Pose pose = Pose.Standing;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
private AirState airState = AirState.Ground;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
private double swordSwingTime = 0;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
private double jumpTime = 0;
|
|
|
|
|
private double ySpeed = 0;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
|
|
|
|
public Player(Texture2D texture) {
|
|
|
|
|
this.texture = texture;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 19:28:50 +00:00
|
|
|
|
// TODO: refactor input to have a virtual "which directions & buttons were being pressed"
|
|
|
|
|
// instead of complicated if-statements in this function.
|
2019-12-10 16:12:06 +00:00
|
|
|
|
public void Update(GameTime time, History<GamePadState> gamePad) {
|
|
|
|
|
if (gamePad[0].IsButtonDown(Buttons.A) && airState == AirState.Ground) {
|
2019-12-08 15:44:43 +00:00
|
|
|
|
pose = Pose.Jumping;
|
|
|
|
|
airState = AirState.Jumping;
|
|
|
|
|
jumpTime = 0.5;
|
|
|
|
|
ySpeed = -jumpSpeed;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-10 16:22:42 +00:00
|
|
|
|
|
|
|
|
|
if (gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X)
|
|
|
|
|
&& swordSwingTime <= 0) {
|
2019-12-08 14:48:29 +00:00
|
|
|
|
pose = Pose.SwordSwing;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
swordSwingTime = 0.3;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-10 16:22:42 +00:00
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
Vector2 leftStick = gamePad[0].ThumbSticks.Left;
|
|
|
|
|
if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5) {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
facing = Facing.Left;
|
|
|
|
|
pose = Pose.Walking;
|
2019-12-10 16:12:56 +00:00
|
|
|
|
position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
|
2019-12-10 16:12:06 +00:00
|
|
|
|
} else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5) {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
facing = Facing.Right;
|
|
|
|
|
pose = Pose.Walking;
|
2019-12-10 16:12:56 +00:00
|
|
|
|
position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
|
2019-12-10 16:12:06 +00:00
|
|
|
|
} else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5) {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
pose = Pose.Crouching;
|
2019-12-10 16:12:06 +00:00
|
|
|
|
} else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5) {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
pose = Pose.Stretching;
|
|
|
|
|
} else {
|
|
|
|
|
pose = Pose.Standing;
|
|
|
|
|
}
|
2019-12-08 14:48:29 +00:00
|
|
|
|
|
2019-12-08 15:44:43 +00:00
|
|
|
|
if (jumpTime > 0) {
|
2019-12-09 04:02:22 +00:00
|
|
|
|
jumpTime -= time.ElapsedGameTime.TotalSeconds;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
}
|
2019-12-08 14:48:29 +00:00
|
|
|
|
if (swordSwingTime > 0) {
|
2019-12-09 04:02:22 +00:00
|
|
|
|
swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
pose = Pose.SwordSwing;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 15:44:43 +00:00
|
|
|
|
if (airState == AirState.Jumping) {
|
2019-12-09 04:02:22 +00:00
|
|
|
|
position.Y += (int) (ySpeed * time.ElapsedGameTime.TotalSeconds);
|
|
|
|
|
ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
if (position.Y > groundLevel) {
|
|
|
|
|
position.Y = groundLevel;
|
|
|
|
|
ySpeed = 0;
|
|
|
|
|
airState = AirState.Ground;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (airState == AirState.Jumping && pose != Pose.SwordSwing) {
|
|
|
|
|
pose = Pose.Jumping;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 01:04:22 +00:00
|
|
|
|
position.X = Math.Min(Math.Max(position.X, 0 + spriteWidth), Camera.Width - spriteWidth);
|
2019-12-08 14:38:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Point spritePosition(Pose pose, GameTime time) {
|
2019-12-08 19:45:13 +00:00
|
|
|
|
int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
|
|
|
|
|
if (frameNum == 3) {
|
|
|
|
|
frameNum = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 14:38:26 +00:00
|
|
|
|
switch (pose) {
|
|
|
|
|
case Pose.Walking:
|
|
|
|
|
return new Point(spriteSize * frameNum + spriteSize * 6, 0);
|
|
|
|
|
case Pose.Crouching:
|
|
|
|
|
return new Point(spriteSize * 7, spriteSize * 2);
|
|
|
|
|
case Pose.Stretching:
|
2019-12-08 19:45:13 +00:00
|
|
|
|
return new Point(spriteSize * frameNum, spriteSize * 2);
|
2019-12-08 15:44:43 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2019-12-08 14:48:29 +00:00
|
|
|
|
case Pose.SwordSwing:
|
|
|
|
|
if (swordSwingTime > 0.2) {
|
2019-12-08 19:26:43 +00:00
|
|
|
|
return new Point(spriteSize * 3, spriteSize * 0);
|
2019-12-08 14:48:29 +00:00
|
|
|
|
} else if (swordSwingTime > 0.1) {
|
2019-12-08 19:26:43 +00:00
|
|
|
|
return new Point(spriteSize * 4, spriteSize * 0);
|
2019-12-08 14:48:29 +00:00
|
|
|
|
} else {
|
2019-12-08 19:26:43 +00:00
|
|
|
|
return new Point(spriteSize * 5, spriteSize * 0);
|
2019-12-08 14:48:29 +00:00
|
|
|
|
}
|
2019-12-08 14:38:26 +00:00
|
|
|
|
case Pose.Standing:
|
|
|
|
|
default:
|
|
|
|
|
return new Point(spriteSize * 7, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Draw(GameTime time, SpriteBatch spriteBatch) {
|
|
|
|
|
Point source = spritePosition(pose, time);
|
|
|
|
|
Rectangle textureSource = new Rectangle(source.X, source.Y, spriteSize, spriteSize);
|
|
|
|
|
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
|
|
|
|
|
SpriteEffects effect = facing == Facing.Right ?
|
|
|
|
|
SpriteEffects.FlipHorizontally : SpriteEffects.None;
|
2019-12-08 23:51:23 +00:00
|
|
|
|
Vector2 drawPos = new Vector2(position.X, position.Y);
|
|
|
|
|
spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
|
2019-12-08 14:38:26 +00:00
|
|
|
|
Vector2.One, effect, 0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|