using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; 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; // 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, PlayerInput input) { Movement(input); // Update player position based on speed position.X += hsp; position.Y += vsp; } private void Movement(PlayerInput input) { // 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; } public void Draw(SpriteBatch spriteBatch, Texture2D texture) { // Snap position to integer values Vector2 drawPosition = new Vector2((int)position.X, (int)position.Y); spriteBatch.Draw(texture, drawPosition, Color.White); } } }