: AyaanScript+ — 3D Starfield flying past the camera.
: 50 stars with x, y, z. Z decreases each frame; respawn when too close.
: Projects stars onto screen; closer ones are bigger and brighter.

const W = 800
const H = 600
const CX = 400
const CY = 300
gui_native "AyaanScript+ — Starfield" W H
gui_fps 60

set N = 50

: build initial stars: spread in a 3D volume
list xs = []
list ys = []
list zs = []
set i = 0
repeat N i
    rand sx -400 400
    rand sy -300 300
    rand sz 1 80
    push xs sx
    push ys sy
    push zs sz
endrepeat

set speed = 1.5
set running = 1
set frame = 0

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

    gui_clear "#000005"
    gui_text_big 20 20 "STARFIELD" "cyan" 2
    gui_text 20 60 "ESC quits  |  UP/DOWN change speed" "lightgray"

    set i = 0
    repeat N i
        set idx = {i - 1}
        list_at xs idx -> sx
        list_at ys idx -> sy
        list_at zs idx -> sz

        : move toward camera
        set sz = {sz - speed}
        if sz < 1
            : respawn at far distance
            rand sx -400 400
            rand sy -300 300
            set sz = 80
        endif

        : perspective project: screen_x = cx + sx * focal / sz
        set px = {CX + sx * 60 / sz}
        set py = {CY + sy * 60 / sz}

        : size & brightness fall off with distance
        set size = {1 + 10 / sz}
        : color brightness 30..255 based on closeness
        set br = {255 - sz * 3}
        if br < 50
            set br = 50
        endif
        : pack RGB into hex - simple white star scaled by brightness
        set bi = {int(br)}
        set hex = bi
        gui_fill_circle px py size "white"

        : leave a tiny streak behind for fast stars
        if sz < 20
            set tx = {CX + sx * 60 / (sz + 4)}
            set ty = {CY + sy * 60 / (sz + 4)}
            gui_line px py tx ty "lightgray"
        endif

        lset xs idx sx
        lset ys idx sy
        lset zs idx sz
    endrepeat

    gui_key_down "up" -> ku
    gui_key_down "down" -> kd
    gui_key_down "esc" -> esc
    if ku == 1
        set speed = {speed + 0.1}
    endif
    if kd == 1
        set speed = {speed - 0.1}
        if speed < 0.2
            set speed = 0.2
        endif
    endif
    if esc == 1
        set running = 0
    endif

    set frame = {frame + 1}
    gui_update
endwhile

gui_close
