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 Texture2D Texture { get; private set; }
public Vector2 PointerPosition { get; private set; } public Vector2 PointerPosition { get; private set; }
public bool IsColliding { get; private set; } = false; 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; private readonly string _imagePath;
@ -57,25 +58,35 @@ public class Player
{ {
Texture = Raylib.LoadTexture(_imagePath); Texture = Raylib.LoadTexture(_imagePath);
} }
private void HandleGravity()
{
if (IsOnGround) return;
_newPosition.Y += Gravity;
}
private void HandleInput() private void HandleInput()
{ {
_newPosition = Position; _newPosition = Position;
// WASD // 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)) if (Raylib.IsKeyDown(KeyboardKey.A))
{ {
_newPosition += new Vector2(-1, 0) * Speed; _newPosition += new Vector2(-1, 0) * Speed;
_direction = 0; _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)) if (Raylib.IsKeyDown(KeyboardKey.D))
{ {
_newPosition += new Vector2(1, 0) * Speed; _newPosition += new Vector2(1, 0) * Speed;
_direction = 1; _direction = 1;
} }
HandleGravity();
HandleCollision(_newPosition); HandleCollision(_newPosition);
if (!IsColliding) if (!IsColliding)
@ -142,6 +153,7 @@ public class Player
int endY = (int)((newPosition.Y + _currentRect.Height) / tileSize); int endY = (int)((newPosition.Y + _currentRect.Height) / tileSize);
IsColliding = false; IsColliding = false;
IsOnGround = false;
for (int x = startX; x <= endX; x++) 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;
}
}
} }
} }