: AyaanScript+ — 2D world demo
: Shows: PNG sprite, scaled drawing, 2D camera follow, color-key transparency.
: Procedurally-placed tiles + animated player sprite.

const W = 800
const H = 600
gui_native "AyaanScript+ - 2D Engine" W H
gui_fps 60

: load sprite (16x16 PNG with magenta corners)
gui_2d_load_image "tile" "demos/assets/sprite_test.png"
gui_2d_color_key "#ff00ff"

const TILE_SIZE = 32
const MAP_W = 20
const MAP_H = 15

set running = 1
set frame = 0
set player_x = 320
set player_y = 240

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

    gui_clear "#1a2030"

    : ===== INPUT =====
    gui_key_down "esc" -> esc
    if esc == 1
        set running = 0
    endif
    gui_key_down "left" -> kl
    gui_key_down "right" -> kr
    gui_key_down "up" -> ku
    gui_key_down "down" -> kd
    if kl == 1
        set player_x = {player_x - 3}
    endif
    if kr == 1
        set player_x = {player_x + 3}
    endif
    if ku == 1
        set player_y = {player_y - 3}
    endif
    if kd == 1
        set player_y = {player_y + 3}
    endif

    : camera follows player (gives feeling of scrolling world)
    gui_2d_camera player_x player_y 1.2

    : draw a procedural tile pattern as "world ground"
    set ty = 0
    repeat MAP_H ty
        set tx = 0
        repeat MAP_W tx
            set ttx = {tx - 1}
            set tty = {ty - 1}
            : Place tile on borders + sparse interior pattern via prime hash
            set mod_check = {(ttx * 7 + tty * 13) - int((ttx * 7 + tty * 13) / 11) * 11}
            set is_border = 0
            if ttx == 0
                set is_border = 1
            endif
            if tty == 0
                set is_border = 1
            endif
            if ttx == 19
                set is_border = 1
            endif
            if tty == 14
                set is_border = 1
            endif
            if is_border == 1
                set wx = {ttx * TILE_SIZE}
                set wy = {tty * TILE_SIZE}
                gui_2d_draw_image_scaled wx wy TILE_SIZE TILE_SIZE "tile"
            endif
            if is_border == 0
                if mod_check < 2
                    set wx = {ttx * TILE_SIZE}
                    set wy = {tty * TILE_SIZE}
                    gui_2d_draw_image_scaled wx wy TILE_SIZE TILE_SIZE "tile"
                endif
            endif
        endrepeat
    endrepeat

    : animated player sprite (bobs up and down)
    set bob = {int(sin(frame * 0.1) * 4)}
    set py_anim = {player_y + bob}
    gui_2d_draw_image_scaled player_x py_anim 48 48 "tile"

    : reset camera for HUD overlay
    gui_2d_camera_reset
    gui_text_big 20 20 "2D ENGINE" "white" 2
    gui_text 20 60 "ARROWS to move  |  camera follows you" "lightgray"
    gui_2d_text_outline 20 80 "PNG + tilemap + color key transparency" "yellow" "black"
    gui_text 20 H - 30 "ESC to quit" "lightgray"

    set frame = {frame + 1}
    gui_update
endwhile

gui_close
