Skip to content

Instantly share code, notes, and snippets.

@SuperDoxin
Created June 10, 2014 17:39
Show Gist options
  • Save SuperDoxin/ece7733d184515c0743a to your computer and use it in GitHub Desktop.
Save SuperDoxin/ece7733d184515c0743a to your computer and use it in GitHub Desktop.
lua script to show a very basic tutorial in The Powder Toy
function lerp(a,b,t)
return {(a[1]*(1-t))+(b[1]*t),(a[2]*(1-t))+(b[2]*t)}
end
function bezier(p0,p1,p2,p3,t)
local p4=lerp(p0,p1,t)
local p5=lerp(p2,p3,t)
local p6=lerp(p4,p5,t)
return p6
end
function drawbezier(p0,p1,p2,p3,r,g,b)
if r==nil then
r=255
g=255
b=255
end
local pp2=nil
local pp1
for i=0,10 do
t1=i/10
pp1=bezier(p0,p1,p2,p3,t1)
--p2=bezier(p0,p1,p2,p3,t2)
if pp2 then
graphics.drawLine(pp1[1],pp1[2],pp2[1],pp2[2],r,g,b)
end
pp2=pp1
end
end
function drawarrow(s,e,dir,r,g,b)
if r==nil then
r=255
g=255
b=255
end
local dir=math.rad(dir)
local ln=128
local dp={e[1]+math.cos(dir)*ln,e[2]+math.sin(dir)*ln}
drawbezier(s,dp,dp,e,r,g,b)
graphics.drawLine(e[1],e[2],e[1]+math.cos(dir+math.pi*0.15)*16,e[2]+math.sin(dir+math.pi*0.15)*16,r,g,b)
graphics.drawLine(e[1],e[2],e[1]+math.cos(dir-math.pi*0.15)*16,e[2]+math.sin(dir-math.pi*0.15)*16,r,g,b)
end
function drawoutlinearrow(s,e,dir)
for x=-2,2 do
for y=-2,2 do
drawarrow({s[1]+x,s[2]+y},{e[1]+x,e[2]+y},dir,0,0,0)
end
end
drawarrow(s,e,dir)
end
function drawoutlinebezier(p0,p1,p2,p3)
for x=-2,2 do
for y=-2,2 do
drawbezier({p0[1]+x,p0[2]+y},{p1[1]+x,p1[2]+y},{p2[1]+x,p2[2]+y},{p3[1]+x,p3[2]+y},0,0,0)
end
end
drawbezier(p0,p1,p2,p3,255,255,255)
end
function drawfreearrow(p0,p1,p2,p3,r,g,b)
if r==nil then
r=255
g=255
b=255
end
drawbezier(p0,p1,p2,p3,r,g,b)
local sp=bezier(p0,p1,p2,p3,0.9)
local ep=bezier(p0,p1,p2,p3,1)
local dir=math.atan2(sp[2]-ep[2],sp[1]-ep[1])
graphics.drawLine(ep[1],ep[2],ep[1]+math.cos(dir+math.pi*0.15)*16,ep[2]+math.sin(dir+math.pi*0.15)*16,r,g,b)
graphics.drawLine(ep[1],ep[2],ep[1]+math.cos(dir-math.pi*0.15)*16,ep[2]+math.sin(dir-math.pi*0.15)*16,r,g,b)
end
function drawoutlinetext(textpos,text)
for x=-2,2 do
for y=-2,2 do
graphics.drawText(textpos[1]+x,textpos[2]+y,text,0,0,0)
end
end
graphics.drawText(textpos[1],textpos[2],text)
end
function drawinstruction(text,point,dir)
textwidth=graphics.textSize(text)
arrowpos=lerp({314,212},point,0.5)
textpos={arrowpos[1]-textwidth/2,arrowpos[2]-5}
drawoutlinearrow(arrowpos,point,dir)
drawoutlinetext(textpos,text)
end
function hide_all()
for k,v in pairs(tpt.el) do tpt.el[k].enabled = 1 tpt.el[k].menu=0 end
end
function show_all()
for k,v in pairs(tpt.el) do tpt.el[k].enabled = 1 tpt.el[k].menu=1 end
end
function init()
tpt.register_mouseclick(function(mx,my,btn,ev)
end)
end
function lvl01_fire()
sim.loadSave(1558068,1)
hide_all()
tpt.el.plnt.enabled=1
tpt.el.plnt.menu=1
local hadplant=false
stepfunction=function()
if sim.elementCount(tpt.element("firw"))==0 then
drawoutlinetext({314,212},"A winner is you!")
else
if sim.elementCount(tpt.element("plnt"))>1000 or hadplant then
--500,218
drawinstruction("burn",{500,218},270)
hadplant=true
else
if tpt.selectedl=="DEFAULT_PT_PLNT" then
tpt.brushx=32
tpt.brushy=32
drawoutlinetext({314,212},"draw")
drawfreearrow({50,280},{50,0},{550,400},{550,280})
else
if tpt.active_menu()~=9 then
drawinstruction("select the 'solids' menu",{610,302},180)
else
drawinstruction("select the 'PLNT' element",{588,370},270)
end
end
end
end
end
end
stepfunction=nil
do
local modal=Window:new(16,171,596,80)
interface.showWindow(modal)
local b=tpt.el.dust.color%256
local g=bit.rshift(tpt.el.dust.color,8)%256
local r=bit.rshift(tpt.el.dust.color,16)%256
local freeplaybutton=Button:new(4,4,96,32,"\15"..string.char(r,g,b)..string.char(208).." Free Play")
modal:addComponent(freeplaybutton)
freeplaybutton:action(function(sender) interface.closeWindow(modal) end)
local freeplaytext=Label:new(52,4,540,32,"All elements are available from the start.")
modal:addComponent(freeplaytext)
local careerbutton=Button:new(4,44,96,32,"\15"..string.char(1,255,1)..string.char(207).." Career mode")
modal:addComponent(careerbutton)
careerbutton:action(function(sender) interface.closeWindow(modal) init() lvl01_fire() end)
local careertext=Label:new(52,44,540,32,"Unlock elements as you discover them. reccomended for new players")
modal:addComponent(careertext)
tpt.register_step(function()
if stepfunction~=nil then
stepfunction()
end
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment