2019-12-08 14:38:26 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
2019-12-16 23:23:25 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
using System;
|
2019-12-11 22:57:30 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
|
|
|
|
namespace Jumpy {
|
|
|
|
|
class Player {
|
|
|
|
|
enum Facing { Left, Right };
|
2019-12-08 15:44:43 +00:00
|
|
|
|
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
|
2019-12-12 18:33:52 +00:00
|
|
|
|
enum AirState { Jumping, Ground, Falling };
|
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-12 20:20:08 +00:00
|
|
|
|
private const int moveSpeed = 180;
|
|
|
|
|
private const int jumpSpeed = 600;
|
|
|
|
|
private const int gravity = 2400;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
2019-12-12 18:33:52 +00:00
|
|
|
|
private Point position = new Point(Camera.Width / 2, 10);
|
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-16 23:23:25 +00:00
|
|
|
|
private Rectangle Bbox(Point position) {
|
|
|
|
|
return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 22:57:30 +00:00
|
|
|
|
public void Update(
|
|
|
|
|
GameTime time, History<GamePadState> gamePad, List<Rectangle> collisionTargets) {
|
2019-12-12 18:33:52 +00:00
|
|
|
|
Point oldPosition = position;
|
|
|
|
|
AirState oldAirState = airState;
|
2019-12-11 22:57:30 +00:00
|
|
|
|
UpdateFromGamePad(time, gamePad);
|
|
|
|
|
|
2019-12-16 23:23:25 +00:00
|
|
|
|
Rectangle oldBbox = Bbox(oldPosition);
|
|
|
|
|
Rectangle playerBbox = Bbox(position);
|
2019-12-12 18:33:52 +00:00
|
|
|
|
bool standingOnGround = false;
|
2019-12-16 04:01:19 +00:00
|
|
|
|
// TODO: implement https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
|
|
|
|
|
// e.g. http://members.chello.at/~easyfilter/bresenham.html
|
2019-12-11 22:57:30 +00:00
|
|
|
|
foreach (var rect in collisionTargets) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
playerBbox = Bbox(position);
|
2019-12-12 18:33:52 +00:00
|
|
|
|
|
2019-12-16 02:26:39 +00:00
|
|
|
|
// first we check for left-right collisions...
|
|
|
|
|
if (playerBbox.Intersects(rect)) {
|
|
|
|
|
if (oldBbox.Right <= rect.Left && playerBbox.Right > rect.Left) {
|
|
|
|
|
position.X = rect.Left - spriteWidth;
|
|
|
|
|
}
|
|
|
|
|
if (oldBbox.Left >= rect.Right && playerBbox.Left < rect.Right) {
|
|
|
|
|
position.X = rect.Right + spriteWidth;
|
|
|
|
|
}
|
2019-12-16 23:23:25 +00:00
|
|
|
|
playerBbox = Bbox(position);
|
2019-12-16 02:26:39 +00:00
|
|
|
|
}
|
|
|
|
|
// after fixing that, we check for hitting our head or hitting the ground.
|
2019-12-11 22:57:30 +00:00
|
|
|
|
if (playerBbox.Intersects(rect)) {
|
2019-12-12 18:33:52 +00:00
|
|
|
|
if (oldPosition.Y > position.Y) {
|
|
|
|
|
int diff = playerBbox.Top - rect.Bottom;
|
|
|
|
|
position.Y -= diff;
|
|
|
|
|
} else {
|
|
|
|
|
airState = AirState.Ground;
|
|
|
|
|
int diff = playerBbox.Bottom - rect.Top;
|
|
|
|
|
position.Y -= diff;
|
|
|
|
|
}
|
2019-12-11 22:57:30 +00:00
|
|
|
|
} else {
|
2019-12-12 18:33:52 +00:00
|
|
|
|
playerBbox.Height += 1;
|
|
|
|
|
if (playerBbox.Intersects(rect)) {
|
|
|
|
|
standingOnGround = true;
|
|
|
|
|
Debug.AddRect(rect, Color.Cyan);
|
|
|
|
|
} else {
|
|
|
|
|
Debug.AddRect(rect, Color.Green);
|
|
|
|
|
}
|
2019-12-11 22:57:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-12 18:33:52 +00:00
|
|
|
|
if (oldAirState != AirState.Ground && standingOnGround) {
|
|
|
|
|
airState = AirState.Ground;
|
|
|
|
|
ySpeed = 0.0;
|
|
|
|
|
}
|
|
|
|
|
if (airState == AirState.Ground && !standingOnGround) {
|
|
|
|
|
airState = AirState.Falling;
|
|
|
|
|
ySpeed = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (airState == AirState.Ground) {
|
|
|
|
|
Debug.AddRect(playerBbox, Color.Red);
|
|
|
|
|
} else if (airState == AirState.Jumping) {
|
|
|
|
|
Debug.AddRect(playerBbox, Color.Orange);
|
|
|
|
|
} else {
|
|
|
|
|
Debug.AddRect(playerBbox, Color.Yellow);
|
|
|
|
|
}
|
2019-12-11 22:57:30 +00:00
|
|
|
|
}
|
2019-12-16 23:23:25 +00:00
|
|
|
|
|
2019-12-12 19:00:07 +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-16 23:23:25 +00:00
|
|
|
|
// TODO: refactor to use a state-machine.
|
2019-12-11 22:57:30 +00:00
|
|
|
|
void UpdateFromGamePad(GameTime time, History<GamePadState> gamePad) {
|
2019-12-12 20:22:24 +00:00
|
|
|
|
if (gamePad[0].IsButtonDown(Buttons.A) && gamePad[1].IsButtonUp(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-12 18:33:52 +00:00
|
|
|
|
if (airState == AirState.Jumping || airState == AirState.Falling) {
|
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 (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
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 23:23:25 +00:00
|
|
|
|
private int 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:
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 6 + frameNum;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
case Pose.Stretching:
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 18 + frameNum;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
case Pose.Jumping:
|
|
|
|
|
if (jumpTime > 0.25) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 15;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
} else if (jumpTime > 0) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 16;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
} else {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 17;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
}
|
2019-12-08 14:48:29 +00:00
|
|
|
|
case Pose.SwordSwing:
|
|
|
|
|
if (swordSwingTime > 0.2) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 30;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
} else if (swordSwingTime > 0.1) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 31;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
} else {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 32;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
}
|
2019-12-16 23:23:25 +00:00
|
|
|
|
case Pose.Crouching:
|
|
|
|
|
return 25;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
case Pose.Standing:
|
|
|
|
|
default:
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 7;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Draw(GameTime time, SpriteBatch spriteBatch) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
// TODO: don't create so many "new" things that could be cached / precomputed.
|
|
|
|
|
int index = spritePosition(pose, time);
|
|
|
|
|
Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
|
2019-12-08 14:38:26 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|