-- title:   Dark - lighting test
-- author:  pixelbath
-- desc:    lighting test
-- site:    website link
-- license: MIT License
-- version: 0.1
-- script:  lua

local entities = {
    { x=30, y=20, spr=256, w=2, h=2, },
    { x=90, y=60, spr=256, w=2, h=2, },
    { x=150, y=80, spr=258, w=2, h=2, },
}

function get_distance(x1, y1, x2, y2)
    return math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
end

function draw_base()
    local mx,my = mouse()
    for key,val in ipairs(entities) do
        spr(val.spr, val.x, val.y, 0, 1, 0, 0, val.w, val.h)
        for x=0,val.w*8-1 do
            --only do this for filled pixels
            if pix(val.x + x, val.y) == 12 then
                local dist = get_distance(val.x + x, val.y, mx, my)
                
            end
        end
    end
end

-- Step 1: get an SDF working for various sprites
-- Step 2: calculate lighting for each white pixel in sprite by marching from pixel to light(s)
-- Step 3: color any pixel over a lighting threshold

function draw_lighting()
    local mx,my,lmb = mouse()
    if lmb then return end

    for i=0,32265 do
        x=i % 240
        y=i // 240
        if pix(x, y) == 12 and pix(x+1, y-1) == 0 then
            pix(x, y, 2)
        else
            pix(x, y, 0)
        end
    end
end

function TIC()
    cls(0)
    draw_base()
    draw_lighting()
end



-- <SPRITES>
-- 000:00000000000000cc00000ccc0000cccc000ccccc000ccccc000ccccc000ccccc
-- 001:00000000cc000000ccc00000cccc0000ccccc000ccccc000ccccc000ccccc000
-- 002:0000cccc000ccccc000c000c00000000000000000000000000000000000ccc00
-- 003:cc000000cccc0000cccc0000cccc000000ccc000000cccc0000cccc00000ccc0
-- 016:0000cccc00000ccc000000cc0000000000000000000000000000000000000000
-- 017:cccc0000ccc00000cc0000000000000000000000000000000000000000000000
-- 018:000cccc0000cccc0000cccc00000ccc000000000000000000000000000000000
-- 019:00000cc000000cc0ccc00cc0ccc00cc0ccc00cc000000c000000000000000000
-- </SPRITES>

-- <PALETTE>
-- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
-- </PALETTE>

