sneak/Jumpy.Shared/Player.cs

201 lines
6.9 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace Jumpy {
class Player {
enum Facing { Left, Right };
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
enum AirState { Jumping, Ground, Falling };
private Texture2D texture;
private const int spriteSize = 48;
private const int spriteWidth = 7;
private const int moveSpeed = 180;
private const int jumpSpeed = 600;
private const int gravity = 2400;
private Point position = new Point(Camera.Width / 2, 10);
private Facing facing = Facing.Right;
private Pose pose = Pose.Standing;
private AirState airState = AirState.Ground;
private double swordSwingTime = 0;
private double jumpTime = 0;
private double ySpeed = 0;
public Player(Texture2D texture) {
this.texture = texture;
}
private Rectangle Bbox(Point position) {
return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
}
public void Update(
GameTime time, History<GamePadState> gamePad, List<Rectangle> collisionTargets) {
Point oldPosition = position;
AirState oldAirState = airState;
UpdateFromGamePad(time, gamePad);
Rectangle oldBbox = Bbox(oldPosition);
Rectangle playerBbox = Bbox(position);
bool standingOnGround = false;
// TODO: implement https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
// e.g. http://members.chello.at/~easyfilter/bresenham.html
foreach (var rect in collisionTargets) {
playerBbox = Bbox(position);
// 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;
}
playerBbox = Bbox(position);
}
// after fixing that, we check for hitting our head or hitting the ground.
if (playerBbox.Intersects(rect)) {
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;
}
} else {
playerBbox.Height += 1;
if (playerBbox.Intersects(rect)) {
standingOnGround = true;
Debug.AddRect(rect, Color.Cyan);
} else {
Debug.AddRect(rect, Color.Green);
}
}
}
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);
}
}
// TODO: refactor input to have a virtual "which directions & buttons were being pressed"
// instead of complicated if-statements in this function.
// TODO: refactor to use a state-machine.
void UpdateFromGamePad(GameTime time, History<GamePadState> gamePad) {
if (gamePad[0].IsButtonDown(Buttons.A) && gamePad[1].IsButtonUp(Buttons.A) &&
airState == AirState.Ground) {
pose = Pose.Jumping;
airState = AirState.Jumping;
jumpTime = 0.5;
ySpeed = -jumpSpeed;
return;
}
if (gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X)
&& swordSwingTime <= 0) {
pose = Pose.SwordSwing;
swordSwingTime = 0.3;
return;
}
Vector2 leftStick = gamePad[0].ThumbSticks.Left;
if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5) {
facing = Facing.Left;
pose = Pose.Walking;
position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
} else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5) {
facing = Facing.Right;
pose = Pose.Walking;
position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
} else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5) {
pose = Pose.Crouching;
} else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5) {
pose = Pose.Stretching;
} else {
pose = Pose.Standing;
}
if (jumpTime > 0) {
jumpTime -= time.ElapsedGameTime.TotalSeconds;
}
if (swordSwingTime > 0) {
swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
pose = Pose.SwordSwing;
}
if (airState == AirState.Jumping || airState == AirState.Falling) {
position.Y += (int) (ySpeed * time.ElapsedGameTime.TotalSeconds);
ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
}
if (airState == AirState.Jumping && pose != Pose.SwordSwing) {
pose = Pose.Jumping;
}
position.X = Math.Min(Math.Max(position.X, 0 + spriteWidth), Camera.Width - spriteWidth);
}
private int spritePosition(Pose pose, GameTime time) {
int frameNum = (time.TotalGameTime.Milliseconds / 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.25) {
return 15;
} else if (jumpTime > 0) {
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(GameTime time, SpriteBatch spriteBatch) {
// 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);
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
SpriteEffects effect = facing == Facing.Right ?
SpriteEffects.FlipHorizontally : SpriteEffects.None;
Vector2 drawPos = new Vector2(position.X, position.Y);
spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
Vector2.One, effect, 0f);
}
}
}