Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created April 21, 2013 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/5428488 to your computer and use it in GitHub Desktop.
Save dermotbalson/5428488 to your computer and use it in GitHub Desktop.
zoom
-- Main
function setup()
numtouches=0 --number of current touches
aim={id=0,x=0,y=0} --details for touch which is aiming
createScope()
CreateBackground()
shots={} --table for shots fired
shotSize=7 --size of shot mark
parameter.integer("Zoom",1,5,2)
shotColour=color(255)
print("Hold and move one finger on the screen to aim")
print("Tap with a second finger to shoot")
end
function CreateBackground()
backdrop=image(WIDTH,HEIGHT)
setContext(backdrop)
img=readImage("Small World:Church")
sprite(img,400,300)
img=readImage("Small World:House White")
sprite(img,250,550)
img=readImage("Small World:Mine Large")
sprite(img,300,150)
img=readImage("Small World:Windmill")
sprite(img,600,400)
setContext()
end
function createScope()
scopeBuffer=90 --draw scope this many pixels above finger (so user can see scope!)
scopeOuterRing=120 --pixels
scopeInnerRing=60
crossHairExtends=20 --crosshairs extend this far outside rings
local s=scopeOuterRing+crossHairExtends*2 --total width
--since we're drawing the same scope a lot, we create an image
imgScope=image(s,s)
setContext(imgScope) --start drawing on the image instead of the screen
ellipseMode(CENTER)
strokeWidth(.5) --lines one pixel wide
stroke(94, 89, 89, 255) --dark colour
fill(0,0,0,0) --transparent fill (the last item, alpha, is nil)
ellipse(s/2,s/2,scopeOuterRing) --draw rings
ellipse(s/2,s/2,scopeInnerRing)
line(s/2,0,s/2,s) --draw crosshairs
line(0,s/2,s,s/2)
setContext() --stop drawing on image
end
function touched(touch)
if touch.state==BEGAN then --user has just touched, hasn't let go yet
numtouches=numtouches+1 --keep count
if numtouches==1 then --if first touch, it is aiming, store details
aim.id=touch.id --so we know which touch is which when we shoot
aim.x=touch.x
aim.y=touch.y
end
elseif touch.state==MOVING then --finger is moving
if touch.id==aim.id then --if the aiming finger, store new x,y
aim.x=touch.x
aim.y=touch.y
end
elseif touch.state==ENDED then
if touch.id==aim.id then aim.id=0 --if aiming finger lifted, remove scope
elseif numtouches>1 then --if more than one finger, bang bang!
shots[#shots+1]={x=aim.x,y=aim.y+scopeBuffer} --add shot to table
end
numtouches=numtouches-1 --adjust touch count
end
end
function draw()
background(195, 211, 215, 255)
pushStyle()
--make copy of background
imgBack=backdrop:copy(0,0,backdrop.width,backdrop.height)
setContext(imgBack)
--draw past shots on screen
for i,v in pairs(shots) do
fill(shotColour)
strokeWidth(0)
ellipse(v.x,v.y,shotSize)
end
if aim.id>0 then --if aiming, draw scope and zoom
local x,y=aim.x, aim.y+scopeBuffer --aiming point
--zoom first
--create new image to contain zoomed image and scope
img=image(imgScope.width,imgScope.height)
s=imgScope.width
setContext(img)
spriteMode(CORNER)
--draw backdrop on our little image so it shows the bit we want
--note zoom factor
sprite(imgBack,-x*Zoom+s/2,-y*Zoom+s/2,Zoom*backdrop.width)
--overlay the scope
sprite(imgScope,0,0)
setContext() --this is needed for some reason before we set it to a different image
setContext(imgBack) --now draw our zoom image onto the background copy
spriteMode(CENTER)
sprite(img,x,y)
end
setContext()
spriteMode(CORNER)
sprite(imgBack,0,0) --draw the background copy with everything on it
popStyle()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment