
// Basic animation variables
static float time = 0.0f;
static float trainPosition = 0.0f;
const float TRAIN_SPEED = 150.0f;
static float moonGlow = 0.0f;
static float moonY = 0.0f;

void RenderScene(); {
    time += ImGui::GetIO().DeltaTime;
    trainPosition += TRAIN_SPEED * ImGui::GetIO().DeltaTime;
    
    // Update moon animations
    moonGlow = 0.5f + 0.5f * sinf(time * 0.5f);
    moonY = 5.0f * sinf(time * 0.2f);

    // Reset train position
    if (trainPosition > ImGui::GetWindowSize().x + 400.0f) {
        trainPosition = -800.0f;  // Increased negative value to account for bogies
    }

    ImDrawList* pDrawList = ImGui::GetWindowDrawList();
    ImVec2 windowPos = ImGui::GetWindowPos();
    ImVec2 windowSize = ImGui::GetWindowSize();

    // Draw moon
    float moonRadius = 40.0f;
    ImVec2 moonCenter(
        windowPos.x + windowSize.x * 0.8f,
        windowPos.y + windowSize.y * 0.2f + moonY
    );

    // Moon glow effect
    for (float r = moonRadius + 30.0f; r >= moonRadius; r -= 1.0f) {
        float alpha = moonGlow * (1.0f - (r - moonRadius) / 30.0f) * 0.3f;
        pDrawList->AddCircleFilled(
            moonCenter,
            r,
            ImGui::GetColorU32(ImVec4(0.9f, 0.9f, 0.7f, alpha))
        );
    }

    // Main moon
    pDrawList->AddCircleFilled(
        moonCenter,
        moonRadius,
        ImGui::GetColorU32(ImVec4(0.95f, 0.95f, 0.8f, 1.0f))
    );

    // Moon craters
    const ImVec2 craters[] = {
        ImVec2(-15, -10), ImVec2(10, -15), ImVec2(5, 5),
        ImVec2(-5, 15), ImVec2(15, 10), ImVec2(-20, 5)
    };

    for (const auto& crater : craters) {
        pDrawList->AddCircleFilled(
            ImVec2(moonCenter.x + crater.x, moonCenter.y + crater.y),
            4.0f,
            ImGui::GetColorU32(ImVec4(0.85f, 0.85f, 0.7f, 1.0f))
        );
    }

    // Add stars around moon
    const int NUM_STARS = 50;
    static std::vector<ImVec2> stars;
    static bool starsInitialized = false;

    // Initialize star positions once
    if (!starsInitialized) {
        stars.clear();
        for (int i = 0; i < NUM_STARS; i++) {
            float angle = (float)i / NUM_STARS * 2 * 3.14159f;
            float distance = 100.0f + rand() % 200;
            stars.push_back(ImVec2(
                moonCenter.x + cosf(angle) * distance,
                moonCenter.y + sinf(angle) * distance
            ));
        }
        starsInitialized = true;
    }

    // Draw stars with twinkling effect
    for (size_t i = 0; i < stars.size(); i++) {
        float starGlow = 0.3f + 0.7f * sinf(time * 2.0f + i * 1.5f);
        float starSize = 2.0f + sinf(time * 3.0f + i) * 0.5f;
        
        pDrawList->AddCircleFilled(
            stars[i],
            starSize,
            ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, starGlow))
        );
    }

    // Draw railway tracks
    float trackY = windowPos.y + windowSize.y * 0.7f;
    
    // Draw track base
    pDrawList->AddRectFilled(
        ImVec2(windowPos.x, trackY - 5),
        ImVec2(windowPos.x + windowSize.x, trackY + 15),
        ImGui::GetColorU32(ImVec4(0.3f, 0.3f, 0.3f, 1.0f))
    );

    // Draw railroad ties
    float tieSpacing = 50.0f;
    float tieOffset = fmodf(time * TRAIN_SPEED, tieSpacing);
    
    for (float x = -tieSpacing + tieOffset; x < windowSize.x; x += tieSpacing) {
        pDrawList->AddRectFilled(
            ImVec2(windowPos.x + x, trackY - 10),
            ImVec2(windowPos.x + x + 30, trackY + 20),
            ImGui::GetColorU32(ImVec4(0.4f, 0.2f, 0.1f, 1.0f))
        );
    }

    // Draw rails
    pDrawList->AddRectFilled(
        ImVec2(windowPos.x, trackY - 2),
        ImVec2(windowPos.x + windowSize.x, trackY),
        ImGui::GetColorU32(ImVec4(0.7f, 0.7f, 0.7f, 1.0f))
    );
    
    pDrawList->AddRectFilled(
        ImVec2(windowPos.x, trackY + 10),
        ImVec2(windowPos.x + windowSize.x, trackY + 12),
        ImGui::GetColorU32(ImVec4(0.7f, 0.7f, 0.7f, 1.0f))
    );

    // Draw Train
    ImVec2 trainPos(windowPos.x + trainPosition, trackY - 60);


