Here’s an updated version
-- destructable terrain in a large level
--by West
-- Use this function to perform your initial setup
local numObjs = 50
local obj
function setup()
displayMode(FULLSCREEN)
globalx=0
globaly=0
levelmaxx=10000
levelmaxy=2000
globalxspd=0
globalyspd=0
globalxacc=0
globalyacc=0
mapx=550
mapy=20
mapw=WIDTH-600
maph=60
touchdeceleration=0.95
mainx=10
mainw=WIDTH-2*mainx
mainy=100
mainh=HEIGHT-110
touches={}
tsup={} --tsup contains the supplementary info about the start position of the touch
points={}
pulse={}
parameter.boolean("includeTarget",false)
parameter.boolean("includeTrail",false)
parameter.boolean("includeStart",false)
parameter.boolean("includeDuration",false)
parameter.boolean("includeHistory",false)
parameter.boolean("joinHistory",false)
parameter.boolean("includePulse",false)
parameter.boolean("includeClassification",false)
obj={}
for i=1,numObjs do
table.insert(obj, {x=math.random(levelmaxx),y=math.random(levelmaxy),w=50+math.random(50),h=50+math.random(50),img=readImage("Cargo Bot:Crate Green "..math.random(3))})
end
for c=0,levelmaxx,100 do
table.insert(obj,{x=c,y=100,w=100,h=100,img=readImage("Cargo Bot:Crate Yellow "..math.random(3))})
end
--create hole image
holeimg=image(50,50)
setContext(holeimg)
fill(0)
ellipse(25,25,50)
setContext()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(0,0,0)
globalxspd = globalxspd *touchdeceleration
globalyspd=globalyspd*touchdeceleration
if math.abs(globalxspd)<0.1 then globalxspd=0 end
if math.abs(globalyspd)<0.1 then globalyspd=0 end
globalx = globalx + globalxspd
globaly = globaly + globalyspd
if globalx>levelmaxx-WIDTH then
globalx=levelmaxx-WIDTH
end
if globalx<0 then
globalx=0
end
if globaly>levelmaxy-HEIGHT then
globaly=levelmaxy-HEIGHT
end
if globaly<0 then
globaly=0
end
for i=1,#obj do
--check a certain point in the screen coords poix,poiy
local poix=WIDTH/2
local poiy=HEIGHT/2
if poix>(obj[i].x-globalx)-obj[i].w/2 and poix<(obj[i].x-globalx)+obj[i].w/2
and poiy>(obj[i].y-globaly)-obj[i].h/2 and poiy<(obj[i].y-globaly)+obj[i].h/2 then
local b1r,b1g,b1b,b1a=obj[i].img:get(math.floor((poix-(obj[i].x-globalx)+obj[i].w/2)*obj[i].img.width/obj[i].w+1),math.floor((poiy-(obj[i].y-globaly)+obj[i].h/2)*obj[i].img.height/obj[i].h+1))
end
-- only render if its onscreen
if (obj[i].x-globalx)+obj[i].w>mainx and (obj[i].x-globalx)-obj[i].w<(mainx+mainw)
and (obj[i].y-globaly)+obj[i].h>mainy and (obj[i].y-globaly)-obj[i].h<(mainy+mainh) then
sprite(obj[i].img,obj[i].x-globalx,obj[i].y-globaly,obj[i].w,obj[i].h)
end
if math.abs(CurrentTouch.x-(obj[i].x-globalx))<obj[i].w and math.abs(CurrentTouch.y-(obj[i].y-globaly))<obj[i].h then
setContext(obj[i].img)
--sprite("Dropbox:Hole",(CurrentTouch.x-(obj[i].x-globalx)+obj[i].w/2)*obj[i].img.width/obj[i].w,(CurrentTouch.y-(obj[i].y-globaly)+obj[i].h/2)*obj[i].img.height/obj[i].h,20*obj[i].img.width/obj[i].w,20*obj[i].img.height/obj[i].h)
sprite(holeimg,(CurrentTouch.x-(obj[i].x-globalx)+obj[i].w/2)*obj[i].img.width/obj[i].w,(CurrentTouch.y-(obj[i].y-globaly)+obj[i].h/2)*obj[i].img.height/obj[i].h,20*obj[i].img.width/obj[i].w,20*obj[i].img.height/obj[i].h)
setContext()
end
end
--draw surround
fill(50)
rect(-1,0,mainx,HEIGHT)
rect(mainx+mainw,0,WIDTH-mainx-mainw+1,HEIGHT)
rect(0,-1,WIDTH,mainy)
rect(0,mainy+mainh,WIDTH,HEIGHT-mainy-mainh+1)
--draw map
fill(0)
rect(mapx,mapy,mapw,maph)
for i=1,#obj do
local px=mapx+mapw*(obj[i].x)/(levelmaxx)
local py=mapy+maph*obj[i].y/(levelmaxy)
sprite(obj[i].img,px,py,5,5)
end
--draw map reticle
noFill()
strokeWidth(3)
stroke(0,0,255)
local leftedge=mapx+(mapw)*(globalx)/(levelmaxx)
local rightedge=mapx+(mapw)*(mainw+globalx)/(levelmaxx)
local bottom=mapy+(maph)*globaly/levelmaxy
local top=mapy+(maph)*(mainh+globaly)/levelmaxy
line(leftedge,bottom,leftedge,top)
line(rightedge,bottom,rightedge,top)
line(rightedge,bottom,leftedge,bottom)
line(rightedge,top,leftedge,top)
noStroke()
--move screen via map
strokeWidth(2)
for i,t in pairs(touches) do
if t.x>mapx and t.x<(mapx+mapw) and t.y>mapy and t.y<mapy+maph then
globalx=(levelmaxx*(t.x-mapx)/(mapw))-WIDTH/2
globaly=(levelmaxy*(t.y-(mapy))/maph)-HEIGHT/2
globalxspd=0
globalyspd=0
end
fill(255)
stroke(255)
if includeStart==true then
line(tsup[i].tstartx,tsup[i].tstarty,t.x,t.y)
ellipse(tsup[i].tstartx,tsup[i].tstarty,10)
end
fill(255)
if includeDuration==true then
local formattime=math.floor(10*(ElapsedTime-tsup[i].starttime))/10
text(formattime,t.x,t.y+50)
end
ellipse(t.x,t.y,10)
if includeTarget==true then
local endpt=math.max(1200-800*(ElapsedTime-tsup[i].starttime),50+10*math.sin(5*ElapsedTime))
local spin=ElapsedTime*40
pushMatrix()
translate(t.x,t.y)
rotate(spin)
translate(-endpt,0)
sprite("Cargo Bot:How Arrow",0,0)
translate(endpt,0)
rotate(90)
translate(-endpt,0)
sprite("Cargo Bot:How Arrow",0,0)
translate(endpt,0)
rotate(90)
translate(-endpt,0)
sprite("Cargo Bot:How Arrow",0,0)
translate(endpt,0)
rotate(90)
translate(-endpt,0)
sprite("Cargo Bot:How Arrow",0,0)
translate(endpt,0)
popMatrix()
end
--draw path of touch
if includeTrail==true then
fadespeed=500
for j,p in pairs(tsup[i].path) do
trailfade=math.max((p.age-ElapsedTime)*fadespeed,-255)
stroke(255,255,255,255+trailfade)
fill(255,255,255,255+trailfade)
if trailfade>-255 then
-- ellipse(p.pos.x,p.pos.y,5)
if j>1 then
line(prevx,prevy,p.pos.x,p.pos.y)
end
end
prevx=p.pos.x
prevy=p.pos.y
end
end
end
noStroke()
end
function touched(touch)
if touch.state==MOVING then
--record path
if tsup[touch.id]~=nil then
table.insert(tsup[touch.id].path,{pos=vec2(touch.x,touch.y),age=ElapsedTime})
end
end
if touch.state==ENDED or touch.state==CANCELLED then
processTouch(touch)
touches[touch.id] = nil
tsup[touch.id]=nil
else
touches[touch.id] = touch
--if there is no supplementary info associated with the current touch then add it
if tsup[touch.id]==nil then
tsup[touch.id]={tstartx=touch.x,tstarty=touch.y,starttime=ElapsedTime,path={}}
end
end
end
function processTouch(touch)
if #tsup[touch.id].path>1 then
if tsup[touch.id].path[#tsup[touch.id].path].pos:dist(tsup[touch.id].path[#tsup[touch.id].path-1].pos)>1 then
globalxspd=-2*(tsup[touch.id].path[#tsup[touch.id].path].pos.x-tsup[touch.id].path[#tsup[touch.id].path-1].pos.x)
globalyspd=-2*(tsup[touch.id].path[#tsup[touch.id].path].pos.y-tsup[touch.id].path[#tsup[touch.id].path-1].pos.y)
end
-- print(tsup[touch.id].path[#tsup[touch.id].path].pos.x)
end
if ElapsedTime-tsup[touch.id].starttime<0.2 then
--very short event
if tsup[touch.id]==nil or vec2(touch.x,touch.y):dist(vec2(tsup[touch.id].tstartx,tsup[touch.id].tstarty))<10 then
--tap
--maybe check in window
globalxspd=0
globalyspd=0
end
end
if includeHistory==true then
table.insert(points,vec2(touch.x,touch.y)) --add a point to the points table
end
if includePulse==true then
table.insert(pulse,{x=touch.x,y=touch.y,r=8,rate=1}) --add a new pulse
end
if includeClassification==true then
if ElapsedTime-tsup[touch.id].starttime<0.2 then
--very short event
if tsup[touch.id]==nil then
print("tap")
elseif vec2(touch.x,touch.y):dist(vec2(tsup[touch.id].tstartx,tsup[touch.id].tstarty))<10 then
print("tap")
else
print("dash")
end
elseif ElapsedTime-tsup[touch.id].starttime<1 then
--a slightly longer gesture
if touch.x>tsup[touch.id].tstartx+25 and math.abs(touch.y-tsup[touch.id].tstarty)<50 then
print("swipe right")
elseif touch.x<tsup[touch.id].tstartx-25 and math.abs(touch.y-tsup[touch.id].tstarty)<50 then
print("swipe left")
elseif touch.y>tsup[touch.id].tstarty+25 and math.abs(touch.x-tsup[touch.id].tstartx)<50 then
print("swipe up")
elseif touch.y<tsup[touch.id].tstarty-25 and math.abs(touch.x-tsup[touch.id].tstartx)<50 then
print("swipe down")
end
end
end
end