lighttest.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. -- title: Dark - lighting test
  2. -- author: pixelbath
  3. -- desc: lighting test
  4. -- site: website link
  5. -- license: MIT License
  6. -- version: 0.1
  7. -- script: lua
  8. local entities = {
  9. { x=30, y=20, spr=256, w=2, h=2, },
  10. { x=90, y=60, spr=256, w=2, h=2, },
  11. { x=150, y=80, spr=258, w=2, h=2, },
  12. }
  13. function get_distance(x1, y1, x2, y2)
  14. return math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
  15. end
  16. function draw_base()
  17. local mx,my = mouse()
  18. for key,val in ipairs(entities) do
  19. spr(val.spr, val.x, val.y, 0, 1, 0, 0, val.w, val.h)
  20. for x=0,val.w*8-1 do
  21. --only do this for filled pixels
  22. if pix(val.x + x, val.y) == 12 then
  23. local dist = get_distance(val.x + x, val.y, mx, my)
  24. end
  25. end
  26. end
  27. end
  28. -- Step 1: get an SDF working for various sprites
  29. -- Step 2: calculate lighting for each white pixel in sprite by marching from pixel to light(s)
  30. -- Step 3: color any pixel over a lighting threshold
  31. function draw_lighting()
  32. local mx,my,lmb = mouse()
  33. if lmb then return end
  34. for i=0,32265 do
  35. x=i % 240
  36. y=i // 240
  37. if pix(x, y) == 12 and pix(x+1, y-1) == 0 then
  38. pix(x, y, 2)
  39. else
  40. pix(x, y, 0)
  41. end
  42. end
  43. end
  44. function TIC()
  45. cls(0)
  46. draw_base()
  47. draw_lighting()
  48. end
  49. -- <SPRITES>
  50. -- 000:00000000000000cc00000ccc0000cccc000ccccc000ccccc000ccccc000ccccc
  51. -- 001:00000000cc000000ccc00000cccc0000ccccc000ccccc000ccccc000ccccc000
  52. -- 002:0000cccc000ccccc000c000c00000000000000000000000000000000000ccc00
  53. -- 003:cc000000cccc0000cccc0000cccc000000ccc000000cccc0000cccc00000ccc0
  54. -- 016:0000cccc00000ccc000000cc0000000000000000000000000000000000000000
  55. -- 017:cccc0000ccc00000cc0000000000000000000000000000000000000000000000
  56. -- 018:000cccc0000cccc0000cccc00000ccc000000000000000000000000000000000
  57. -- 019:00000cc000000cc0ccc00cc0ccc00cc0ccc00cc000000c000000000000000000
  58. -- </SPRITES>
  59. -- <PALETTE>
  60. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  61. -- </PALETTE>