Whitemambaa

Splash 2.0

Jul 2nd, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1. --By xXxMoNkEyMaNxXx (Whitemambaa)
  2. local arg={
  3.     monitor=term,
  4.     speed=8,
  5.     length=1.2,
  6. }
  7. local argtypes={
  8.     monitor="peripheral",
  9.     speed="number",
  10.     length="number",
  11. }
  12. for parameter,value in table.concat({...}," "):gmatch'%-([%w_]+) ([^ ]+)' do
  13.     local argtype=argtypes[parameter]
  14.     if argtype=="string" then
  15.         arg[parameter]=tostring(value) or arg[parameter]
  16.     elseif argtype=="number" then
  17.         arg[parameter]=tonumber(value) or arg[parameter]
  18.     elseif argtype=="boolean" then
  19.         arg[parameter]=value=="true" or arg[parameter]
  20.     elseif argtype=="peripheral" then
  21.         arg[parameter]=peripheral.wrap(value) or arg[parameter]
  22.     end
  23. end
  24.  
  25. local next=next
  26. local time=os.time
  27. local tick=os.clock
  28.  
  29. local sqrt=math.sqrt
  30. local ceil=math.ceil
  31. local floor=math.floor
  32.  
  33. local match=string.match
  34.  
  35. local remove=table.remove
  36.  
  37. local pull=os.pullEvent
  38. local setAlarm=os.setAlarm
  39.  
  40. local paint=arg.monitor.write
  41. local setPos=arg.monitor.setCursorPos
  42. local setBGC=arg.monitor.setBackgroundColour
  43.  
  44. local palette={colours.black,colours.grey,colours.lightGrey,colours.white}
  45.  
  46. local size={arg.monitor.getSize()}
  47. local gridsize={1.1,0.7}
  48. local maxAge=(sqrt(size[1]*size[1]+size[2]*size[2])+arg.length)/arg.speed
  49. local splashes={}
  50.  
  51. local function draw(t)
  52.     local radii={}
  53.     for i=1,#splashes do
  54.         radii[i]=(t-splashes[i].t)*arg.speed
  55.     end
  56.     for x=1,size[1] do
  57.         for y=1,size[2] do
  58.             local prod=1
  59.             for i=1,#splashes do
  60.                 local s=splashes[i]
  61.                 local dx,dy=(s[1]-x)/gridsize[1],(s[2]-y)/gridsize[2]
  62.                 local d=(radii[i]-sqrt(dx*dx+dy*dy))/arg.length
  63.                 local dsq=d*d
  64.                 if dsq<1 then
  65.                     prod=prod*(1-sqrt(1-dsq))
  66.                 end
  67.             end
  68.             setPos(x,y)
  69.             if prod<1 then
  70.                 setBGC(palette[ceil(#palette*(1-prod))])
  71.             else
  72.                 setBGC(32768)
  73.             end
  74.             paint' '
  75.         end
  76.     end
  77. end
  78.  
  79. do
  80.     arg.monitor.clear()
  81.     local message="Click Anywhere!"
  82.     setPos(floor((size[1]-#message)/2),floor(size[2]/2-0.5))
  83.     paint(message)
  84. end
  85.  
  86. local enabled=true
  87. while enabled do
  88.     local event={pull()}
  89.     local now=tick()
  90.     for i=#splashes,1,-1 do
  91.         if now-splashes[i].t>=maxAge then
  92.             remove(splashes,i)
  93.         end
  94.     end
  95.     if event[1]=="mouse_click" or event[1]=="monitor_touch" then
  96.         if #splashes==0 then
  97.             setAlarm((time()+0.001)%24)
  98.         end
  99.         splashes[#splashes+1]={event[3],event[4],t=now}
  100.     end
  101.     if #splashes>0 then
  102.         draw(now)
  103.         if event[1]=="alarm" then
  104.             setAlarm((time()+0.001)%24)
  105.         end
  106.     end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment