diff --git a/Tiles/Player.cs b/Tiles/Player.cs index 96d56d4..8f65576 100644 --- a/Tiles/Player.cs +++ b/Tiles/Player.cs @@ -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; @@ -57,25 +58,35 @@ 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; + } + } } } \ No newline at end of file