using Raylib_cs;

int screenWidth = 800;
int screenHeight = 600;
int playerSpeed = 7;
int obstacleSpeed = 6;

Raylib.InitWindow(screenWidth, screenHeight, "Dodger");
Raylib.SetTargetFPS(60);

var player = new Rectangle(370, 540, 60, 20);
var obstacle = new Rectangle(Raylib.GetRandomValue(0, 760), -40, 40, 40);
var score = 0;
var gameOver = false;

while (!Raylib.WindowShouldClose())
{
    if (!gameOver)
    {
        if (Raylib.IsKeyDown(KeyboardKey.Left))
        {
            player.X -= playerSpeed;
        }

        if (Raylib.IsKeyDown(KeyboardKey.Right))
        {
            player.X += playerSpeed;
        }

        player.X = Math.Clamp(player.X, 0, screenWidth - player.Width);
        obstacle.Y += obstacleSpeed;

        if (Raylib.CheckCollisionRecs(player, obstacle))
        {
            gameOver = true;
        }
        else if (obstacle.Y > screenHeight)
        {
            obstacle.X = Raylib.GetRandomValue(0, screenWidth - (int)obstacle.Width);
            obstacle.Y = -obstacle.Height;
            score++;
        }
    }
    else if (Raylib.IsKeyPressed(KeyboardKey.Enter))
    {
        player.X = 370;
        obstacle.X = Raylib.GetRandomValue(0, screenWidth - (int)obstacle.Width);
        obstacle.Y = -obstacle.Height;
        score = 0;
        gameOver = false;
    }

    Raylib.BeginDrawing();
    Raylib.ClearBackground(Color.Black);
    Raylib.DrawRectangleRec(player, Color.SkyBlue);
    Raylib.DrawRectangleRec(obstacle, Color.Orange);
    Raylib.DrawText($"Score: {score}", 20, 20, 24, Color.White);

    if (gameOver)
    {
        Raylib.DrawRectangle(0, 0, screenWidth, screenHeight, new Color(0, 0, 0, 180));
        Raylib.DrawText("Game Over", 320, 250, 36, Color.Red);
        Raylib.DrawText("Press Enter to try again", 275, 310, 24, Color.White);
    }

    Raylib.EndDrawing();
}

Raylib.CloseWindow();
