: AyaanScript+ — Advanced 3D scene
: Showcases the upgraded engine:
:   - Multi-light setup (warm key + cool fill + colored rim)
:   - Phong specular highlights
:   - Volumetric fog
:   - Backface culling (default on, big perf win)
:   - New primitives: cylinder, cone, plane
: Hit SPACE to toggle fog. ESC quits.

const W = 800
const H = 800
gui_native "AyaanScript+ - Advanced 3D Scene" W H
gui_fps 60

gui_3d_setup 800 400 400
gui_3d_camera 8
gui_3d_ambient 0.15

: primary "sun" light from upper-left
gui_3d_light -0.4 -0.6 -0.5
: warm fill light from right
gui_3d_add_light 1.0 -0.2 0.0 1.0 0.7 0.4
: cool rim light from behind
gui_3d_add_light 0.0 0.0 1.0 0.3 0.5 1.0

: shiny surfaces
gui_3d_specular 0.6 32

: atmospheric fog
gui_3d_fog 5 18 "#1a1f3a"

set ay = 0
set running = 1
set fog_on = 1
set last_space = 0

while running == 1
    gui_open running
    if running == 0
        break
    endif

    gui_gradient_rect 0 0 W H "#1a1f3a" "#0d1228"
    gui_3d_clear "#1a1f3a"

    set ay = {ay + 0.012}
    gui_3d_rotate 0.25 ay 0

    : ground plane
    gui_3d_plane 0 -1.5 0 8 "#3a4060"

    : centerpiece — cone
    gui_3d_cone 0 -0.5 0 1.0 2.0 24 "#ffaa44"

    : ring of cylinders around it
    set i = 0
    repeat 6 i
        set ang = {(i - 1) * 1.047}
        set px = {cos(ang) * 2.5}
        set pz = {sin(ang) * 2.5}
        set hue = {(i - 1) * 60}
        hsv_to_rgb hue 0.9 1.0 -> col
        gui_3d_cylinder px -1 pz 0.4 1.2 16 col
    endrepeat

    : cubes farther out (will fade into fog)
    set j = 0
    repeat 8 j
        set ang2 = {(j - 1) * 0.785}
        set px2 = {cos(ang2) * 5}
        set pz2 = {sin(ang2) * 5}
        gui_3d_cube px2 -1.2 pz2 0.6 "#aaccff"
    endrepeat

    : floating sphere
    gui_3d_sphere 0 1 0 0.7 14 "#ff5599"

    : HUD
    gui_text_big 20 20 "ADVANCED 3D" "white" 2
    gui_text 20 60 "Multi-light + Phong + Fog + Cull" "lightgray"
    gui_text 20 78 "SPACE toggle fog  |  ESC quit" "lightgray"
    if fog_on == 1
        gui_text 20 96 "fog: ON" "yellow"
    endif
    if fog_on == 0
        gui_text 20 96 "fog: off" "gray"
    endif

    : input — toggle fog on space (edge-triggered)
    gui_key_down "space" -> ks
    if ks == 1
        if last_space == 0
            if fog_on == 1
                gui_3d_no_fog
                set fog_on = 0
            else
                gui_3d_fog 5 18 "#1a1f3a"
                set fog_on = 1
            endif
        endif
        set last_space = 1
    else
        set last_space = 0
    endif

    gui_key_down "esc" -> esc
    if esc == 1
        set running = 0
    endif

    gui_update
endwhile

gui_close
