Skip to content

Instantly share code, notes, and snippets.

@interactivenyc
Created April 17, 2016 01:33
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 interactivenyc/408c33d272c88d465519c843ee2336c6 to your computer and use it in GitHub Desktop.
Save interactivenyc/408c33d272c88d465519c843ee2336c6 to your computer and use it in GitHub Desktop.
Top Down Melee
displayMode(FULLSCREEN)
function setup()
sx,sy=WIDTH/2,HEIGHT/2
j,m={},{}
end
function draw()
background(40, 40, 50)
for a,b in pairs(j) do
b:draw()
end
fill(255)
for a,b in pairs(m) do
ellipse(b.x,b.y,5)
b.x=b.x+b.z
b.y=b.y+b.w
if b.x<0 or b.x>WIDTH or b.y<0 or b.y>HEIGHT then
table.remove(m,a)
end
end
sprite("Documents:ExampleCircle2",sx,sy,50)
end
function touched(t)
if t.state==BEGAN then
if t.x<WIDTH/2 then
for a,b in pairs(j) do
if b.type=="move" then
return -- limit 1 move joystick
end
end
table.insert(j,js(t.x,t.y,t.id,"move"))
else
table.insert(j,js(t.x,t.y,t.id,"shoot")) -- allow multiple shoot
end
end
for a,b in pairs(j) do
b:touched(t)
end
end
js=class()
function js:init(x,y,id,ty)
self.id=id
self.ox=x
self.oy=y
self.cx=x
self.cy=y
self.dx=0
self.dy=0
self.type=ty
self.c=20
end
function js:draw()
stroke(255)
strokeWidth(2)
fill(255,255,255,100)
ellipse(self.ox,self.oy,150)
if self.type=="move" then
sx=sx+(self.cx-self.ox)/10
sy=sy+(self.cy-self.oy)/10
end
if self.type=="shoot" then
v1=vec2(self.cx-self.ox,self.cy-self.oy)
v1=v1:normalize()
if math.abs(v1.x+v1.y)>0 then
self.dx=v1.x*8
self.dy=v1.y*8
self.c=self.c+0.7
if self.c>12 then
table.insert(m,vec4(sx,sy,self.dx,self.dy))
self.c=0
end
end
end
end
function js:touched(t)
for a,b in pairs(j) do
if t.id==b.id then
if t.state==MOVING then
b.cx=t.x
b.cy=t.y
end
if t.state==ENDED then
table.remove(j,a)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment