mirror of
https://github.com/TheRavenwoodArts/LunaLightXMG.git
synced 2026-07-30 22:55:03 -07:00
Super happy jumping pickle man (#3)
* Added enemy class * Added enemy class and fixed DumpFraction bug. * Update README.md * Fixed enemy movement bug, added game music. * Fixed bug: Background music looping. * Fixed enemey getting stuck on wall. Code reorganization * Minor updates
This commit is contained in:
@@ -5,9 +5,8 @@ using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace LunaLightXMG
|
||||
{
|
||||
internal class Player
|
||||
public class Player
|
||||
{
|
||||
// Declare variables
|
||||
private float hsp; // Horizontal speed
|
||||
public float vsp; // Vertical speed
|
||||
private float hsp_frac; // Hold fractional position data
|
||||
@@ -25,16 +24,13 @@ namespace LunaLightXMG
|
||||
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; }
|
||||
public BoundingBox boundingBox { get; private set; }
|
||||
private CollisionManager collisionManager;
|
||||
|
||||
// Constructor to initialize variables
|
||||
public Player(Vector2 initPosition)
|
||||
{
|
||||
// Initialize your variables here
|
||||
hsp = 0;
|
||||
vsp = 0;
|
||||
hsp_frac = 0;
|
||||
@@ -52,34 +48,36 @@ namespace LunaLightXMG
|
||||
grvWall = 0.1f;
|
||||
vspMaxWall = 5f;
|
||||
vspJump = -4f;
|
||||
|
||||
position = initPosition;
|
||||
BoundingBox = new BoundingBox(new Rectangle((int)position.X, (int)position.Y, 8, 16));
|
||||
boundingBox = new BoundingBox(new Rectangle((int)position.X, (int)position.Y, 8, 16));
|
||||
collisionManager = new CollisionManager();
|
||||
}
|
||||
|
||||
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);
|
||||
// Dump fractional position data.
|
||||
DumpFractions();
|
||||
// Update the player's bounding box
|
||||
BoundingBox.Update(position, 8, 16);
|
||||
|
||||
boundingBox.Update(position, boundingBox.Bounds.Width, boundingBox.Bounds.Height);
|
||||
DumpFractions();
|
||||
CollisionChecks(platforms);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Method Implementation -------------------------------------------------------------------------------
|
||||
private void Movement(PlayerInput input)
|
||||
{
|
||||
// Handle jumping
|
||||
if (input.KeyJump && grounded)
|
||||
{
|
||||
grounded = false;
|
||||
vsp = vspJump;
|
||||
}
|
||||
|
||||
@@ -113,7 +111,41 @@ namespace LunaLightXMG
|
||||
vsp = MathHelper.Clamp(vsp, vspJump, vspMaxFinal);
|
||||
}
|
||||
|
||||
// Helper method to smoothly approach a target value
|
||||
private void CollisionChecks(Platform[] platforms)
|
||||
{
|
||||
// Horizontal collision prediction
|
||||
float onePixh = Math.Sign(hsp);
|
||||
if (collisionManager.IsColliding(position + new Vector2(hsp, 0), boundingBox, platforms))
|
||||
{
|
||||
while (!collisionManager.IsColliding(position + new Vector2(onePixh, 0), boundingBox, platforms))
|
||||
{
|
||||
position.X += onePixh;
|
||||
}
|
||||
hsp = 0;
|
||||
}
|
||||
position.X += hsp; // Horizontal move
|
||||
|
||||
// Vertical collision prediction
|
||||
float onePixv = Math.Sign(vsp);
|
||||
if (collisionManager.IsColliding(position + new Vector2(0, vsp), boundingBox, platforms))
|
||||
{
|
||||
while (!collisionManager.IsColliding(position + new Vector2(0, onePixv), boundingBox, platforms))
|
||||
{
|
||||
position.Y += onePixv;
|
||||
}
|
||||
vsp = 0;
|
||||
}
|
||||
position.Y += vsp; // Vertical move
|
||||
|
||||
// Grounded?
|
||||
grounded = collisionManager.IsColliding(position + new Vector2(0, 1), boundingBox, platforms);
|
||||
|
||||
// Walled?
|
||||
walled = collisionManager.WallCheck(position, boundingBox, platforms);
|
||||
if (walled)
|
||||
wasWalled = walled;
|
||||
}
|
||||
|
||||
private float Approach(float start, float end, float shift)
|
||||
{
|
||||
if (start < end)
|
||||
@@ -133,92 +165,23 @@ namespace LunaLightXMG
|
||||
|
||||
private void DumpFractions()
|
||||
{
|
||||
// Movement Speeds
|
||||
hsp += hsp_frac;
|
||||
vsp += vsp_frac;
|
||||
hsp_frac = hsp - (float)Math.Floor(hsp);
|
||||
vsp_frac = vsp - (float)Math.Floor(vsp);
|
||||
hsp -= hsp_frac;
|
||||
vsp -= vsp_frac;
|
||||
}
|
||||
|
||||
// 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))
|
||||
if (grounded)
|
||||
{
|
||||
while (!IsColliding(position + new Vector2(onePixh, 0), platforms))
|
||||
{
|
||||
position.X += onePixh;
|
||||
}
|
||||
hsp = 0;
|
||||
float adjustedPosX = (float)Math.Round(position.X);
|
||||
float adjustedPosY = (float)Math.Round(position.Y);
|
||||
position.X = adjustedPosX;
|
||||
position.Y = adjustedPosY;
|
||||
}
|
||||
position.X += hsp; // Horizontal move
|
||||
|
||||
// Vertical collision prediction
|
||||
float onePixv = Math.Sign(vsp);
|
||||
if (IsColliding(position + new Vector2(0, vsp), platforms))
|
||||
if (vsp >= 0)
|
||||
{
|
||||
while (!IsColliding(position + new Vector2(0, onePixv), platforms))
|
||||
{
|
||||
position.Y += onePixv;
|
||||
}
|
||||
vsp = 0;
|
||||
hsp += hsp_frac;
|
||||
vsp += vsp_frac;
|
||||
hsp_frac = hsp - (float)Math.Floor(hsp);
|
||||
vsp_frac = vsp - (float)Math.Floor(vsp);
|
||||
hsp -= hsp_frac;
|
||||
vsp -= vsp_frac;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user