: AyaanScript+ — Classic animated plasma effect with a 16-color palette.
: 40x30 grid of 20x20 cells. Maps a sin/cos plasma function to palette indices.
: Smooth, hypnotic, demoscene-style visuals.

const W = 800
const H = 600
const CELL = 20
const COLS = 40
const ROWS = 30
gui_native "AyaanScript+ — Plasma" W H
gui_fps 30

: rainbow palette
list pal = ["#1a0033", "#2d0066", "#440099", "#5500cc", "#3300ff", "#0033ff", "#0099ff", "#00ccff", "#00ffcc", "#00ff66", "#33ff00", "#ccff00", "#ffcc00", "#ff6600", "#ff0033", "#cc0099"]

set running = 1
set t = 0
set PI = 3.14159265

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

    set cy = 0
    repeat ROWS cy
        set cx = 0
        repeat COLS cx
            set fx = {cx / 8}
            set fy = {cy / 8}
            set v1 = {sin(fx + t)}
            set v2 = {sin(fy + t * 0.7)}
            set v3 = {sin((fx + fy + t * 1.3) / 2)}
            set v = {(v1 + v2 + v3) / 3}
            : v is in [-1, 1]; map to 0..15
            set idx = {int((v + 1) * 7.5)}
            if idx < 0
                set idx = 0
            endif
            if idx > 15
                set idx = 15
            endif
            list_at pal idx -> col
            set px = {(cx - 1) * CELL}
            set py = {(cy - 1) * CELL}
            gui_fill_rect px py CELL CELL col
        endrepeat
    endrepeat

    : title overlay (drawn on top each frame)
    gui_text_big 20 20 "PLASMA" "white" 3
    gui_text 20 70 "ESC quits  |  AyaanScript+ demoscene" "white"

    set t = {t + 0.05}

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

    gui_update
endwhile

gui_close
