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-02-15 19:56:42 +00:00
|
|
|
private enum Pose { Walking, Standing, Crouching, Stretching, 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-02-19 19:59:17 +00:00
|
|
|
private Point position = new Point(64, 16 * 12);
|
2020-01-30 21:42:31 +00:00
|
|
|
private Vector2 halfSize = new Vector2(11, 24);
|
2020-02-02 13:57:49 +00:00
|
|
|
private Vector2 eyeOffsetStanding = new Vector2(7, -14);
|
|
|
|
private Vector2 eyeOffsetWalking = new Vector2(15, -7);
|
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-02-02 15:23:47 +00:00
|
|
|
private const int swordSwingMax = 6;
|
2020-01-18 03:41:45 +00:00
|
|
|
private float ySpeed = 0;
|
2020-02-19 19:59:17 +00:00
|
|
|
private double jumpTime = 0;
|
2020-02-28 00:13:34 +00:00
|
|
|
private float invincibilityTime = 0;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-02-19 16:19:23 +00:00
|
|
|
public Player() {
|
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-02-15 19:48:17 +00:00
|
|
|
public int Facing { get; private set; } = 1;
|
2020-02-11 22:06:17 +00:00
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
public Point Position { get { return position; } }
|
|
|
|
|
2020-03-06 17:28:58 +00:00
|
|
|
public void Update(float modelTime, World world, History<Input> input) {
|
2020-01-30 21:58:42 +00:00
|
|
|
AABB BoxOffset(Point position, int yOffset) {
|
|
|
|
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-01-30 21:58:42 +00:00
|
|
|
AABB Box(Point 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-01-24 23:36:55 +00:00
|
|
|
Vector2 movement = HandleInput(modelTime, input);
|
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-01-29 22:43:32 +00:00
|
|
|
new Vector2(position.X + movement.X / 2, position.Y + movement.Y / 2),
|
|
|
|
new Vector2(halfSize.X + Math.Abs(movement.X) + 1, halfSize.Y + 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) {
|
|
|
|
Point newPosition = new Point(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-02-28 00:13:34 +00:00
|
|
|
if (box.Terrain.IsHarmful) {
|
|
|
|
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) {
|
|
|
|
Point newPosition = new Point(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-02-28 00:13:34 +00:00
|
|
|
if (box.Terrain.IsHarmful) {
|
|
|
|
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-02-28 00:13:34 +00:00
|
|
|
if (box.Terrain.IsHarmful) {
|
|
|
|
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-02-19 19:59:17 +00:00
|
|
|
double jumpElapsed = Clock.ModelTime.TotalSeconds - jumpTime;
|
2020-02-26 22:58:38 +00:00
|
|
|
// if (jumpElapsed > 0.2) {
|
|
|
|
// Debug.WriteLine("jump time: " + jumpElapsed);
|
|
|
|
// }
|
2020-02-19 19:59:17 +00:00
|
|
|
jumpTime = Clock.ModelTime.TotalSeconds;
|
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) {
|
|
|
|
Health -= 1;
|
|
|
|
invincibilityTime = 0.6f;
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
if (movement.X > 0) {
|
2020-02-15 19:48:17 +00:00
|
|
|
Facing = 1;
|
2020-01-18 03:41:45 +00:00
|
|
|
} else if (movement.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;
|
|
|
|
} 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;
|
|
|
|
}
|
2020-02-02 13:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 22:06:17 +00:00
|
|
|
public Vector2 EyePosition {
|
|
|
|
get {
|
|
|
|
bool walking = pose == Pose.Walking || pose == Pose.Jumping;
|
|
|
|
Vector2 eyeOffset = walking ? eyeOffsetWalking : eyeOffsetStanding;
|
|
|
|
return Vector2.Add(
|
2020-02-15 19:48:17 +00:00
|
|
|
Position.ToVector2(), new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
|
2020-02-02 13:57:49 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-01-25 02:09:09 +00:00
|
|
|
|
2020-02-15 19:56:42 +00:00
|
|
|
public float VisionRange {
|
|
|
|
get {
|
|
|
|
return 150;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:59:59 +00:00
|
|
|
public float FieldOfView {
|
|
|
|
get {
|
|
|
|
return FMath.DegToRad(120);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:56:42 +00:00
|
|
|
public Vector2 VisionRay {
|
|
|
|
get {
|
|
|
|
Vector2 ray = new Vector2(VisionRange * Facing, 0);
|
|
|
|
if (pose == Pose.Stretching) {
|
|
|
|
ray = ray.Rotate(Facing * FMath.DegToRad(-30));
|
|
|
|
}
|
|
|
|
if (pose == Pose.Crouching) {
|
|
|
|
ray = ray.Rotate(Facing * FMath.DegToRad(30));
|
|
|
|
}
|
|
|
|
return ray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
X = (int) (input[0].Motion.X * moveSpeed * modelTime)
|
|
|
|
};
|
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-01-30 21:53:13 +00:00
|
|
|
swordSwingNum = (swordSwingNum + 1) % swordSwingMax;
|
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.Crouching:
|
2020-02-03 14:03:44 +00:00
|
|
|
case Pose.Stretching:
|
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-05 20:28:34 +00:00
|
|
|
spriteBatch.Draw(Textures.Ninja.Get, position.ToVector2(), textureSource, color, 0f,
|
2020-02-19 16:19:23 +00:00
|
|
|
spriteCenter, Vector2.One, effect, 0f);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|