Files
LunaLightXMG/Player.cs
T
2024-05-24 19:22:48 -07:00

133 lines
4.3 KiB
C#

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace LunaLightXMG
{
internal class Player
{
// Declare variables
private float hsp; // Horizontal speed
private float vsp; // Vertical speed
private float wallJumpDelay; // Delay for wall jumping
private bool grounded; // Is the player grounded?
private bool walled; // Is the player wall sliding?
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 position
public Vector2 Position;
// Placeholder for key input states
private bool keyLeft;
private bool keyRight;
// Constructor to initialize variables
public Player()
{
// Initialize your variables here
hsp = 0;
vsp = 0;
wallJumpDelay = 0;
grounded = false;
walled = false;
hspAcc = 0.5f;
hspFrictionGround = 0.2f;
hspFrictionAir = 0.1f;
hspWalk = 4f;
//grv = 0.5f;
grv = 0.0f; // No grav until we have walls, bounding boxes, etc..
vspMax = 10f;
grvWall = 0.1f;
vspMaxWall = 5f;
vspJump = -10f;
Position = new Vector2(163, 63); // Initialize player position
}
// Update method to handle movement logic
public void Update(GameTime gameTime)
{
HandleInput();
Movement();
// Update player position based on speed
Position.X += hsp;
Position.Y += vsp;
}
private void Movement()
{
// 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 = (keyRight ? 1 : 0) - (keyLeft ? 1 : 0);
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;
}
// Placeholder for key input handling (to be connected to actual input handling logic)
private void HandleInput()
{
KeyboardState keyboardState = Keyboard.GetState();
keyLeft = keyboardState.IsKeyDown(Keys.A);
keyRight = keyboardState.IsKeyDown(Keys.D);
}
public void Draw(SpriteBatch spriteBatch, Texture2D texture)
{
spriteBatch.Draw(texture, Position, Color.White);
}
}
}