Super Happy Jumping Pickle Man!

This commit is contained in:
J. M. Ellis
2024-05-27 21:50:09 -07:00
parent 1bfddab1b5
commit 02907cb4b6
11 changed files with 396 additions and 55 deletions
+117 -29
View File
@@ -1,5 +1,6 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace LunaLightXMG
@@ -8,25 +9,28 @@ namespace LunaLightXMG
{
// Declare variables
private float hsp; // Horizontal speed
private float vsp; // Vertical speed
public 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
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
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
// Player initilization
private Texture2D texture;
public Vector2 position;
public BoundingBox BoundingBox { get; private set; }
// Constructor to initialize variables
public Player()
public Player(Vector2 initPosition)
{
// Initialize your variables here
hsp = 0;
@@ -34,32 +38,46 @@ namespace LunaLightXMG
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..
wasWalled = false;
hspAcc = 0.2f;
hspFrictionGround = 0.3f;
hspFrictionAir = 0.3f;
hspWalk = 3f;
grv = 0.2f;
vspMax = 10f;
grvWall = 0.1f;
vspMaxWall = 5f;
vspJump = -10f;
vspJump = -4f;
position = new Vector2(163, 63); // Initialize player position
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)
public void Update(GameTime gameTime, PlayerInput input, Platform[] platforms)
{
// Update player position
Movement(input);
// Update player position based on speed
position.X += hsp;
position.Y += vsp;
// 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
@@ -86,7 +104,6 @@ namespace LunaLightXMG
grvFinal = grvWall;
vspMaxFinal = vspMaxWall;
}
vsp += (vsp == 0 ? grvFinal * 2 : grvFinal);
vsp = MathHelper.Clamp(vsp, vspJump, vspMaxFinal);
}
@@ -109,12 +126,83 @@ namespace LunaLightXMG
return start;
}
public void Draw(SpriteBatch spriteBatch, Texture2D texture)
// 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);
}
}
}