Started work on Player.cs class, converted our OG movement script to C# and implemented as a method in Player class.

This commit is contained in:
J. M. Ellis
2024-05-24 17:48:52 -07:00
parent 7dc1f166ab
commit 34e2d6e6d6
3 changed files with 141 additions and 61 deletions
+9 -20
View File
@@ -21,10 +21,8 @@ namespace LunaLightXMG
Texture2D sprCave1_foreground; Texture2D sprCave1_foreground;
Texture2D sprCave1_walls; Texture2D sprCave1_walls;
// Test player input and movement with abstracted player input class // Test player input and movement with abstracted player class
private PlayerInput playerInput; private Player player;
private Vector2 objectPosition;
private float movementSpeed = 100f; // Movement speed in pixels per second
// Native retro scale // Native retro scale
private int retroWidth = 320; private int retroWidth = 320;
@@ -40,8 +38,8 @@ namespace LunaLightXMG
Window.AllowUserResizing = true; Window.AllowUserResizing = true;
IsMouseVisible = true; IsMouseVisible = true;
// Test player input and movement with abstracted player input class // Test player input and movement with abstracted player class
playerInput = new PlayerInput(); player = new Player();
} }
private void OnClientSizeChanged(object sender, EventArgs eventArgs) private void OnClientSizeChanged(object sender, EventArgs eventArgs)
@@ -61,11 +59,8 @@ namespace LunaLightXMG
graphics.PreferredBackBufferHeight = 1080; graphics.PreferredBackBufferHeight = 1080;
graphics.ApplyChanges(); graphics.ApplyChanges();
// Test player input and movement with abstracted player input class
objectPosition = new Vector2(163, 63); // Initial position
base.Initialize(); base.Initialize();
CalculateRenderDestination(); // Jensen can implement CalculateRenderDestination();
} }
private void CalculateRenderDestination() private void CalculateRenderDestination()
@@ -100,14 +95,8 @@ namespace LunaLightXMG
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit(); Exit();
playerInput.Update(); // Test player input and movement with abstracted player class
player.Update(gameTime);
// Test player input and movement with abstracted player input class
// Update the object's position based on input
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
objectPosition = playerInput.MoveWithWASD(objectPosition, movementSpeed * deltaTime);
base.Update(gameTime); base.Update(gameTime);
} }
@@ -124,8 +113,8 @@ namespace LunaLightXMG
// Draw sprites here // Draw sprites here
spriteBatch.Draw(sprCave1_background,new Vector2(0,0),Color.White); spriteBatch.Draw(sprCave1_background,new Vector2(0,0),Color.White);
// Test player input and movement with abstracted player input class // Test player input and movement with abstracted player class
spriteBatch.Draw(sprRo, objectPosition, Color.White); player.Draw(spriteBatch, sprRo);
spriteBatch.Draw(sprCave1_walls, new Vector2(0, 0), Color.White); spriteBatch.Draw(sprCave1_walls, new Vector2(0, 0), Color.White);
spriteBatch.Draw(sprCave1_foreground, new Vector2(0, 0), Color.White); spriteBatch.Draw(sprCave1_foreground, new Vector2(0, 0), Color.White);
+132
View File
@@ -0,0 +1,132 @@
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);
}
}
}
-41
View File
@@ -1,41 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace LunaLightXMG
{
internal class PlayerInput
{
// Field to store the current keyboard state
private KeyboardState currentKeyboardState;
// Method to update the input state
public void Update()
{
currentKeyboardState = Keyboard.GetState();
}
// Method to check if a key is currently held down
public bool IsKeyDown(Keys key)
{
return currentKeyboardState.IsKeyDown(key);
}
// Method to check WASD movement keys
public Vector2 MoveWithWASD(Vector2 currentPosition, float speed)
{
Vector2 movement = Vector2.Zero;
if (IsKeyDown(Keys.W))
movement.Y -= speed;
if (IsKeyDown(Keys.A))
movement.X -= speed;
if (IsKeyDown(Keys.S))
movement.Y += speed;
if (IsKeyDown(Keys.D))
movement.X += speed;
return currentPosition + movement;
}
}
}