Files
LunaLightXMG/Player.cs
T

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 hsp_acc; // Horizontal acceleration
private float hsp_friction_ground; // Ground friction
private float hsp_friction_air; // Air friction
private float hsp_walk; // Maximum horizontal speed
private float grv; // Gravity
private float vsp_max; // Maximum vertical speed
private float grv_wall; // Gravity when wall sliding
private float vsp_max_wall; // Maximum vertical speed when wall sliding
private float vsp_jump; // Jump speed
// Player position
public Vector2 Position;
// Placeholder for key input states
private bool key_left;
private bool key_right;
// Constructor to initialize variables
public Player()
{
// Initialize your variables here
hsp = 0;
vsp = 0;
wallJumpDelay = 0;
grounded = false;
walled = false;
hsp_acc = 0.5f;
hsp_friction_ground = 0.2f;
hsp_friction_air = 0.1f;
hsp_walk = 4f;
//grv = 0.5f;
grv = 0.0f; // No grav until we have walls, bounding boxes, etc..
vsp_max = 10f;
grv_wall = 0.1f;
vsp_max_wall = 5f;
vsp_jump = -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 move_h = (key_right ? 1 : 0) - (key_left ? 1 : 0);
hsp += move_h * hsp_acc;
if (move_h == 0)
{
float hsp_friction_final = grounded ? hsp_friction_ground : hsp_friction_air;
hsp = Approach(hsp, 0, hsp_friction_final);
}
hsp = MathHelper.Clamp(hsp, -hsp_walk, hsp_walk);
}
// Calculate vertical movement
float grv_final = grv;
float vsp_max_final = vsp_max;
if (walled && vsp > 0)
{
grv_final = grv_wall;
vsp_max_final = vsp_max_wall;
}
vsp += (vsp == 0 ? grv_final * 2 : grv_final);
vsp = MathHelper.Clamp(vsp, vsp_jump, vsp_max_final);
}
// 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();
key_left = keyboardState.IsKeyDown(Keys.A);
key_right = keyboardState.IsKeyDown(Keys.D);
}
public void Draw(SpriteBatch spriteBatch, Texture2D texture)
{
spriteBatch.Draw(texture, Position, Color.White);
}
}
}