sneak/Shared/Player.cs

305 lines
10 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace SemiColinGames {
class Player {
// The player's Facing corresponds to the x-direction that they're looking.
enum Facing {
Left = -1,
Right = 1
};
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
private const int moveSpeed = 180;
private const int jumpSpeed = -600;
private const int gravity = 2400;
// Details of the sprite image.
// player_1x is 48 x 48, yOffset=5, halfSize=(7, 14)
// Ninja_Female is 96 x 64, yOffset=1, halfSize=(11, 24)
private const int spriteWidth = 96;
private const int spriteHeight = 64;
private const int spriteCenterYOffset = 1;
private readonly Texture2D texture;
// 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(11, 24);
private Vector2 eyeOffsetStanding = new Vector2(7, -14);
private Vector2 eyeOffsetWalking = new Vector2(15, -7);
private int jumps = 0;
private Facing facing = Facing.Right;
private Pose pose = Pose.Jumping;
private double swordSwingTime = 0;
private int swordSwingNum = 0;
private const int swordSwingMax = 6;
private float ySpeed = 0;
public Player(Texture2D texture) {
this.texture = texture;
}
public Point Position { get { return position; } }
public void Update(float modelTime, History<Input> input, AABB[] collisionTargets) {
AABB BoxOffset(Point position, int yOffset) {
return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize);
}
AABB Box(Point position) {
return BoxOffset(position, 0);
}
Vector2 movement = HandleInput(modelTime, input);
// Broad test: remove all collision targets nowhere near the player.
// TODO: don't allocate a list here.
var candidates = new List<AABB>();
// 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.)
AABB largeBox = new AABB(
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));
foreach (var box in collisionTargets) {
if (box.Intersect(largeBox) != null) {
// Debug.AddRect(box, Color.Green);
candidates.Add(box);
}
}
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;
foreach (var box in candidates) {
if (box.Intersect(player) != null) {
reject = true;
break;
}
}
if (!reject) {
position = newPosition;
}
}
if (dx != 0) {
Point newPosition = new Point(position.X + dx, position.Y);
AABB player = Box(newPosition);
bool reject = false;
foreach (var box in candidates) {
if (box.Intersect(player) != null) {
reject = true;
break;
}
}
if (!reject) {
position = newPosition;
}
}
}
bool standingOnGround = false;
AABB groundIntersect = BoxOffset(position, 1);
foreach (var box in candidates) {
if (groundIntersect.Intersect(box) != null) {
standingOnGround = true;
break;
}
}
if (standingOnGround) {
jumps = 1;
ySpeed = -0.0001f;
// Debug.AddRect(Box(position), Color.Cyan);
} else {
jumps = 0;
// Debug.AddRect(Box(position), 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;
}
DrawSightLines(collisionTargets);
}
Vector2 Rotate(Vector2 point, float angle) {
float cos = FMath.Cos(angle);
float sin = FMath.Sin(angle);
return new Vector2(
point.X * cos - point.Y * sin,
point.Y * cos + point.X * sin);
}
bool PointInCone(
float visionRangeSq, float fovCos, Vector2 eyePos, Vector2 direction, Vector2 test) {
Vector2 delta = Vector2.Subtract(test, eyePos);
if (delta.LengthSquared() > visionRangeSq) {
return false;
}
float dot = Vector2.Dot(Vector2.Normalize(direction), Vector2.Normalize(delta));
return dot > fovCos;
}
void DrawSightLines(AABB[] collisionTargets) {
float fov = FMath.DegToRad(45);
float fovCos = FMath.Cos(fov);
Color color = Color.LightYellow;
Vector2 eyeOffset = pose == Pose.Walking ? eyeOffsetWalking : eyeOffsetStanding;
Vector2 eyePos = Vector2.Add(
Position.ToVector2(), new Vector2(eyeOffset.X * (int) facing, eyeOffset.Y));
float visionRange = 150;
float visionRangeSq = visionRange * visionRange;
Vector2 ray = new Vector2(visionRange * (int) facing, 0);
if (pose == Pose.Stretching) {
ray = Rotate(ray, FMath.DegToRad(-30));
}
if (pose == Pose.Crouching) {
ray = Rotate(ray, FMath.DegToRad(30));
}
Vector2 coneBottom = Rotate(ray, fov);
Vector2 coneTop = Rotate(ray, -fov);
List<Vector2> points = new List<Vector2>();
List<AABB> boxes = new List<AABB>();
points.Add(Vector2.Add(eyePos, coneBottom));
points.Add(Vector2.Add(eyePos, coneTop));
foreach (AABB box in collisionTargets) {
int hitCount = points.Count;
if (PointInCone(visionRangeSq, fovCos, eyePos, ray, box.TopLeft)) {
points.Add(box.TopLeft);
}
if (PointInCone(visionRangeSq, fovCos, eyePos, ray, box.TopRight)) {
points.Add(box.TopRight);
}
if (PointInCone(visionRangeSq, fovCos, eyePos, ray, box.BottomLeft)) {
points.Add(box.BottomLeft);
}
if (PointInCone(visionRangeSq, fovCos, eyePos, ray, box.BottomRight)) {
points.Add(box.BottomRight);
}
if (points.Count > hitCount) {
boxes.Add(box);
}
}
HashSet<AABB> boxesSeen = new HashSet<AABB>();
foreach (Vector2 hit in points) {
float minTime = 1;
AABB? closestBox = null;
Vector2 delta = Vector2.Subtract(hit, eyePos);
foreach (AABB box in boxes) {
Hit? maybeHit = box.IntersectSegment(eyePos, delta);
if (maybeHit != null && maybeHit.Value.Time < minTime) {
minTime = maybeHit.Value.Time;
closestBox = box;
}
}
if (closestBox != null) {
Vector2 target = Vector2.Add(eyePos, Vector2.Multiply(delta, minTime));
Debug.AddLine(eyePos, target, color);
boxesSeen.Add(closestBox.Value);
}
}
foreach (AABB box in boxesSeen) {
Debug.AddRect(box, color);
}
Debug.AddLine(eyePos, Vector2.Add(eyePos, ray), Color.Red);
Debug.AddLine(eyePos, Vector2.Add(eyePos, coneTop), Color.Red);
Debug.AddLine(eyePos, Vector2.Add(eyePos, coneBottom), Color.Red);
}
// Returns the desired (dx, dy) for the player to move this frame.
Vector2 HandleInput(float modelTime, History<Input> input) {
Vector2 result = new Vector2() {
X = (int) (input[0].Motion.X * moveSpeed * modelTime)
};
if (input[0].Jump && !input[1].Jump && jumps > 0) {
jumps--;
ySpeed = jumpSpeed;
}
if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
swordSwingTime = 0.3;
swordSwingNum = (swordSwingNum + 1) % swordSwingMax;
}
result.Y = ySpeed * modelTime;
ySpeed += gravity * modelTime;
swordSwingTime -= modelTime;
return result;
}
private int SpriteIndex(Pose pose) {
int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4;
switch (pose) {
case Pose.Walking:
return 35 + frameNum;
case Pose.Jumping:
return 35 + frameNum;
case Pose.SwordSwing:
if (swordSwingTime > 0.2) {
return 0 + swordSwingNum * 3;
} else if (swordSwingTime > 0.1) {
return 1 + swordSwingNum * 3;
} else {
return 2 + swordSwingNum * 3;
}
case Pose.Crouching:
case Pose.Stretching:
case Pose.Standing:
default: {
if (frameNum == 3) {
frameNum = 1;
}
return 29 + frameNum;
}
}
}
public void Draw(SpriteBatch spriteBatch, Camera camera) {
int index = SpriteIndex(pose);
Rectangle textureSource = new Rectangle(index * spriteWidth, 0, spriteWidth, spriteHeight);
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
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);
}
}
}