Skip to content

Instantly share code, notes, and snippets.

@monkeyman32123
Last active August 29, 2015 13:57
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 monkeyman32123/d3541d72a11f7aab7d62 to your computer and use it in GitHub Desktop.
Save monkeyman32123/d3541d72a11f7aab7d62 to your computer and use it in GitHub Desktop.
V1.0
--# Main
displayMode(OVERLAY)
supportedOrientations(LANDSCAPE_ANY)
function setup()
lightsout = false
drawguides = false
parameter.action("light switch",function() lightsout = not lightsout, fl:calcshadows() end)
parameter.action("edge smoothing",function() drawguides = not drawguides end)
parameter.action("drawprobelines",function() drawmeshcalclines = not drawmeshcalclines end)
smooth()
allupdated = false
perworldwidth = 768
movementfactor = perworldwidth/16
origin = vec2((WIDTH/2)-(perworldwidth/2)-1,(HEIGHT/2)-(perworldwidth/2)-1)
worldmap = worldmaps[2]
numwid = math.sqrt(#worldmap)
worlds={}
for i=0, numwid-1 do
for b=0,numwid-1 do
worlds[b+(i*numwid)] = World(worldmap[(b+(i*numwid))+1],vec2((WIDTH/2)+(b*perworldwidth)-1,(HEIGHT/2)+(i*perworldwidth)-1),perworldwidth)
end
end
for i = 0, #worlds do
worlds[i]:m2(vec2(-perworldwidth*math.floor(numwid/2),-perworldwidth*math.floor(numwid/2)))
end
j = Joystiqs()
hero = Hero()
rotangle = 0
r=0
g=255
b=255
fl = Flashlight()
end
function draw()
background(0, 0, 0, 255)
j:j1Calc()
j:j2Calc()
if j:getmag() ~= nil and j:getang() ~= nil then
hero:m1(j:getmag(),j:getang())
end
local jork = j:getang2()
if jork ~= nil then
hero:changeang(jork)
fl:changeang(jork)
elseif j:getang()~= nil and jork == nil then
fl:changeang(j:getang())
end
for i=0,#worlds do
worlds[i]:draw()
end
if lightsout then
fl:draw()
end
hero:draw()
j:draw()
fill(0, 0, 255, 255)
text(1/DeltaTime,WIDTH/2,HEIGHT-25)
j:cleanJoystickData()
end
function touched(touch)
j:touched(touch)
end
worldmaps = {
{9,2,2,2,6,
5,1,1,1,3,
5,1,1,1,3,
5,1,1,1,3,
8,4,4,4,7},
{9,2,2,2,2,2,6,
5,1,1,1,1,1,3,
5,1,1,1,1,1,3,
5,1,1,1,1,1,3,
5,1,1,1,1,1,3,
5,1,1,1,1,1,3,
8,4,4,4,4,4,7}
}
--# World
World = class()
function World:init(lvl, loc, wid)
self.loc = vec2(loc.x-wid/2,loc.y-wid/2)
self.leveltiles = levels[lvl]
self.leveltileslocs = {}
local numdir = math.sqrt(#self.leveltiles)
self.numdir = numdir
self.widhei = wid/numdir
for i=0, numdir-1 do
for b=0, numdir do
self.leveltileslocs[b+(i*numdir)] = vec2(b*self.widhei,i*self.widhei)
end
end
self.negpoints = nil
self.negblocks = nil
table.remove(self.leveltileslocs,#self.leveltileslocs)
self.active = nil
self:isActive()
self.xmove = true
self.ymove = true
self.add = nil
self.collided = false
self.visible = true
self.offs = vec2(0,0)
end
function World:draw()
if self.visible then
rectMode(CORNER)
strokeWidth(0)
if self:isActive() then
if self.loc.y ~= origin.y-0 or self.loc.x ~= origin.x-0 then
self:move()
end
for i=0, #self.leveltileslocs do
if self.leveltiles[i+1] == 0 then
fill(255,255,255,255)
--fill(r,g,b)
else
fill(0,0,0,255)
--fill(col)
end
rect(self.leveltileslocs[i].x+self.loc.x,self.leveltileslocs[i].y+self.loc.y,self.widhei+2,self.widhei+2)
end
if self.negpoints ~= nil then
fill(255,0,0,255)
strokeWidth()
for i=0, #self.negpoints do
for b=1,4 do
--ellipse(self.negpoints[i][b].x,self.negpoints[i][b].y,2)
end
end
end
if self.negpoints == nil then
if self.loc == origin then
self:calcnegativepoints()
end
end
else
fill(0,0,0,255)
rect(self.loc.x,self.loc.y,self.widhei*self.numdir+2,self.widhei*self.numdir+2)
end
end
end
function World:touched(touch)
end
function World:isVisible()
end
function World:isActive()
if hero ~= nil then
if heroloc.x > self.loc.x and heroloc.x < self.loc.x+(self.numdir*self.widhei) then
if heroloc.y > self.loc.y and heroloc.y < self.loc.y+(self.numdir*self.widhei) then
self.active = true
return true
else
self.active = false
end
else
self.active = false
end
self.negpoints = nil
return false
end
end
function World:calcnegativepoints()
if self.negblocks == nil then
self.negblocks = {}
local ident = 0
for b=0, #self.leveltiles do
if self.leveltiles[b] == 1 then
self.negblocks[ident] = b
ident = ident + 1
end
end
end
self.negpoints = {}
for i = 0, #self.negblocks do
local p1 = self.leveltileslocs[(self.negblocks[i]-1)]+self.loc+vec2(.5,.5)
--p1 = self.leveltileslocs[i-1]+self.loc
local p2 = vec2(p1.x,p1.y+self.widhei)
local p3 = vec2(p2.x+self.widhei,p2.y)
local p4 = vec2(p1.x+self.widhei,p1.y)
local edgel = p1.x
local edger = p3.x
local edgeu = p2.y
local edged = p4.y
local tbl = {p1,p2,p3,p4,edgel,edger,edgeu,edged}
self.negpoints[i] = tbl
end
end
function World:move()
local add = vec2(0,0)
if self.loc.x-(origin.x-0) > 0 then
add.x=-movementfactor
end
if self.loc.x-(origin.x-0) < 0 then
add.x=movementfactor
end
if self.loc.y-(origin.y-0) > 0 then
add.y=-movementfactor
end
if self.loc.y-(origin.y-0) < 0 then
add.y=movementfactor
end
for i=0,#worlds do
worlds[i]:m2(add)
end
hero:m2(add)
end
function World:m2(add)
self.loc = self.loc + add
end
function World:colliding(corner, prev)
end
levels = {
{1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,1,1,0,1,0,1,0,1,1,0,0,1,
1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,
1,0,0,0,0,1,1,0,1,1,0,0,0,0,1,
1,0,0,1,0,1,0,0,0,1,0,1,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,1,0,1,0,0,0,1,0,1,0,0,1,
1,0,0,0,0,1,1,0,1,1,0,0,0,0,1,
1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,
1,0,0,1,1,0,1,0,1,0,1,1,0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0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
}
--# Hero
Hero = class()
function Hero:init(x)
heroloc = vec2(WIDTH/2,HEIGHT/2)
herosize = 20
col = color(255,0,0,255)
blend = 0
red = true
orange = false
yellow = false
green = false
teal = false
blue = false
purple = false
violet = false
end
function Hero:draw()
rectMode(CENTER)
pushMatrix()
translate(heroloc.x,heroloc.y)
rotate(math.deg(rotangle)-90)
fill(col)
strokeWidth(2)
r = 127.5+(127.5-col.r)
g = 127.5+(127.5-col.g)
b = 127.5+(127.5-col.b)
stroke(r,g,b,255)
self:colorCycle()
ellipse(0,0,herosize+4,herosize+4)
line(0,0,0,herosize/2)
popMatrix()
end
function Hero:m1(mag,ang)
local add = vec2(mag/20,0):rotate(ang)
heroloc = heroloc + add
rotangle=ang
for i = 0, #worlds do
if worlds[i].active == true then
for b = 0, #worlds[i].leveltileslocs do
if worlds[i].leveltiles[b+1] == 1 then
if (worlds[i].leveltileslocs[b]+worlds[i].loc):dist(heroloc)<worlds[i].widhei*2 then
local centx = worlds[i].loc.x+worlds[i].leveltileslocs[b].x+(worlds[i].widhei/2)
local edgexr = worlds[i].loc.x+worlds[i].leveltileslocs[b].x+worlds[i].widhei
local edgexl = worlds[i].loc.x+worlds[i].leveltileslocs[b].x
local centy = worlds[i].loc.y+worlds[i].leveltileslocs[b].y+(worlds[i].widhei/2)
local edgeyu = worlds[i].loc.y+worlds[i].leveltileslocs[b].y+worlds[i].widhei
local edgeyd = worlds[i].loc.y+worlds[i].leveltileslocs[b].y
local ang = math.deg(math.atan2(centx-heroloc.x,centy-heroloc.y))
if ang > 45 and ang < 135 then
if heroloc.x+herosize/2>edgexl then
heroloc.x = edgexl-herosize/2
end
end
if ang >= 135 or ang <= -135 then
if heroloc.y-herosize/2 <edgeyu then
heroloc.y = edgeyu+herosize/2
end
end
if ang > -135 and ang < -45 then
if heroloc.x-herosize/2 <edgexr then
heroloc.x = edgexr+herosize/2
end
end
if ang >= -45 and ang <= 45 then
if heroloc.y+herosize/2 >edgeyd then
heroloc.y = edgeyd-herosize/2
end
end
end
end
end
end
end
if lightsout then
fl:calcshadows()
end
end
function Hero:changeang(ang)
rotangle=ang
end
function Hero:m2(add)
heroloc = heroloc+add
if lightsout then
fl:calcshadows()
end
end
function Hero:colorCycle()
if red then
local c2 = color(255,0,0,255)
local c1 = color(255,125,0,255)
col = c1:mix(c2,blend)
if blend >= 1 then
blend = 0
red = false
orange = true
end
elseif orange then
local c2 = color(255,125,0,255)
local c1 = color(255,255,0,255)
col = c1:mix(c2,blend)
if blend >= 1 then
blend = 0
orange = false
yellow = true
end
elseif yellow then
local c2 = color(255,255,0,255)
local c1 = color(0,255,0,255)
col = c1:mix(c2,blend)
if blend >= 1 then
blend = 0
yellow = false
green = true
end
elseif green then
local c2 = color(0,255,0,255)
local c1 = color(0, 255, 255, 255)
col = c1:mix(c2,blend)
if blend >= 1 then
blend = 0
green = false
teal = true
end
elseif teal then
local c2 = color(0,255,255,255)
local c1 = color(0, 0, 255, 255)
col = c1:mix(c2,blend)
if blend >= 1 then
blend = 0
teal = false
blue = true
end
elseif blue then
local c2 = color(0, 0, 255, 255)
local c1 = color(125,0,255,255)
col = c1:mix(c2,blend)
if blend >= 1 then
blend = 0
blue = false
purple = true
end
elseif purple then
local c2 = color(125,0,255,255)
local c1 = color(255, 0, 255, 255)
col = c1:mix(c2,blend)
if blend >= 1 then
blend = 0
purple = false
violet = true
end
elseif violet then
local c2 = color(255, 0, 255, 255)
local c1 = color(255, 0, 0, 255)
col = c1:mix(c2,blend)
if blend >= 1 then
blend = 0
violet = false
red = true
end
end
blend = blend + .01
end
--# Flashlight
Flashlight = class()
function Flashlight:init()
self.corners = {}
self.corners[0] = vec2(0,0)
self.corners[1] = vec2(0,HEIGHT)
self.corners[2] = vec2(WIDTH,HEIGHT)
self.corners[3] = vec2(WIDTH,0)
self.angle = 0
self.spread = 91
self.hs = self.spread/2
self.ang1 = self.angle-self.spread/2
if self.ang1 < 0 then self.ang1 = self.ang1 + 360 end
self.ang2 = self.angle+self.spread/2
if self.ang2 >=360 then self.ang2 = self.ang2 - 360 end
self.mesh1 = mesh()
self:calcshadows()
end
function Flashlight:draw()
--self:calcshadows()
strokeWidth(3)
stroke(0, 0, 0, 255)
smooth()
self.mesh1:draw()
if drawmeshcalclines then
stroke(255,0,0,255)
end
if drawguides then
line(heroloc.x,heroloc.y,self.lineend1.x,self.lineend1.y)
line(heroloc.x,heroloc.y,self.lineend2.x,self.lineend2.y)
end
if drawmeshcalclines then
if not drawguides then
line(heroloc.x,heroloc.y,self.lineend1.x,self.lineend1.y)
line(heroloc.x,heroloc.y,self.lineend2.x,self.lineend2.y)
end
line(heroloc.x,heroloc.y,self.lineend3.x,self.lineend3.y)
line(heroloc.x,heroloc.y,self.lineend4.x,self.lineend4.y)
line(heroloc.x,heroloc.y,self.lineend5.x,self.lineend5.y)
end
end
function Flashlight:calcshadows()
self.mesh1 = nil
self.mesh1 = mesh()
local len = math.sqrt(2*(900^2))
local angr = math.rad(self.angle)
local ang1r = math.rad(self.ang1)
local ang15r = math.rad(self.angle-self.spread/4)
if ang15r < 0 then ang15r = ang15r + 2*math.pi end
local ang2r = math.rad(self.ang2)
local ang25r = math.rad(self.angle+self.spread/4)
if ang25r > 2*math.pi then ang25r = ang25r - 2*math.pi end
self.lineend1 = vec2(heroloc.x+math.cos(ang1r)*len,heroloc.y+math.sin(ang1r)*len)
self.lineend2 = vec2(heroloc.x+math.cos(ang2r)*len,heroloc.y+math.sin(ang2r)*len)
self.lineend3 = vec2(heroloc.x+math.cos(angr)*len,heroloc.y+math.sin(angr)*len)
self.lineend4 = vec2(heroloc.x+math.cos(ang15r)*len,heroloc.y+math.sin(ang15r)*len)
self.lineend5 = vec2(heroloc.x+math.cos(ang25r)*len,heroloc.y+math.sin(ang25r)*len)
if self.lineend1.x < 0 then
self.lineend1.x = 0
self.lineend1.y = heroloc.y-(heroloc.x*math.tan(ang1r))
end
if self.lineend1.y>HEIGHT then
self.lineend1.y = HEIGHT
self.lineend1.x = heroloc.x+((HEIGHT-heroloc.y)/math.tan(ang1r))
end
if self.lineend1.x>WIDTH then
self.lineend1.x = WIDTH
self.lineend1.y = heroloc.y+((WIDTH-heroloc.x)*math.tan(ang1r))
end
if self.lineend1.y < 0 then
self.lineend1.y = 0
self.lineend1.x = heroloc.x-(heroloc.y/math.tan(ang1r))
end
if self.lineend2.x < 0 then
self.lineend2.x = 0
self.lineend2.y = heroloc.y-(heroloc.x*math.tan(ang2r))
end
if self.lineend2.y>HEIGHT then
self.lineend2.y = HEIGHT
self.lineend2.x = heroloc.x+((HEIGHT-heroloc.y)/math.tan(ang2r))
end
if self.lineend2.x>WIDTH then
self.lineend2.x = WIDTH
self.lineend2.y = heroloc.y+((WIDTH-heroloc.x)*math.tan(ang2r))
end
if self.lineend2.y < 0 then
self.lineend2.y = 0
self.lineend2.x = heroloc.x-(heroloc.y/math.tan(ang2r))
end
if self.lineend3.x < 0 then
self.lineend3.x = 0
self.lineend3.y = heroloc.y-(heroloc.x*math.tan(angr))
end
if self.lineend3.y>HEIGHT then
self.lineend3.y = HEIGHT
self.lineend3.x = heroloc.x+((HEIGHT-heroloc.y)/math.tan(angr))
end
if self.lineend3.x>WIDTH then
self.lineend3.x = WIDTH
self.lineend3.y = heroloc.y+((WIDTH-heroloc.x)*math.tan(angr))
end
if self.lineend3.y < 0 then
self.lineend3.y = 0
self.lineend3.x = heroloc.x-(heroloc.y/math.tan(angr))
end
if self.lineend4.x < 0 then
self.lineend4.x = 0
self.lineend4.y = heroloc.y-(heroloc.x*math.tan(ang15r))
end
if self.lineend4.y>HEIGHT then
self.lineend4.y = HEIGHT
self.lineend4.x = heroloc.x+((HEIGHT-heroloc.y)/math.tan(ang15r))
end
if self.lineend4.x>WIDTH then
self.lineend4.x = WIDTH
self.lineend4.y = heroloc.y+((WIDTH-heroloc.x)*math.tan(ang15r))
end
if self.lineend4.y < 0 then
self.lineend4.y = 0
self.lineend4.x = heroloc.x-(heroloc.y/math.tan(ang15r))
end
if self.lineend5.x < 0 then
self.lineend5.x = 0
self.lineend5.y = heroloc.y-(heroloc.x*math.tan(ang25r))
end
if self.lineend5.y>HEIGHT then
self.lineend5.y = HEIGHT
self.lineend5.x = heroloc.x+((HEIGHT-heroloc.y)/math.tan(ang25r))
end
if self.lineend5.x>WIDTH then
self.lineend5.x = WIDTH
self.lineend5.y = heroloc.y+((WIDTH-heroloc.x)*math.tan(ang25r))
end
if self.lineend5.y < 0 then
self.lineend5.y = 0
self.lineend5.x = heroloc.x-(heroloc.y/math.tan(ang25r))
end
local notposscorn = {}
local q = 0
for i = 0, #self.corners do
if (self.corners[i].x == self.lineend1.x or self.corners[i].x == self.lineend4.x) and
(self.corners[i].y == self.lineend1.y or self.corners[i].y == self.lineend4.y) then
notposscorn[q] = i
q = q + 1
elseif (self.corners[i].x == self.lineend4.x or self.corners[i].x == self.lineend3.x) and
(self.corners[i].y == self.lineend4.y or self.corners[i].y == self.lineend3.y) then
notposscorn[q] = i
q = q + 1
elseif (self.corners[i].x == self.lineend3.x or self.corners[i].x == self.lineend5.x) and
(self.corners[i].y == self.lineend3.y or self.corners[i].y == self.lineend5.y) then
notposscorn[q] = i
q = q + 1
elseif (self.corners[i].x == self.lineend5.x or self.corners[i].x == self.lineend2.x) and
(self.corners[i].y == self.lineend5.y or self.corners[i].y == self.lineend2.y) then
notposscorn[q] = i
q = q + 1
end
end
if notposscorn[0] ~= nil then
local nextvertl = notposscorn[0]-1
if nextvertl <0 then nextvertl = #self.corners end
local nnl = nextvertl-1
if nnl < 0 then nnl = #self.corners end
local nextvertr = notposscorn[#notposscorn]+1
if nextvertr > #self.corners then nextvertr = 0 end
local nnr = nextvertr+1
if nnr > #self.corners then nnr = 0 end
local verts =
{self.lineend2,self.corners[nextvertl],heroloc,self.lineend1,self.corners[nextvertr],heroloc,
heroloc,self.corners[nextvertr],self.corners[nnr],heroloc,self.corners[nextvertl],self.corners[nnl]}
if self.angle > 225 and self.angle < 315 and #notposscorn ~= 0 then
verts = nil
local nextvertr = notposscorn[0]+1
if nextvertr <0 then nextvertr = #self.corners end
local nnr = nextvertr+1
if nnr < 0 then nnr = #self.corners end
local nextvertl = notposscorn[#notposscorn]-1
if nextvertl > #self.corners then nextvertl = 0 end
local nnl = nextvertl-1
if nnl > #self.corners then nnl = 0 end
verts =
{self.lineend2,self.corners[nextvertl],heroloc,
self.lineend1,self.corners[nextvertr],heroloc,
heroloc,self.corners[nextvertr],self.corners[nnr],
heroloc,self.corners[nextvertl],self.corners[nnl]}
end
self.mesh1.vertices = verts
self.mesh1:setColors(0,0,0,255)
else
if self.angle > 135-(self.hs-45) and self.angle < 225+(self.hs-45) then
nextr = 1
elseif self.angle > 45-(self.hs-45) and self.angle < 135+(self.hs-45) then
nextr = 2
elseif self.angle > 315-(self.hs-45) or self.angle < 45+(self.hs-45) then
nextr = 3
elseif self.angle > 225-(self.hs-45) and self.angle < 315+(self.hs-45) then
nextr = 0
end
local nnr = nextr + 1
if nnr>3 then nnr=0 end
local nnnr = nnr + 1
if nnnr>3 then nnnr=0 end
local nnnnr = nnnr + 1
if nnnnr>3 then nnnnr=0 end
verts =
{heroloc,self.lineend1,self.corners[nextr],
heroloc,self.corners[nextr],self.corners[nnr],
heroloc,self.corners[nnr],self.corners[nnnr],
heroloc,self.corners[nnnr],self.corners[nnnnr],
heroloc,self.corners[nnnnr],self.lineend2}
self.mesh1.vertices = verts
self.mesh1:setColors(0,0,0,255)
end
end
function CodeStorage()
local angleto = math.deg(math.atan2(heroloc.x-self.corners[i].x,heroloc.y-self.corners[i].y))
if angleto <= 90 then
angleto = -(angleto-90)
else
angleto = -(angleto-450)
end
angleto = angleto-180
if angleto < 0 then angleto = angleto + 360 end
if math.abs(angleto-self.angle) < self.spread/2 then
end
for i=0,#self.corners do
local use = true
for b = 0, #notposscorn do
if notposscorn[b] == i then
use = false
end
end
if use then
local nr = i+1
if nr > #self.corners then nr = 0 end
local imposs = false
for i = 0, #notposscorn do
if notposscorn[i]==nr then
local v2 = self.lineend1
imposs = true
end
end
if not imposs then
local v2 = self.corners[nr]
end
--verts[#verts+1] = self.corners[i]
--verts[#verts+1] = v2
--verts[#verts+1] = heroloc
end
end
end
function Flashlight:changeang(newang)
self.angle = math.deg(newang)
self.ang1 = self.angle-self.spread/2
if self.ang1 < 0 then self.ang1 = self.ang1 + 360 end
self.ang2 = self.angle+self.spread/2
if self.ang2 >=360 then self.ang2 = self.ang2 - 360 end
if lightsout then
self:calcshadows()
end
end
--# Joystiqs
Joystiqs = class()
touch = nil
originx = nil
originy = nil
originid = nil
touch2 = nil
originx2 = nil
originy2 = nil
originid2 = nil
function Joystiqs:init()
end
function Joystiqs:draw()
stroke(127, 127, 127, 25)
strokeWidth(0)
lineCapMode(SQUARE)
if originx ~= nil then
fill(0, 0, 255, 100)
ellipse(originx, originy, 150)
line(originx, originy, linelimiterx, linelimitery)
fill(0, 0, 255, 100)
ellipse(linelimiterx, linelimitery, 150)
end
if originx2 ~= nil then
fill(0, 0, 255, 100)
ellipse(originx2, originy2, 150)
line(originx2, originy2, linelimiterx2, linelimitery2)
fill(0, 0, 255, 100)
ellipse(linelimiterx2, linelimitery2, 150)
end
--self:cleanJoystickData()
end
function Joystiqs:touched(touched)
if touched.x < WIDTH/2 and touch == nil and touched.state == BEGAN then
touch = touched
originid = touched.id
originx = touched.x
originy = touched.y
elseif touch ~= nil and touched.id == originid then
touch = touched
end
if (touched.state == ENDED) and (touched.id == originid) then
touch = nil
originx = nil
originy = nil
originid= nil
end
if touched.x > WIDTH/2 and touch2 == nil and touched.state == BEGAN then
touch2 = touched
originid2 = touched.id
originx2 = touched.x
originy2 = touched.y
elseif touch2 ~= nil and touched.id == originid2 then
touch2 = touched
end
if (touched.state == ENDED) and (touched.id == originid2) then
touch2 = nil
originx2 = nil
originy2 = nil
originid2 = nil
end
end
function Joystiqs:j1Calc()
if (touch ~= nil) then
offsetx = touch.x - originx
offsety = touch.y - originy
preangle = (math.atan((offsety)/(offsetx)))
if offsetx == 0 and offsety > 0 then
angle = math.pi/2
else
if offsetx <= 0 and offsety > 0 then
angle = math.pi + preangle
elseif offsetx < 0 and offsety <= 0 then
angle = math.pi + preangle
elseif offsetx >=0 and offsety < 0 then
angle = (2*math.pi) + preangle
else
angle = preangle
end
end
if math.sqrt((offsetx*offsetx)+(offsety*offsety)) > 75 then
linelimiterx = (75*math.cos(angle)) + originx
linelimitery = (75*math.sin(angle)) + originy
else
linelimiterx = touch.x
linelimitery = touch.y
end
magnitude = math.sqrt(((linelimiterx-originx)^2)+((linelimitery-originy)^2))
if magnitude < 25 and magnitude >10 then
magnitude = 0
elseif magnitude <= 10 then
magnitude = 0
angle = nil
else
magnitude = magnitude - 25
end
end
end
function Joystiqs:j2Calc()
if (touch2 ~= nil) then
offsetx2 = touch2.x - originx2
offsety2 = touch2.y - originy2
preangle2 = (math.atan((offsety2)/(offsetx2)))
if offsetx2 == 0 and offsety2 > 0 then
angle2 = math.pi/2
else
if offsetx2 <= 0 and offsety2 > 0 then
angle2 = math.pi + preangle2
elseif offsetx2 < 0 and offsety2 <= 0 then
angle2 = math.pi + preangle2
elseif offsetx2 >=0 and offsety2 < 0 then
angle2 = (2*math.pi) + preangle2
else
angle2 = preangle2
end
end
if math.sqrt((offsetx2*offsetx2)+(offsety2*offsety2)) > 75 then
linelimiterx2 = (75*math.cos(angle2)) + originx2
linelimitery2 = (75*math.sin(angle2)) + originy2
else
linelimiterx2 = touch2.x
linelimitery2 = touch2.y
end
magnitude2 = math.sqrt(((linelimiterx2-originx2)^2)+((linelimitery2-originy2)^2))
fill(255, 255, 255, 100)
ellipse(originx2, originy2, 150)
line(originx2, originy2, linelimiterx2, linelimitery2)
fill(0, 0, 0, 100)
ellipse(linelimiterx2, linelimitery2, 150)
if magnitude2 < 10 then
magnitude2 = 0
angle2 = nil
else
magnitude2 = magnitude2 - 25
end
end
end
function Joystiqs:getmag()
return magnitude
end
function Joystiqs:getang()
return angle
end
function Joystiqs:getang2()
return angle2
end
function Joystiqs:cleanJoystickData()
offsetx=nil
offsety=nil
offsetx2=nil
offsety2=nil
preangle=nil
preangle2=nil
angle=nil
angle2=nil
linelimiterx=nil
linelimitery=nil
linelimiterx2=nil
linelimitery2=nil
magnitude=nil
magnitude2=nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment