sneak/Jumpy.Shared/Player.cs

132 lines
4.6 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
namespace Jumpy {
class Player {
enum Facing { Left, Right };
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
enum AirState { Jumping, Ground };
private const int spriteSize = 144;
private const int spriteWidth = 20;
private const int moveSpeed = 600;
private const int jumpSpeed = 1800;
private const int gravity = 6000;
private Texture2D texture;
// TODO: stop assuming 1920x1080.
private const int groundLevel = 1080 - spriteSize / 2 - 200;
private Vector2 position = new Vector2(200, groundLevel);
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;
}
public void Update(GameTime gameTime, GamePadState gamePad) {
if (gamePad.Buttons.A == ButtonState.Pressed && airState == AirState.Ground) {
pose = Pose.Jumping;
airState = AirState.Jumping;
jumpTime = 0.5;
ySpeed = -jumpSpeed;
return;
}
if (gamePad.Buttons.X == ButtonState.Pressed && swordSwingTime <= 0) {
pose = Pose.SwordSwing;
swordSwingTime = 0.3;
return;
}
Vector2 leftStick = gamePad.ThumbSticks.Left;
if (gamePad.DPad.Left == ButtonState.Pressed || leftStick.X < -0.5) {
facing = Facing.Left;
pose = Pose.Walking;
position.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
} else if (gamePad.DPad.Right == ButtonState.Pressed || leftStick.X > 0.5) {
facing = Facing.Right;
pose = Pose.Walking;
position.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
} else if (gamePad.DPad.Down == ButtonState.Pressed || leftStick.Y < -0.5) {
pose = Pose.Crouching;
} else if (gamePad.DPad.Up == ButtonState.Pressed || leftStick.Y > 0.5) {
pose = Pose.Stretching;
} else {
pose = Pose.Standing;
}
if (jumpTime > 0) {
jumpTime -= gameTime.ElapsedGameTime.TotalSeconds;
}
if (swordSwingTime > 0) {
swordSwingTime -= gameTime.ElapsedGameTime.TotalSeconds;
pose = Pose.SwordSwing;
}
if (airState == AirState.Jumping) {
position.Y += (float)(ySpeed * gameTime.ElapsedGameTime.TotalSeconds);
ySpeed += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (position.Y > groundLevel) {
position.Y = groundLevel;
ySpeed = 0;
airState = AirState.Ground;
}
}
if (airState == AirState.Jumping && pose != Pose.SwordSwing) {
pose = Pose.Jumping;
}
position.X = Math.Min(Math.Max(position.X, 0 + spriteWidth), 1920 - spriteWidth);
}
private Point spritePosition(Pose pose, GameTime time) {
switch (pose) {
case Pose.Walking:
int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
if (frameNum == 3) {
frameNum = 1;
}
return new Point(spriteSize * frameNum + spriteSize * 6, 0);
case Pose.Crouching:
return new Point(spriteSize * 7, spriteSize * 2);
case Pose.Stretching:
return new Point(spriteSize * 1, spriteSize * 2);
case Pose.Jumping:
if (jumpTime > 0.25) {
return new Point(spriteSize * 6, spriteSize);
} else if (jumpTime > 0) {
return new Point(spriteSize * 7, spriteSize);
} else {
return new Point(spriteSize * 8, spriteSize);
}
case Pose.SwordSwing:
if (swordSwingTime > 0.2) {
return new Point(spriteSize * 3, 0);
} else if (swordSwingTime > 0.1) {
return new Point(spriteSize * 4, 0);
} else {
return new Point(spriteSize * 5, 0);
}
case Pose.Standing:
default:
return new Point(spriteSize * 7, 0);
}
}
public void Draw(GameTime time, SpriteBatch spriteBatch) {
Point source = spritePosition(pose, time);
Rectangle textureSource = new Rectangle(source.X, source.Y, spriteSize, spriteSize);
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
SpriteEffects effect = facing == Facing.Right ?
SpriteEffects.FlipHorizontally : SpriteEffects.None;
spriteBatch.Draw(texture, position, textureSource, Color.White, 0f, spriteCenter,
Vector2.One, effect, 0f);
}
}
}