2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-01-29 20:43:00 +00:00
|
|
|
using System;
|
2020-01-18 03:41:45 +00:00
|
|
|
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;
|
|
|
|
|
2020-01-29 21:45:13 +00:00
|
|
|
// Details of the sprite image.
|
2020-01-18 03:41:45 +00:00
|
|
|
private const int spriteSize = 48;
|
2020-01-29 21:45:13 +00:00
|
|
|
private const int spriteCenterYOffset = 5;
|
2020-01-25 02:04:30 +00:00
|
|
|
private readonly Texture2D texture;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 21:45:13 +00:00
|
|
|
// Details of the actual Player model.
|
|
|
|
|
|
|
|
// Position is tracked at the Player's center. The Player's bounding box is a rectangle
|
|
|
|
// centered at that point and extending out by halfSize.X and halfSize.Y.
|
|
|
|
private Point position = new Point(64, 16 * 13);
|
|
|
|
private Vector2 halfSize = new Vector2(7, 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; } }
|
|
|
|
|
2020-01-29 21:11:28 +00:00
|
|
|
public void Update(float modelTime, History<Input> input, Aabb[] collisionTargets) {
|
|
|
|
Aabb BoxOffset(Point position, int yOffset) {
|
2020-01-29 21:45:13 +00:00
|
|
|
return new Aabb(new Vector2(position.X, position.Y + yOffset), halfSize);
|
2020-01-29 21:11:28 +00:00
|
|
|
}
|
2020-01-29 19:39:25 +00:00
|
|
|
|
2020-01-29 21:11:28 +00:00
|
|
|
Aabb Box(Point position) {
|
|
|
|
return BoxOffset(position, 0);
|
|
|
|
}
|
2020-01-29 19:39:25 +00:00
|
|
|
|
2020-01-24 23:36:55 +00:00
|
|
|
Vector2 movement = HandleInput(modelTime, input);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 20:43:00 +00:00
|
|
|
// Broad test: remove all collision targets nowhere near the player.
|
2020-01-29 21:11:28 +00:00
|
|
|
var candidates = new List<Aabb>();
|
2020-01-29 22:43:32 +00:00
|
|
|
// Expand the box in the direction of movement. The center is the midpoint of the line
|
|
|
|
// between the player's current position and their desired movement. The width increases by
|
|
|
|
// the magnitude of the movement in each direction. We add 1 to each dimension just to be
|
|
|
|
// sure (the only downside is a small number of false-positive AABBs, which should be
|
|
|
|
// discarded by later tests anyhow.)
|
2020-01-29 20:43:00 +00:00
|
|
|
Aabb largeBox = new Aabb(
|
2020-01-29 22:43:32 +00:00
|
|
|
new Vector2(position.X + movement.X / 2, position.Y + movement.Y / 2),
|
|
|
|
new Vector2(halfSize.X + Math.Abs(movement.X) + 1, halfSize.Y + Math.Abs(movement.Y) + 1));
|
2020-01-29 21:11:28 +00:00
|
|
|
foreach (var box in collisionTargets) {
|
2020-01-29 20:43:00 +00:00
|
|
|
if (box.Intersect(largeBox) != null) {
|
|
|
|
Debug.AddRect(box, Color.Green);
|
2020-01-29 21:11:28 +00:00
|
|
|
candidates.Add(box);
|
2020-01-29 20:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 20:43:00 +00:00
|
|
|
Point[] movePoints = Line.Rasterize(0, 0, (int) movement.X, (int) movement.Y);
|
|
|
|
for (int i = 1; i < movePoints.Length; i++) {
|
|
|
|
int dx = movePoints[i].X - movePoints[i - 1].X;
|
|
|
|
int dy = movePoints[i].Y - movePoints[i - 1].Y;
|
|
|
|
if (dy != 0) {
|
|
|
|
Point newPosition = new Point(position.X, position.Y + dy);
|
|
|
|
Aabb player = Box(newPosition);
|
|
|
|
bool reject = false;
|
2020-01-29 21:11:28 +00:00
|
|
|
foreach (var box in candidates) {
|
2020-01-29 20:43:00 +00:00
|
|
|
if (box.Intersect(player) != null) {
|
|
|
|
reject = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
if (!reject) {
|
|
|
|
position = newPosition;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
if (dx != 0) {
|
|
|
|
Point newPosition = new Point(position.X + dx, position.Y);
|
|
|
|
Aabb player = Box(newPosition);
|
|
|
|
bool reject = false;
|
2020-01-29 21:11:28 +00:00
|
|
|
foreach (var box in candidates) {
|
2020-01-29 20:43:00 +00:00
|
|
|
if (box.Intersect(player) != null) {
|
|
|
|
reject = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
if (!reject) {
|
|
|
|
position = newPosition;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
|
|
|
|
bool standingOnGround = false;
|
2020-01-29 21:11:28 +00:00
|
|
|
Aabb groundIntersect = BoxOffset(position, 1);
|
|
|
|
foreach (var box in candidates) {
|
2020-01-29 20:43:00 +00:00
|
|
|
if (groundIntersect.Intersect(box) != null) {
|
|
|
|
standingOnGround = true;
|
2020-01-29 21:11:28 +00:00
|
|
|
break;
|
2020-01-29 20:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
if (standingOnGround) {
|
|
|
|
jumps = 1;
|
2020-01-29 20:43:00 +00:00
|
|
|
ySpeed = -0.0001f;
|
2020-01-29 21:11:28 +00:00
|
|
|
Debug.AddRect(Box(position), Color.Cyan);
|
2020-01-18 03:41:45 +00:00
|
|
|
} else {
|
|
|
|
jumps = 0;
|
2020-01-29 21:11:28 +00:00
|
|
|
Debug.AddRect(Box(position), Color.Orange);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-01-29 21:45:13 +00:00
|
|
|
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2 + spriteCenterYOffset);
|
2020-01-18 03:41:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|