basic gravity

This commit is contained in:
Chris Bell 2024-11-20 17:12:16 -06:00
parent f0734e27be
commit 6d9628e7b1

View File

@ -12,7 +12,8 @@ public class Player
public Texture2D Texture { get; private set; }
public Vector2 PointerPosition { get; private set; }
public bool IsColliding { get; private set; } = false;
public bool IsOnGround { get; private set; }
public bool IsOnGround { get; private set; } = false;
public float Gravity { get; set; } = 1f;
private readonly string _imagePath;
@ -58,24 +59,34 @@ public class Player
Texture = Raylib.LoadTexture(_imagePath);
}
private void HandleGravity()
{
if (IsOnGround) return;
_newPosition.Y += Gravity;
}
private void HandleInput()
{
_newPosition = Position;
// WASD
if (Raylib.IsKeyDown(KeyboardKey.W)) _newPosition += new Vector2(0, -1) * Speed;
//if (Raylib.IsKeyDown(KeyboardKey.W)) _newPosition += new Vector2(0, -1) * Speed;
if (Raylib.IsKeyDown(KeyboardKey.A))
{
_newPosition += new Vector2(-1, 0) * Speed;
_direction = 0;
}
if (Raylib.IsKeyDown(KeyboardKey.S)) _newPosition += new Vector2(0, 1) * Speed;
//if (Raylib.IsKeyDown(KeyboardKey.S)) _newPosition += new Vector2(0, 1) * Speed;
if (Raylib.IsKeyDown(KeyboardKey.D))
{
_newPosition += new Vector2(1, 0) * Speed;
_direction = 1;
}
HandleGravity();
HandleCollision(_newPosition);
if (!IsColliding)
@ -142,6 +153,7 @@ public class Player
int endY = (int)((newPosition.Y + _currentRect.Height) / tileSize);
IsColliding = false;
IsOnGround = false;
for (int x = startX; x <= endX; x++)
{
@ -154,5 +166,16 @@ public class Player
}
}
}
// Check if player is on the ground
int groundY = endY + 1;
for (int x = startX; x <= endX; x++)
{
if (Game.Instance.World.GetTile(x, groundY) != 0)
{
IsOnGround = true;
break;
}
}
}
}