2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
|
|
|
class Player {
|
2020-01-29 19:05:00 +00:00
|
|
|
// The player's Facing corresponds to the x-direction that they're looking.
|
|
|
|
enum Facing {
|
|
|
|
Left = -1,
|
|
|
|
Right = 1
|
|
|
|
};
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
|
|
|
|
|
|
|
|
private const int moveSpeed = 180;
|
|
|
|
private const int jumpSpeed = -600;
|
|
|
|
private const int gravity = 2400;
|
|
|
|
|
|
|
|
private const int spriteSize = 48;
|
|
|
|
private const int spriteWidth = 7;
|
2020-01-25 02:04:30 +00:00
|
|
|
private readonly Texture2D texture;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-25 01:04:43 +00:00
|
|
|
private Point position = new Point(64, 16 * 14);
|
2020-01-18 03:41:45 +00:00
|
|
|
private int jumps = 0;
|
|
|
|
private Facing facing = Facing.Right;
|
|
|
|
private Pose pose = Pose.Jumping;
|
|
|
|
private double swordSwingTime = 0;
|
|
|
|
private double jumpTime = 0;
|
|
|
|
private float ySpeed = 0;
|
|
|
|
|
|
|
|
public Player(Texture2D texture) {
|
|
|
|
this.texture = texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Point Position { get { return position; } }
|
|
|
|
|
|
|
|
private Rectangle Bbox(Point position) {
|
|
|
|
return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:39:25 +00:00
|
|
|
private Aabb Box(Point position) {
|
|
|
|
return Box(position, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Aabb Box(Point position, int yOffset) {
|
|
|
|
return new Aabb(new Vector2(position.X, position.Y - 7 + 13 + yOffset),
|
|
|
|
new Vector2(spriteWidth, 13));
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:58:48 +00:00
|
|
|
public void Update(float modelTime, History<Input> input, Rectangle[] collisionTargets) {
|
2020-01-18 03:41:45 +00:00
|
|
|
Point oldPosition = position;
|
2020-01-24 23:36:55 +00:00
|
|
|
Vector2 movement = HandleInput(modelTime, input);
|
2020-01-18 03:41:45 +00:00
|
|
|
position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y));
|
|
|
|
|
|
|
|
Rectangle oldBbox = Bbox(oldPosition);
|
|
|
|
Rectangle playerBbox = Bbox(position);
|
|
|
|
bool standingOnGround = false;
|
|
|
|
|
2020-01-29 19:39:25 +00:00
|
|
|
// TODO: we shouldn't hardcode the tile sizes here.
|
|
|
|
Vector2 halfBoxSize = new Vector2(World.TileSize / 2, World.TileSize / 2);
|
2020-01-18 03:41:45 +00:00
|
|
|
foreach (var rect in collisionTargets) {
|
2020-01-29 19:39:25 +00:00
|
|
|
Aabb rectBox = new Aabb(
|
|
|
|
new Vector2(rect.X + World.TileSize / 2, rect.Y + World.TileSize / 2), halfBoxSize);
|
|
|
|
Aabb playerBox = Box(position);
|
2020-01-18 03:41:45 +00:00
|
|
|
playerBbox = Bbox(position);
|
|
|
|
|
|
|
|
// first we check for left-right collisions...
|
2020-01-29 19:39:25 +00:00
|
|
|
if (playerBox.Intersect(rectBox) != null) {
|
2020-01-18 03:41:45 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-01-29 19:39:25 +00:00
|
|
|
playerBox = Box(position);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
// after fixing that, we check for hitting our head or hitting the ground.
|
2020-01-29 19:39:25 +00:00
|
|
|
if (playerBox.Intersect(rectBox) != null) {
|
2020-01-18 03:41:45 +00:00
|
|
|
if (oldPosition.Y > position.Y) {
|
|
|
|
int diff = playerBbox.Top - rect.Bottom;
|
|
|
|
position.Y -= diff;
|
|
|
|
// TODO: set ySpeed = 0 here so that bonking our head actually reduces hangtime?
|
|
|
|
} else {
|
|
|
|
standingOnGround = true;
|
|
|
|
int diff = playerBbox.Bottom - rect.Top;
|
|
|
|
position.Y -= diff;
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-29 19:39:25 +00:00
|
|
|
playerBox = Box(position, 1);
|
|
|
|
if (playerBox.Intersect(rectBox) != null) {
|
2020-01-18 03:41:45 +00:00
|
|
|
standingOnGround = true;
|
|
|
|
Debug.AddRect(rect, Color.Cyan);
|
|
|
|
} else {
|
|
|
|
Debug.AddRect(rect, Color.Green);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (standingOnGround) {
|
|
|
|
jumps = 1;
|
|
|
|
ySpeed = 0;
|
|
|
|
Debug.AddRect(playerBbox, Color.Red);
|
|
|
|
} else {
|
|
|
|
jumps = 0;
|
|
|
|
Debug.AddRect(playerBbox, Color.Orange);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (movement.X > 0) {
|
|
|
|
facing = Facing.Right;
|
|
|
|
} else if (movement.X < 0) {
|
|
|
|
facing = Facing.Left;
|
|
|
|
}
|
|
|
|
if (swordSwingTime > 0) {
|
|
|
|
pose = Pose.SwordSwing;
|
|
|
|
} else if (jumps == 0) {
|
|
|
|
pose = Pose.Jumping;
|
|
|
|
} else if (movement.X != 0) {
|
|
|
|
pose = Pose.Walking;
|
|
|
|
} else if (input[0].Motion.Y > 0) {
|
|
|
|
pose = Pose.Stretching;
|
|
|
|
} else if (input[0].Motion.Y < 0) {
|
|
|
|
pose = Pose.Crouching;
|
|
|
|
} else {
|
|
|
|
pose = Pose.Standing;
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 02:09:09 +00:00
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
// Returns the desired (dx, dy) for the player to move this frame.
|
2020-01-24 23:36:55 +00:00
|
|
|
Vector2 HandleInput(float modelTime, History<Input> input) {
|
2020-01-25 02:09:09 +00:00
|
|
|
Vector2 result = new Vector2() {
|
|
|
|
X = (int) (input[0].Motion.X * moveSpeed * modelTime)
|
|
|
|
};
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
if (input[0].Jump && !input[1].Jump && jumps > 0) {
|
|
|
|
jumpTime = 0.3;
|
|
|
|
jumps--;
|
|
|
|
ySpeed = jumpSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
|
|
|
|
swordSwingTime = 0.3;
|
|
|
|
}
|
|
|
|
|
2020-01-24 23:36:55 +00:00
|
|
|
result.Y = ySpeed * modelTime;
|
|
|
|
ySpeed += gravity * modelTime;
|
|
|
|
jumpTime -= modelTime;
|
|
|
|
swordSwingTime -= modelTime;
|
2020-01-18 03:41:45 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-24 23:36:55 +00:00
|
|
|
private int SpriteIndex(Pose pose) {
|
2020-01-25 01:42:27 +00:00
|
|
|
int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4;
|
2020-01-18 03:41:45 +00:00
|
|
|
if (frameNum == 3) {
|
|
|
|
frameNum = 1;
|
|
|
|
}
|
|
|
|
switch (pose) {
|
|
|
|
case Pose.Walking:
|
|
|
|
return 6 + frameNum;
|
|
|
|
case Pose.Stretching:
|
|
|
|
return 18 + frameNum;
|
|
|
|
case Pose.Jumping:
|
|
|
|
if (jumpTime > 0.2) {
|
|
|
|
return 15;
|
|
|
|
} else if (jumpTime > 0.1) {
|
|
|
|
return 16;
|
|
|
|
} else {
|
|
|
|
return 17;
|
|
|
|
}
|
|
|
|
case Pose.SwordSwing:
|
|
|
|
if (swordSwingTime > 0.2) {
|
|
|
|
return 30;
|
|
|
|
} else if (swordSwingTime > 0.1) {
|
|
|
|
return 31;
|
|
|
|
} else {
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
case Pose.Crouching:
|
|
|
|
return 25;
|
|
|
|
case Pose.Standing:
|
|
|
|
default:
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 23:36:55 +00:00
|
|
|
public void Draw(SpriteBatch spriteBatch, Camera camera) {
|
|
|
|
int index = SpriteIndex(pose);
|
2020-01-18 03:41:45 +00:00
|
|
|
Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
|
|
|
|
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
|
|
|
|
SpriteEffects effect = facing == Facing.Right ?
|
|
|
|
SpriteEffects.FlipHorizontally : SpriteEffects.None;
|
|
|
|
Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);
|
|
|
|
spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
|
|
|
|
Vector2.One, effect, 0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|