207 lines
6.2 KiB
C#
207 lines
6.2 KiB
C#
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;
|
|
|
|
private const int spriteSize = 48;
|
|
// TODO: rename to spriteHalfWidth / spriteHalfHeight.
|
|
private const int spriteWidth = 7;
|
|
private const int spriteHeight = 13;
|
|
private readonly Texture2D texture;
|
|
|
|
private Point position = new Point(64, 16 * 10);
|
|
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; } }
|
|
|
|
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 - 7 + spriteHeight + yOffset),
|
|
new Vector2(spriteWidth, spriteHeight));
|
|
}
|
|
|
|
Aabb Box(Point position) {
|
|
return BoxOffset(position, 0);
|
|
}
|
|
|
|
Vector2 movement = HandleInput(modelTime, input);
|
|
|
|
// Broad test: remove all collision targets nowhere near the player.
|
|
var candidates = new List<Aabb>();
|
|
// TODO: This is strictly larger than it needs to be. We could expand only in the actual
|
|
// direction of movement.
|
|
Aabb largeBox = new Aabb(
|
|
new Vector2(position.X, position.Y - 7 + spriteHeight), // current player position
|
|
new Vector2(spriteWidth + Math.Abs(movement.X), spriteHeight + Math.Abs(movement.Y)));
|
|
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;
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
jumpTime = 0.3;
|
|
jumps--;
|
|
ySpeed = jumpSpeed;
|
|
}
|
|
|
|
if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
|
|
swordSwingTime = 0.3;
|
|
}
|
|
|
|
result.Y = ySpeed * modelTime;
|
|
ySpeed += gravity * modelTime;
|
|
jumpTime -= modelTime;
|
|
swordSwingTime -= modelTime;
|
|
return result;
|
|
}
|
|
|
|
private int SpriteIndex(Pose pose) {
|
|
int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4;
|
|
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;
|
|
}
|
|
}
|
|
|
|
public void Draw(SpriteBatch spriteBatch, Camera camera) {
|
|
int index = SpriteIndex(pose);
|
|
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);
|
|
}
|
|
}
|
|
}
|