mirror of
https://github.com/TheRavenwoodArts/LunaLightXMG.git
synced 2026-07-30 22:55:03 -07:00
209 lines
7.2 KiB
C#
209 lines
7.2 KiB
C#
using System;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Content;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
namespace LunaLightXMG
|
|
{
|
|
internal class Player
|
|
{
|
|
// Declare variables
|
|
private float hsp; // Horizontal speed
|
|
public float vsp; // Vertical speed
|
|
private float wallJumpDelay; // Delay for wall jumping
|
|
public bool grounded; // Is the player grounded?
|
|
public bool walled; // Is the player wall sliding?
|
|
private bool wasWalled; // Remember is the player was on a wall or ground.
|
|
private float hspAcc; // Horizontal acceleration
|
|
private float hspFrictionGround; // Ground friction
|
|
private float hspFrictionAir; // Air friction
|
|
private float hspWalk; // Maximum horizontal speed
|
|
private float grv; // Gravity
|
|
private float vspMax; // Maximum vertical speed
|
|
private float grvWall; // Gravity when wall sliding
|
|
private float vspMaxWall; // Maximum vertical speed when wall sliding
|
|
private float vspJump; // Jump speed
|
|
|
|
// Player initilization
|
|
private Texture2D texture;
|
|
public Vector2 position;
|
|
public BoundingBox BoundingBox { get; private set; }
|
|
|
|
// Constructor to initialize variables
|
|
public Player(Vector2 initPosition)
|
|
{
|
|
// Initialize your variables here
|
|
hsp = 0;
|
|
vsp = 0;
|
|
wallJumpDelay = 0;
|
|
grounded = false;
|
|
walled = false;
|
|
wasWalled = false;
|
|
hspAcc = 0.2f;
|
|
hspFrictionGround = 0.3f;
|
|
hspFrictionAir = 0.3f;
|
|
hspWalk = 3f;
|
|
grv = 0.2f;
|
|
vspMax = 10f;
|
|
grvWall = 0.1f;
|
|
vspMaxWall = 5f;
|
|
vspJump = -4f;
|
|
|
|
position = initPosition;
|
|
BoundingBox = new BoundingBox(new Rectangle((int)position.X, (int)position.Y, 8, 16));
|
|
}
|
|
public void LoadContent(ContentManager content)
|
|
{
|
|
texture = content.Load<Texture2D>("player");
|
|
}
|
|
|
|
// Update method to handle movement logic
|
|
public void Update(GameTime gameTime, PlayerInput input, Platform[] platforms)
|
|
{
|
|
// Update player position
|
|
Movement(input);
|
|
|
|
// Update the player's bounding box
|
|
BoundingBox.Update(position, 8, 16);
|
|
|
|
CollisionChecks(platforms);
|
|
}
|
|
|
|
private void Movement(PlayerInput input)
|
|
{
|
|
// Handle jumping
|
|
if (input.KeyJump && grounded)
|
|
{
|
|
grounded = false;
|
|
vsp = vspJump;
|
|
}
|
|
|
|
// Horizontal movement with acceleration + wall jump delay
|
|
wallJumpDelay = Math.Max(wallJumpDelay - 1, 0); // counts down every frame
|
|
|
|
if (wallJumpDelay == 0) // no left-right control until delay is 0, prevents rapid wall climb
|
|
{
|
|
float moveH = input.KeyRight - input.KeyLeft;
|
|
hsp += moveH * hspAcc;
|
|
|
|
if (moveH == 0)
|
|
{
|
|
float hspFrictionFinal = grounded ? hspFrictionGround : hspFrictionAir;
|
|
hsp = Approach(hsp, 0, hspFrictionFinal);
|
|
}
|
|
|
|
hsp = MathHelper.Clamp(hsp, -hspWalk, hspWalk);
|
|
}
|
|
|
|
// Calculate vertical movement
|
|
float grvFinal = grv;
|
|
float vspMaxFinal = vspMax;
|
|
|
|
if (walled && vsp > 0)
|
|
{
|
|
grvFinal = grvWall;
|
|
vspMaxFinal = vspMaxWall;
|
|
}
|
|
vsp += (vsp == 0 ? grvFinal * 2 : grvFinal);
|
|
vsp = MathHelper.Clamp(vsp, vspJump, vspMaxFinal);
|
|
}
|
|
|
|
// Helper method to smoothly approach a target value
|
|
private float Approach(float start, float end, float shift)
|
|
{
|
|
if (start < end)
|
|
{
|
|
start += shift;
|
|
if (start > end)
|
|
return end;
|
|
}
|
|
else
|
|
{
|
|
start -= shift;
|
|
if (start < end)
|
|
return end;
|
|
}
|
|
return start;
|
|
}
|
|
|
|
// Collision Checks - maybe abstract to a class ---------------------------------------------------------------
|
|
private void CollisionChecks(Platform[] platforms)
|
|
{
|
|
// Horizontal collision prediction
|
|
float onePixh = Math.Sign(hsp);
|
|
if (IsColliding(position + new Vector2(hsp, 0), platforms))
|
|
{
|
|
while (!IsColliding(position + new Vector2(onePixh, 0), platforms))
|
|
{
|
|
position.X += onePixh;
|
|
}
|
|
hsp = 0;
|
|
}
|
|
position.X += hsp; // Horizontal move
|
|
|
|
// Vertical collision prediction
|
|
float onePixv = Math.Sign(vsp);
|
|
if (IsColliding(position + new Vector2(0, vsp), platforms))
|
|
{
|
|
while (!IsColliding(position + new Vector2(0, onePixv), platforms))
|
|
{
|
|
position.Y += onePixv;
|
|
}
|
|
vsp = 0;
|
|
}
|
|
position.Y += vsp; // Vertical move
|
|
|
|
// Grounded?
|
|
grounded = IsColliding(position + new Vector2(0, 1), platforms);
|
|
|
|
// Walled?
|
|
walled = WallCheck(platforms);
|
|
if (walled)
|
|
wasWalled = walled;
|
|
}
|
|
|
|
private bool IsColliding(Vector2 newPosition, Platform[] platforms)
|
|
{
|
|
Rectangle newBounds = new Rectangle((int)newPosition.X, (int)newPosition.Y, BoundingBox.Bounds.Width, BoundingBox.Bounds.Height);
|
|
foreach (var platform in platforms)
|
|
{
|
|
if (newBounds.Intersects(platform.BoundingBox.Bounds))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private Platform GetCollidingPlatform(Vector2 newPosition, Platform[] platforms)
|
|
{
|
|
Rectangle newBounds = new Rectangle((int)newPosition.X, (int)newPosition.Y, BoundingBox.Bounds.Width, BoundingBox.Bounds.Height);
|
|
foreach (var platform in platforms)
|
|
{
|
|
if (newBounds.Intersects(platform.BoundingBox.Bounds))
|
|
{
|
|
return platform;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private bool WallCheck(Platform[] platforms)
|
|
{
|
|
if (IsColliding(position + new Vector2(1, 0), platforms) || IsColliding(position + new Vector2(-1, 0), platforms))
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
// Collision checks end ---------------------------------------------------------------------------------------------------------
|
|
|
|
public void Draw(SpriteBatch spriteBatch)
|
|
{
|
|
// Snap position to integer values
|
|
Vector2 drawPosition = new Vector2((int)position.X, (int)position.Y);
|
|
spriteBatch.Draw(texture, drawPosition, Color.White);
|
|
}
|
|
}
|
|
}
|