Skip to content

Instantly share code, notes, and snippets.

@monkeyman32123
Created March 31, 2014 01:19
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/d521856ab7cb4ad103f0 to your computer and use it in GitHub Desktop.
Save monkeyman32123/d521856ab7cb4ad103f0 to your computer and use it in GitHub Desktop.
--# Main
displayMode(OVERLAY)
supportedOrientations(LANDSCAPE_ANY)
function setup()
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()
if j:getmag() ~= nil and j:getang() ~= nil then
hero:m1(j:getmag(),j:getang())
fl:changeang(j:getang())
end
for i=0,#worlds do
worlds[i]:draw()
end
j:draw()
fl:draw()
hero: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
--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,0,1,
1,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,0,0,
1,0,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,0,0,0,0,0,0,0,0,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ero:init(x)
heroloc = vec2(WIDTH/2,HEIGHT/2)
herosize = 40
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/15,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
end
function Hero:m2(add)
heroloc = heroloc+add
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.angle = 0
self.spread = 90
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()
end
function Flashlight:draw()
self:calcshadows()
strokeWidth(3)
stroke(255, 0, 0, 255)
line(heroloc.x,heroloc.y,self.lineend1.x,self.lineend1.y)
line(heroloc.x,heroloc.y,self.lineend2.x,self.lineend2.y)
end
function Flashlight:calcshadows()
local len = math.sqrt(2*(768^2))
local ang1r = math.rad(self.ang1)
local ang2r = math.rad(self.ang2)
local slope1 = math.sin(ang1r)/math.cos(ang1r)
local yb1 = heroloc.y-(slope1*heroloc.x)
local xb1 = -yb1/slope1
local yb2 = (slope1*WIDTH)+yb1
local xb2 = (HEIGHT-yb1)/slope1
local slope2 = math.sin(ang2r)/math.cos(ang2r)
local yb3 = heroloc.y-(slope2*heroloc.x)
local xb3 = -yb3/slope2
local yb4 = (slope2*WIDTH)+yb3
local xb4 = (HEIGHT-yb3)/slope2
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)
if ang1r <= math.pi*(3/2) and ang1r >= math.pi/2 then
if yb1< HEIGHT and yb1>0 then
self.lineend1.x=0
self.lineend1.y=yb1
end
end
if ang1r <= math.pi and ang1r >= 0 then
if xb2<WIDTH and xb2 >0 then
self.lineend1.x=xb2
self.lineend1.y=HEIGHT
end
end
if ang1r < math.pi/2 and ang1r > math.pi*(3/2) then
if yb2<HEIGHT and yb2 >0 then
self.lineend1.x=WIDTH
self.lineend1.y=yb2
end
end
if ang1r < math.pi*2 and ang1r > math.pi/2 then
if xb1<WIDTH and xb1 > 0 then
self.lineend1.x=xb1
self.lineend1.y=0
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
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
--self:j2Calc()
--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: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