2019-12-08 14:38:26 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
using System;
|
2019-12-11 22:57:30 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
2020-01-08 22:56:57 +00:00
|
|
|
|
namespace SemiColinGames {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
class Player {
|
|
|
|
|
enum Facing { Left, Right };
|
2019-12-08 15:44:43 +00:00
|
|
|
|
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
|
2019-12-12 18:33:52 +00:00
|
|
|
|
enum AirState { Jumping, Ground, Falling };
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
2019-12-09 01:04:22 +00:00
|
|
|
|
private Texture2D texture;
|
2019-12-08 23:51:23 +00:00
|
|
|
|
private const int spriteSize = 48;
|
2019-12-09 01:04:22 +00:00
|
|
|
|
private const int spriteWidth = 7;
|
2019-12-12 20:20:08 +00:00
|
|
|
|
private const int moveSpeed = 180;
|
|
|
|
|
private const int jumpSpeed = 600;
|
|
|
|
|
private const int gravity = 2400;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
2019-12-17 20:37:32 +00:00
|
|
|
|
private Point position = new Point(64, 16);
|
2019-12-08 14:38:26 +00:00
|
|
|
|
private Facing facing = Facing.Right;
|
|
|
|
|
private Pose pose = Pose.Standing;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
private AirState airState = AirState.Ground;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
private double swordSwingTime = 0;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
private double jumpTime = 0;
|
|
|
|
|
private double ySpeed = 0;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
|
|
|
|
public Player(Texture2D texture) {
|
|
|
|
|
this.texture = texture;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 20:37:32 +00:00
|
|
|
|
public Point Position { get { return position; } }
|
|
|
|
|
|
2019-12-16 23:23:25 +00:00
|
|
|
|
private Rectangle Bbox(Point position) {
|
|
|
|
|
return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 22:57:30 +00:00
|
|
|
|
public void Update(
|
2019-12-20 23:13:56 +00:00
|
|
|
|
GameTime time, History<GamePadState> gamePad, History<KeyboardState> keyboard,
|
|
|
|
|
List<Rectangle> collisionTargets) {
|
2019-12-12 18:33:52 +00:00
|
|
|
|
Point oldPosition = position;
|
|
|
|
|
AirState oldAirState = airState;
|
2019-12-20 23:13:56 +00:00
|
|
|
|
UpdateFromInput(time, gamePad, keyboard);
|
2019-12-11 22:57:30 +00:00
|
|
|
|
|
2019-12-16 23:23:25 +00:00
|
|
|
|
Rectangle oldBbox = Bbox(oldPosition);
|
|
|
|
|
Rectangle playerBbox = Bbox(position);
|
2019-12-12 18:33:52 +00:00
|
|
|
|
bool standingOnGround = false;
|
2020-01-10 20:41:54 +00:00
|
|
|
|
|
2019-12-11 22:57:30 +00:00
|
|
|
|
foreach (var rect in collisionTargets) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
playerBbox = Bbox(position);
|
2019-12-12 18:33:52 +00:00
|
|
|
|
|
2019-12-16 02:26:39 +00:00
|
|
|
|
// 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;
|
|
|
|
|
}
|
2019-12-16 23:23:25 +00:00
|
|
|
|
playerBbox = Bbox(position);
|
2019-12-16 02:26:39 +00:00
|
|
|
|
}
|
|
|
|
|
// after fixing that, we check for hitting our head or hitting the ground.
|
2019-12-11 22:57:30 +00:00
|
|
|
|
if (playerBbox.Intersects(rect)) {
|
2019-12-12 18:33:52 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-12-11 22:57:30 +00:00
|
|
|
|
} else {
|
2019-12-12 18:33:52 +00:00
|
|
|
|
playerBbox.Height += 1;
|
|
|
|
|
if (playerBbox.Intersects(rect)) {
|
|
|
|
|
standingOnGround = true;
|
|
|
|
|
Debug.AddRect(rect, Color.Cyan);
|
|
|
|
|
} else {
|
|
|
|
|
Debug.AddRect(rect, Color.Green);
|
|
|
|
|
}
|
2019-12-11 22:57:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-12 18:33:52 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2019-12-11 22:57:30 +00:00
|
|
|
|
}
|
2019-12-16 23:23:25 +00:00
|
|
|
|
|
2019-12-20 23:13:56 +00:00
|
|
|
|
void UpdateFromInput(
|
|
|
|
|
GameTime time, History<GamePadState> gamePad, History<KeyboardState> keyboard) {
|
|
|
|
|
if ((gamePad[0].IsButtonDown(Buttons.A) && gamePad[1].IsButtonUp(Buttons.A) ||
|
|
|
|
|
keyboard[0].IsKeyDown(Keys.J) && keyboard[1].IsKeyUp(Keys.J)) &&
|
2019-12-12 20:22:24 +00:00
|
|
|
|
airState == AirState.Ground) {
|
2019-12-08 15:44:43 +00:00
|
|
|
|
pose = Pose.Jumping;
|
|
|
|
|
airState = AirState.Jumping;
|
|
|
|
|
jumpTime = 0.5;
|
|
|
|
|
ySpeed = -jumpSpeed;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-10 16:22:42 +00:00
|
|
|
|
|
2019-12-20 23:13:56 +00:00
|
|
|
|
if ((gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X) ||
|
|
|
|
|
keyboard[0].IsKeyDown(Keys.K) && keyboard[1].IsKeyUp(Keys.K))
|
2019-12-10 16:22:42 +00:00
|
|
|
|
&& swordSwingTime <= 0) {
|
2019-12-08 14:48:29 +00:00
|
|
|
|
pose = Pose.SwordSwing;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
swordSwingTime = 0.3;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-10 16:22:42 +00:00
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
Vector2 leftStick = gamePad[0].ThumbSticks.Left;
|
2019-12-20 23:13:56 +00:00
|
|
|
|
if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5 ||
|
|
|
|
|
keyboard[0].IsKeyDown(Keys.A)) {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
facing = Facing.Left;
|
|
|
|
|
pose = Pose.Walking;
|
2019-12-10 16:12:56 +00:00
|
|
|
|
position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
|
2019-12-20 23:13:56 +00:00
|
|
|
|
} else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5 ||
|
|
|
|
|
keyboard[0].IsKeyDown(Keys.D)) {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
facing = Facing.Right;
|
|
|
|
|
pose = Pose.Walking;
|
2019-12-10 16:12:56 +00:00
|
|
|
|
position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
|
2019-12-20 23:13:56 +00:00
|
|
|
|
} else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5 ||
|
|
|
|
|
keyboard[0].IsKeyDown(Keys.S)) {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
pose = Pose.Crouching;
|
2019-12-20 23:13:56 +00:00
|
|
|
|
} else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5 ||
|
|
|
|
|
keyboard[0].IsKeyDown(Keys.W)) {
|
2019-12-08 14:38:26 +00:00
|
|
|
|
pose = Pose.Stretching;
|
|
|
|
|
} else {
|
|
|
|
|
pose = Pose.Standing;
|
|
|
|
|
}
|
2019-12-08 14:48:29 +00:00
|
|
|
|
|
2019-12-08 15:44:43 +00:00
|
|
|
|
if (jumpTime > 0) {
|
2019-12-09 04:02:22 +00:00
|
|
|
|
jumpTime -= time.ElapsedGameTime.TotalSeconds;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
}
|
2019-12-08 14:48:29 +00:00
|
|
|
|
if (swordSwingTime > 0) {
|
2019-12-09 04:02:22 +00:00
|
|
|
|
swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
pose = Pose.SwordSwing;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 18:33:52 +00:00
|
|
|
|
if (airState == AirState.Jumping || airState == AirState.Falling) {
|
2019-12-09 04:02:22 +00:00
|
|
|
|
position.Y += (int) (ySpeed * time.ElapsedGameTime.TotalSeconds);
|
|
|
|
|
ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
if (airState == AirState.Jumping && pose != Pose.SwordSwing) {
|
|
|
|
|
pose = Pose.Jumping;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 20:37:32 +00:00
|
|
|
|
position.X = Math.Max(position.X, 0 + spriteWidth);
|
2019-12-08 14:38:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 14:31:14 +00:00
|
|
|
|
private int SpritePosition(Pose pose, GameTime time) {
|
2019-12-08 19:45:13 +00:00
|
|
|
|
int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
|
|
|
|
|
if (frameNum == 3) {
|
|
|
|
|
frameNum = 1;
|
|
|
|
|
}
|
2019-12-08 14:38:26 +00:00
|
|
|
|
switch (pose) {
|
|
|
|
|
case Pose.Walking:
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 6 + frameNum;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
case Pose.Stretching:
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 18 + frameNum;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
case Pose.Jumping:
|
|
|
|
|
if (jumpTime > 0.25) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 15;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
} else if (jumpTime > 0) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 16;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
} else {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 17;
|
2019-12-08 15:44:43 +00:00
|
|
|
|
}
|
2019-12-08 14:48:29 +00:00
|
|
|
|
case Pose.SwordSwing:
|
|
|
|
|
if (swordSwingTime > 0.2) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 30;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
} else if (swordSwingTime > 0.1) {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 31;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
} else {
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 32;
|
2019-12-08 14:48:29 +00:00
|
|
|
|
}
|
2019-12-16 23:23:25 +00:00
|
|
|
|
case Pose.Crouching:
|
|
|
|
|
return 25;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
case Pose.Standing:
|
|
|
|
|
default:
|
2019-12-16 23:23:25 +00:00
|
|
|
|
return 7;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 20:37:32 +00:00
|
|
|
|
public void Draw(SpriteBatch spriteBatch, Camera camera, GameTime time) {
|
2020-01-11 14:31:14 +00:00
|
|
|
|
int index = SpritePosition(pose, time);
|
2019-12-16 23:23:25 +00:00
|
|
|
|
Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
|
2019-12-08 14:38:26 +00:00
|
|
|
|
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
|
|
|
|
|
SpriteEffects effect = facing == Facing.Right ?
|
|
|
|
|
SpriteEffects.FlipHorizontally : SpriteEffects.None;
|
2019-12-17 20:37:32 +00:00
|
|
|
|
Vector2 drawPos = new Vector2(position.X - camera.Left, position.Y);
|
2019-12-08 23:51:23 +00:00
|
|
|
|
spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
|
2019-12-08 14:38:26 +00:00
|
|
|
|
Vector2.One, effect, 0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|