// Add train bogies (compartments) BEHIND the locomotive
    const int NUM_BOGIES = 3;
    float bogieWidth = 180.0f;
    float bogieGap = 20.0f;
    float bogieStartX = trainPos.x - bogieWidth - bogieGap;

    for (int i = 0; i < NUM_BOGIES; i++) {
        float bogieX = bogieStartX - (bogieWidth + bogieGap) * i;
        
        // Bogie body
        pDrawList->AddRectFilled(
            ImVec2(bogieX, trainPos.y),
            ImVec2(bogieX + bogieWidth, trainPos.y + 60),
            ImGui::GetColorU32(ImVec4(0.7f, 0.2f, 0.2f, 1.0f))
        );

        // Glowing windows
        float windowGlow = 0.5f + 0.5f * sinf(time * 2.0f + i * 1.5f);
        for (float wx = bogieX + 20; wx < bogieX + bogieWidth - 20; wx += 40) {
            // Window glow effect
            for (float r = 20.0f; r >= 0.0f; r -= 1.0f) {
                float alpha = windowGlow * (1.0f - r / 20.0f) * 0.3f;
                pDrawList->AddRectFilled(
                    ImVec2(wx - r, trainPos.y + 10 - r),
                    ImVec2(wx + 30 + r, trainPos.y + 40 + r),
                    ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 0.8f, alpha))
                );
            }

            // Actual window
            pDrawList->AddRectFilled(
                ImVec2(wx, trainPos.y + 10),
                ImVec2(wx + 30, trainPos.y + 40),
                ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 0.8f, 1.0f))
            );
        }

        // Wheels for each bogie
        float wheelRadius = 15.0f;
        float wheelSpacing = 50.0f;
        
        for (float x = bogieX + 30; x < bogieX + bogieWidth - 30; x += wheelSpacing) {
            pDrawList->AddCircleFilled(
                ImVec2(x, trainPos.y + 55),
                wheelRadius,
                ImGui::GetColorU32(ImVec4(0.1f, 0.1f, 0.1f, 1.0f))
            );
        }
    }
    
    // Locomotive body
    pDrawList->AddRectFilled(
        ImVec2(trainPos.x, trainPos.y),
        ImVec2(trainPos.x + 200, trainPos.y + 60),
        ImGui::GetColorU32(ImVec4(0.2f, 0.3f, 0.8f, 1.0f))
    );

    // Locomotive front
    pDrawList->AddRectFilled(
        ImVec2(trainPos.x + 200, trainPos.y + 10),
        ImVec2(trainPos.x + 240, trainPos.y + 60),
        ImGui::GetColorU32(ImVec4(0.2f, 0.2f, 0.7f, 1.0f))
    );

    // Cabin
    pDrawList->AddRectFilled(
        ImVec2(trainPos.x + 140, trainPos.y - 30),
        ImVec2(trainPos.x + 200, trainPos.y),
        ImGui::GetColorU32(ImVec4(0.2f, 0.2f, 0.7f, 1.0f))
    );

    // Windows
    for (float x = trainPos.x + 20; x < trainPos.x + 120; x += 40) {
        pDrawList->AddRectFilled(
            ImVec2(x, trainPos.y + 10),
            ImVec2(x + 30, trainPos.y + 40),
            ImGui::GetColorU32(ImVec4(0.8f, 0.8f, 1.0f, 1.0f))
        );
    }

    // Chimney
    pDrawList->AddRectFilled(
        ImVec2(trainPos.x + 180, trainPos.y - 50),
        ImVec2(trainPos.x + 195, trainPos.y),
        ImGui::GetColorU32(ImVec4(0.2f, 0.2f, 0.2f, 1.0f))
    );

    // Locomotive wheels
    float wheelRadius = 15.0f;
    float wheelSpacing = 50.0f;
    
    for (float x = trainPos.x + 30; x < trainPos.x + 220; x += wheelSpacing) {
        pDrawList->AddCircleFilled(
            ImVec2(x, trainPos.y + 55),
            wheelRadius,
            ImGui::GetColorU32(ImVec4(0.1f, 0.1f, 0.1f, 1.0f))
        );
    }

    // Smoke animation
    for (int i = 0; i < 5; i++) {
        float smokeOffset = sinf(time * 2.0f + i) * 10.0f;
        float smokeSize = 10.0f + i * 5.0f;
        float smokeAlpha = 0.6f - (i * 0.1f);
        
        pDrawList->AddCircleFilled(
            ImVec2(trainPos.x + 187 + smokeOffset, trainPos.y - 60 - (i * 20)),
            smokeSize,
            ImGui::GetColorU32(ImVec4(0.7f, 0.7f, 0.7f, smokeAlpha))
        );
    }
}