2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-01-29 20:43:00 +00:00
|
|
|
using System;
|
2020-01-18 03:41:45 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-03-06 17:28:58 +00:00
|
|
|
public class Player {
|
2020-03-11 20:12:46 +00:00
|
|
|
private enum Pose { Walking, Standing, SwordSwing, Jumping };
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
private const int moveSpeed = 180;
|
|
|
|
private const int jumpSpeed = -600;
|
2020-02-19 19:59:17 +00:00
|
|
|
private const int gravity = 1600;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 21:45:13 +00:00
|
|
|
// Details of the sprite image.
|
2020-01-30 21:42:31 +00:00
|
|
|
// 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;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 21:45:13 +00:00
|
|
|
// Details of the actual Player model.
|
2020-02-26 00:18:58 +00:00
|
|
|
|
2020-01-29 21:45:13 +00:00
|
|
|
// 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.
|
2020-03-11 20:04:22 +00:00
|
|
|
private Vector2 position;
|
2020-01-30 21:42:31 +00:00
|
|
|
private Vector2 halfSize = new Vector2(11, 24);
|
2020-03-11 20:04:22 +00:00
|
|
|
// Fractional-pixel movement that was left over from a previous frame's movement.
|
|
|
|
// Useful so that we can run at a slow time-step and still get non-zero motion.
|
|
|
|
private Vector2 residual = Vector2.Zero;
|
2020-01-29 21:45:13 +00:00
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
private int jumps = 0;
|
|
|
|
private Pose pose = Pose.Jumping;
|
|
|
|
private double swordSwingTime = 0;
|
2020-01-30 21:53:13 +00:00
|
|
|
private int swordSwingNum = 0;
|
2020-01-18 03:41:45 +00:00
|
|
|
private float ySpeed = 0;
|
2020-02-28 00:13:34 +00:00
|
|
|
private float invincibilityTime = 0;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-03-11 20:04:22 +00:00
|
|
|
public Player(Vector2 position, int facing) {
|
2020-03-08 21:23:51 +00:00
|
|
|
this.position = position;
|
|
|
|
Facing = facing;
|
2020-02-27 21:26:22 +00:00
|
|
|
Health = MaxHealth;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 00:13:34 +00:00
|
|
|
public int MaxHealth { get; private set; } = 3;
|
2020-02-27 21:26:22 +00:00
|
|
|
|
|
|
|
public int Health { get; private set; }
|
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
public int Facing { get; private set; }
|
2020-02-11 22:06:17 +00:00
|
|
|
|
2020-03-11 20:04:22 +00:00
|
|
|
public Vector2 Position { get { return position; } }
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-03-06 17:28:58 +00:00
|
|
|
public void Update(float modelTime, World world, History<Input> input) {
|
2020-03-11 20:04:22 +00:00
|
|
|
AABB BoxOffset(Vector2 position, int yOffset) {
|
2020-01-30 21:58:42 +00:00
|
|
|
return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize);
|
2020-01-29 21:11:28 +00:00
|
|
|
}
|
2020-01-29 19:39:25 +00:00
|
|
|
|
2020-03-11 20:04:22 +00:00
|
|
|
AABB Box(Vector2 position) {
|
2020-01-29 21:11:28 +00:00
|
|
|
return BoxOffset(position, 0);
|
|
|
|
}
|
2020-01-29 19:39:25 +00:00
|
|
|
|
2020-02-28 00:13:34 +00:00
|
|
|
invincibilityTime -= modelTime;
|
|
|
|
|
2020-03-11 20:04:22 +00:00
|
|
|
Vector2 inputMovement = HandleInput(modelTime, input);
|
|
|
|
Vector2 movement = Vector2.Add(residual, inputMovement);
|
|
|
|
residual = new Vector2(movement.X - (int) movement.X, movement.Y - (int) movement.Y);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-29 20:43:00 +00:00
|
|
|
// Broad test: remove all collision targets nowhere near the player.
|
2020-01-30 21:58:42 +00:00
|
|
|
// TODO: don't allocate a list here.
|
|
|
|
var candidates = new List<AABB>();
|
2020-01-29 22:43:32 +00:00
|
|
|
// 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.)
|
2020-01-30 21:58:42 +00:00
|
|
|
AABB largeBox = new AABB(
|
2020-03-11 20:04:22 +00:00
|
|
|
Vector2.Add(position, Vector2.Divide(movement, 2)),
|
|
|
|
Vector2.Add(halfSize, new Vector2(Math.Abs(movement.X) + 1, Math.Abs(movement.Y) + 1)));
|
2020-03-06 17:28:58 +00:00
|
|
|
foreach (var box in world.CollisionTargets) {
|
2020-01-29 20:43:00 +00:00
|
|
|
if (box.Intersect(largeBox) != null) {
|
2020-02-28 00:13:34 +00:00
|
|
|
// Debug.AddRect(box, Color.Green);
|
2020-01-29 21:11:28 +00:00
|
|
|
candidates.Add(box);
|
2020-01-29 20:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-02-28 00:13:34 +00:00
|
|
|
bool harmedByCollision = false;
|
2020-01-29 20:43:00 +00:00
|
|
|
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) {
|
2020-03-11 20:04:22 +00:00
|
|
|
Vector2 newPosition = new Vector2(position.X, position.Y + dy);
|
2020-01-30 21:58:42 +00:00
|
|
|
AABB player = Box(newPosition);
|
2020-01-29 20:43:00 +00:00
|
|
|
bool reject = false;
|
2020-01-29 21:11:28 +00:00
|
|
|
foreach (var box in candidates) {
|
2020-01-29 20:43:00 +00:00
|
|
|
if (box.Intersect(player) != null) {
|
2020-02-28 00:13:34 +00:00
|
|
|
Debug.AddRect(box, Color.Cyan);
|
2020-01-29 20:43:00 +00:00
|
|
|
reject = true;
|
2020-03-08 22:30:46 +00:00
|
|
|
if (box.Tile?.IsHazard ?? false) {
|
2020-02-28 00:13:34 +00:00
|
|
|
Debug.AddRect(box, Color.Red);
|
|
|
|
harmedByCollision = true;
|
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
if (!reject) {
|
|
|
|
position = newPosition;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
if (dx != 0) {
|
2020-03-11 20:04:22 +00:00
|
|
|
Vector2 newPosition = new Vector2(position.X + dx, position.Y);
|
2020-01-30 21:58:42 +00:00
|
|
|
AABB player = Box(newPosition);
|
2020-01-29 20:43:00 +00:00
|
|
|
bool reject = false;
|
2020-01-29 21:11:28 +00:00
|
|
|
foreach (var box in candidates) {
|
2020-01-29 20:43:00 +00:00
|
|
|
if (box.Intersect(player) != null) {
|
2020-02-28 00:13:34 +00:00
|
|
|
Debug.AddRect(box, Color.Cyan);
|
2020-01-29 20:43:00 +00:00
|
|
|
reject = true;
|
2020-03-08 22:30:46 +00:00
|
|
|
if (box.Tile?.IsHazard ?? false) {
|
2020-02-28 00:13:34 +00:00
|
|
|
Debug.AddRect(box, Color.Red);
|
|
|
|
harmedByCollision = true;
|
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
if (!reject) {
|
|
|
|
position = newPosition;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
|
|
|
|
bool standingOnGround = false;
|
2020-01-30 21:58:42 +00:00
|
|
|
AABB groundIntersect = BoxOffset(position, 1);
|
2020-01-29 21:11:28 +00:00
|
|
|
foreach (var box in candidates) {
|
2020-01-29 20:43:00 +00:00
|
|
|
if (groundIntersect.Intersect(box) != null) {
|
2020-02-28 00:13:34 +00:00
|
|
|
Debug.AddRect(box, Color.Cyan);
|
2020-01-29 20:43:00 +00:00
|
|
|
standingOnGround = true;
|
2020-03-08 22:30:46 +00:00
|
|
|
if (box.Tile?.IsHazard ?? false) {
|
2020-02-28 00:13:34 +00:00
|
|
|
Debug.AddRect(box, Color.Red);
|
|
|
|
harmedByCollision = true;
|
|
|
|
}
|
2020-01-29 20:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
if (standingOnGround) {
|
|
|
|
jumps = 1;
|
2020-01-29 20:43:00 +00:00
|
|
|
ySpeed = -0.0001f;
|
2020-02-25 14:57:31 +00:00
|
|
|
Debug.AddRect(Box(position), Color.Cyan);
|
2020-01-18 03:41:45 +00:00
|
|
|
} else {
|
|
|
|
jumps = 0;
|
2020-02-25 14:57:31 +00:00
|
|
|
Debug.AddRect(Box(position), Color.Orange);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 00:13:34 +00:00
|
|
|
if (harmedByCollision && invincibilityTime <= 0) {
|
2020-03-10 19:04:41 +00:00
|
|
|
world.ScreenShake();
|
2020-02-28 00:13:34 +00:00
|
|
|
Health -= 1;
|
|
|
|
invincibilityTime = 0.6f;
|
|
|
|
}
|
|
|
|
|
2020-03-11 20:04:22 +00:00
|
|
|
if (inputMovement.X > 0) {
|
2020-02-15 19:48:17 +00:00
|
|
|
Facing = 1;
|
2020-03-11 20:04:22 +00:00
|
|
|
} else if (inputMovement.X < 0) {
|
2020-02-15 19:48:17 +00:00
|
|
|
Facing = -1;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
if (swordSwingTime > 0) {
|
|
|
|
pose = Pose.SwordSwing;
|
|
|
|
} else if (jumps == 0) {
|
|
|
|
pose = Pose.Jumping;
|
2020-03-11 20:04:22 +00:00
|
|
|
} else if (inputMovement.X != 0) {
|
2020-01-18 03:41:45 +00:00
|
|
|
pose = Pose.Walking;
|
|
|
|
} else {
|
|
|
|
pose = Pose.Standing;
|
|
|
|
}
|
2020-02-02 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
// Returns the desired (dx, dy) for the player to move this frame.
|
2020-01-24 23:36:55 +00:00
|
|
|
Vector2 HandleInput(float modelTime, History<Input> input) {
|
2020-01-25 02:09:09 +00:00
|
|
|
Vector2 result = new Vector2() {
|
2020-03-11 20:04:22 +00:00
|
|
|
X = input[0].Motion.X * moveSpeed * modelTime
|
2020-01-25 02:09:09 +00:00
|
|
|
};
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
if (input[0].Jump && !input[1].Jump && jumps > 0) {
|
|
|
|
jumps--;
|
|
|
|
ySpeed = jumpSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
|
|
|
|
swordSwingTime = 0.3;
|
2020-03-11 20:04:22 +00:00
|
|
|
swordSwingNum++;
|
2020-02-28 01:23:05 +00:00
|
|
|
SoundEffects.SwordSwings[swordSwingNum % SoundEffects.SwordSwings.Length].Play();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 23:36:55 +00:00
|
|
|
result.Y = ySpeed * modelTime;
|
|
|
|
ySpeed += gravity * modelTime;
|
|
|
|
swordSwingTime -= modelTime;
|
2020-01-18 03:41:45 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:28:34 +00:00
|
|
|
private Rectangle GetTextureSource(Pose pose) {
|
2020-03-06 02:12:11 +00:00
|
|
|
double time = Clock.ModelTime.TotalSeconds;
|
2020-01-18 03:41:45 +00:00
|
|
|
switch (pose) {
|
|
|
|
case Pose.Walking:
|
|
|
|
case Pose.Jumping:
|
2020-03-05 20:28:34 +00:00
|
|
|
return Sprites.Ninja.GetTextureSource("run", time);
|
2020-01-18 03:41:45 +00:00
|
|
|
case Pose.SwordSwing:
|
2020-03-05 20:28:34 +00:00
|
|
|
// TODO: make a proper animation class & FSM-driven animations.
|
|
|
|
return Sprites.Ninja.GetTextureSource(
|
2020-03-06 02:12:11 +00:00
|
|
|
"attack_sword", 0.3 - swordSwingTime);
|
2020-01-18 03:41:45 +00:00
|
|
|
case Pose.Standing:
|
2020-03-05 20:28:34 +00:00
|
|
|
default:
|
|
|
|
return Sprites.Ninja.GetTextureSource("idle", time);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:37:42 +00:00
|
|
|
public void Draw(SpriteBatch spriteBatch) {
|
2020-03-05 20:28:34 +00:00
|
|
|
Rectangle textureSource = GetTextureSource(pose);
|
2020-01-30 21:42:31 +00:00
|
|
|
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
|
2020-02-15 19:48:17 +00:00
|
|
|
SpriteEffects effect = Facing == 1 ?
|
2020-02-28 22:08:34 +00:00
|
|
|
SpriteEffects.None : SpriteEffects.FlipHorizontally;
|
2020-02-28 00:13:34 +00:00
|
|
|
Color color = Color.White;
|
|
|
|
if (invincibilityTime > 0 && invincibilityTime % 0.2f > 0.1f) {
|
|
|
|
color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
|
|
|
|
}
|
2020-03-11 20:04:22 +00:00
|
|
|
spriteBatch.Draw(Textures.Ninja.Get, Vector2.Floor(position), textureSource, color, 0f,
|
2020-02-19 16:19:23 +00:00
|
|
|
spriteCenter, Vector2.One, effect, 0f);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|