Map = class()
function Map:init()
self.bodies = {}
--[[
self:createEdge(0, 0, WIDTH, 0)
self:createEdge(0, 0, 0, HEIGHT)
self:createEdge(WIDTH, 0, WIDTH, HEIGHT)
self:createEdge(0, HEIGHT, WIDTH, HEIGHT)
--]]
self.maps = {self.CLASSIC, self.OPEN, self.SMAZE, self.LMAZE, self.BUMPERS, self.CROSSHAIRS, self.SLIDERS, self.SQUEEZE, self.SPINNER}
self.currentMap = (mapnumber == 0 and math.random(1, #self.maps)) or mapnumber
self.changetime = 0
self:resetMap()
self.lastchange = ElapsedTime
--[[
print(i)
local f = maps[i]
print(f)
self:f()
--]]
--[[
slider1 = physicsBox(50, HEIGHT/3)
slider1.type = STATIC
slider1.position = vec2(WIDTH/2, HEIGHT/6)
tween(1, slider1.linearVelocity, {y = HEIGHT/1.2})--, {loop = tween.loop.pingpong})
table.insert(self.bodies, slider1)
--]]
end
function Map:resetMap()
for k,v in pairs(self.bodies) do
v:destroy()
end
self.bodies = {}
self:createEdge(0, 0, WIDTH, 0)
self:createEdge(0, 0, 0, HEIGHT)
self:createEdge(WIDTH, 0, WIDTH, HEIGHT)
self:createEdge(0, HEIGHT, WIDTH, HEIGHT)
self.maps[self.currentMap](self)
end
function Map:draw()
stroke(255)
strokeWidth(5)
fill(255)
if self.changetime == 0 then
for k,v in pairs(self.bodies) do
drawBody(v)
end
else
local t = ElapsedTime - self.lastchange
local _,d = math.modf(t/self.changetime)
if d < 1 - 1/self.changetime or ElapsedTime * 10 < 0.5 + math.floor(ElapsedTime * 10) then
for k,v in pairs(self.bodies) do
drawBody(v)
end
end
if d > 1 - DeltaTime then
self:resetMap()
end
end
end
function Map:CLASSIC()
local body = physics.body(CIRCLE, 100)
body.position = CENTRE
body.restitution = 1
body.type = STATIC
table.insert(self.bodies, body)
end
function Map:OPEN()
self:createEdge(HEIGHT/3, 0, 0, HEIGHT/3)
self:createEdge(0, HEIGHT/1.5, HEIGHT/3, HEIGHT)
self:createEdge(WIDTH - HEIGHT/3, HEIGHT, WIDTH, HEIGHT/1.5)
self:createEdge(WIDTH, HEIGHT/3, WIDTH - HEIGHT/3, 0)
end
function Map:SMAZE()
numw,numh = 5,3
m = Maze(numw, numh)
z = m.maze
for i = 1,numw do
for j = 1,numh do
local x, y = (i - 1) * WIDTH/numw, HEIGHT - (j - 1) * HEIGHT/numh
if z[i][j][1] == 1 then self:createEdge(x, y, x + WIDTH/numw, y) end
if z[i][j][2] == 1 then self:createEdge(x, y, x, y - HEIGHT/numh) end
end
end
self.changetime = 5
end
function Map:LMAZE()
numw,numh = 7,5
m = Maze(numw, numh)
z = m.maze
for i = 1,numw do
for j = 1,numh do
local x, y = (i - 1) * WIDTH/numw, HEIGHT - (j - 1) * HEIGHT/numh
if z[i][j][1] == 1 then self:createEdge(x, y, x + WIDTH/numw, y) end
if z[i][j][2] == 1 then self:createEdge(x, y, x, y - HEIGHT/numh) end
end
end
self.changetime = 5
end
function Map:BUMPERS()
for i=1,4 do
local bumper = physics.body(CIRCLE, 50)
bumper.position = CENTRE + vec2(math.random(-100,100), math.random(-100,100))
bumper.restitution = 1
bumper.mass = 1000
bumper.linearVelocity = math.random(200) * dirvec(math.random(360))
table.insert(self.bodies, bumper)
end
end
function Map:CROSSHAIRS()
local body = physicsBox(50, HEIGHT/3)
body.type = STATIC
body.position = vec2(WIDTH/2, HEIGHT/6)
table.insert(self.bodies, body)
local body = physicsBox(50, HEIGHT/3)
body.type = STATIC
body.position = vec2(WIDTH/2, HEIGHT/1.2)
table.insert(self.bodies, body)
local body = physicsBox(WIDTH/3, 50)
body.type = STATIC
body.position = vec2(WIDTH/6, HEIGHT/2)
table.insert(self.bodies, body)
local body = physicsBox(WIDTH/3, 50)
body.type = STATIC
body.position = vec2(WIDTH/1.2, HEIGHT/2)
table.insert(self.bodies, body)
end
function Map:SLIDERS()
slider1 = physicsBox(50, HEIGHT/3)
slider1.type = KINEMATIC
slider1.position = vec2(WIDTH/2, HEIGHT/6)
tween(5, slider1.position, {y = HEIGHT/1.2}, {loop = tween.loop.pingpong})
table.insert(self.bodies, slider1)
slider2 = physicsBox(50, HEIGHT/3)
slider2.type = KINEMATIC
slider2.position = vec2(WIDTH/2, HEIGHT/1.2)
tween(5, slider2.position, {y = HEIGHT/6}, {loop = tween.loop.pingpong})
table.insert(self.bodies, slider2)
slider3 = physicsBox(WIDTH/3, 50)
slider3.type = KINEMATIC
slider3.position = vec2(WIDTH/6, HEIGHT/2)
tween(5, slider3.position, {x = WIDTH/1.2}, {loop = tween.loop.pingpong})
table.insert(self.bodies, slider3)
slider4 = physicsBox(WIDTH/3, 50)
slider4.type = KINEMATIC
slider4.position = vec2(WIDTH/1.2, HEIGHT/2)
tween(5, slider4.position, {x = WIDTH/6}, {loop = tween.loop.pingpong})
table.insert(self.bodies, slider4)
--[[
local vbar = physics.body(POLYGON, vec2(-25,-100), vec2(25,-100), vec2(25,100), vec2(-25,100))
vbar.categories = {1}
vbar.mask = {0}
vbar.restitution = 1
vbar.mass = 100000
vbar.fixedRotation = true
vbar.position = CENTRE
vbar.linearVelocity = vec2(0,100)
local hbar = physics.body(POLYGON, vec2(-100,-25), vec2(-100,25), vec2(100,25), vec2(100,-25))
hbar.categories = {2}
hbar.mask = {0}
hbar.restitution = 1
hbar.mass = 100000
hbar.fixedRotation = true
hbar.position = CENTRE
hbar.linearVelocity = vec2(100,0)
table.insert(self.bodies, vbar)
table.insert(self.bodies, hbar)
local vbar = physics.body(POLYGON, vec2(-25,-100), vec2(25,-100), vec2(25,100), vec2(-25,100))
vbar.categories = {3}
vbar.mask = {0}
vbar.restitution = 1
vbar.mass = 100000
vbar.fixedRotation = true
vbar.position = CENTRE
vbar.linearVelocity = vec2(0,-100)
local hbar = physics.body(POLYGON, vec2(-100,-25), vec2(-100,25), vec2(100,25), vec2(100,-25))
hbar.categories = {4}
hbar.mask = {0}
hbar.restitution = 1
hbar.mass = 100000
hbar.fixedRotation = true
hbar.position = CENTRE
hbar.linearVelocity = vec2(-100,0)
table.insert(self.bodies, vbar)
table.insert(self.bodies, hbar)
]]
end
function Map:SQUEEZE()
local speed = 50
local up = physics.body(EDGE, vec2(0,0), vec2(WIDTH,0))
up.type = KINEMATIC
up.linearVelocity = vec2(0,speed)
local right = physics.body(EDGE, vec2(0,0), vec2(0,HEIGHT))
right.type = KINEMATIC
right.linearVelocity = vec2(speed,0)
local left = physics.body(EDGE, vec2(WIDTH,0), vec2(WIDTH,HEIGHT))
left.type = KINEMATIC
left.linearVelocity = vec2(-speed,0)
local down = physics.body(EDGE, vec2(0,HEIGHT), vec2(WIDTH,HEIGHT))
down.type = KINEMATIC
down.linearVelocity = vec2(0,-speed)
table.insert(self.bodies, up)
table.insert(self.bodies, right)
table.insert(self.bodies, left)
table.insert(self.bodies, down)
local body = physics.body(CIRCLE, 50)
body.position = CENTRE
body.restitution = 1
body.type = STATIC
table.insert(self.bodies, body)
self.changetime = 5
end
function Map:SPINNER()
box1 = physicsBox(50, HEIGHT - 100)
box1.type = KINEMATIC
box1.position = CENTRE
box1.restitution = 1
box1.angularVelocity = 20
table.insert(self.bodies, box1)
box2 = physicsBox(HEIGHT - 100, 50)
box2.type = KINEMATIC
box2.position = CENTRE
box2.restitution = 1
box2.angularVelocity = 20
table.insert(self.bodies, box2)
end
function Map:clear()
for k,v in pairs(self.bodies) do
v:destroy()
end
end
function Map:createEdge(ax, ay, bx, by)
local body = physicsEdge(ax, ay, bx, by)
body.restitution = 1
table.insert(self.bodies, body)
end
Maze = class()
-- Code by Ignatz
function Maze:init(n,m)
local m=m or n --set m=n if not provided
t={} for i=1,n do t[i]={} end --create 2D maze
local stack={} --holds visited cells
local visitedCells=0
local totalCells=n*m
--set up little array to help with finding neighbours
local nb={{x=0,y=-1},{x=-1,y=0},{x=1,y=0},{x=0,y=1}}
--generate random starting cell
local cell={x=math.random(1,n),y=math.random(1,m)}
self.firstX,self.firstY=cell.x,cell.y
t[cell.x][cell.y]={1,1,1,1} -- top, left, right, bottom (1=wall)
visitedCells = visitedCells + 1
--loop through
while visitedCells<totalCells do
local prevVisitedCells=visitedCells
local r={1,2,3,4}
while #r~=0 do
--pick a random neighbour from the four directions
local rr=math.random(1,#r)
local s=r[rr]
table.remove(r,rr)
--work out x and y positions of neighbour
local x,y=cell.x+nb[s].x,cell.y+nb[s].y
--must be a valid cell that hasnt been visited
if x>0 and x<=n and y>0 and y<=m and t[x][y]==nil then
t[x][y]={1,1,1,1} -- top, right,bottom,left (1=wall)
t[cell.x][cell.y][s]=0 --break down wall in current cell
t[x][y][5-s]=0 --and neighbour
visitedCells = visitedCells + 1
table.insert(stack,{x=cell.x,y=cell.y}) --add previous cell to stack in case we need it
cell.x,cell.y=x,y --make neighbour the current cell
break
end
end
if prevVisitedCells==visitedCells then
--no unvisited neighbours found, go back to previous cell from stack
cell.x,cell.y=stack[#stack].x,stack[#stack].y
table.remove(stack,#stack)
end
end
self.maze=t
end