Skip to content

Instantly share code, notes, and snippets.

Created March 6, 2015 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/c6deaf507d43594ab6b3 to your computer and use it in GitHub Desktop.
Save anonymous/c6deaf507d43594ab6b3 to your computer and use it in GitHub Desktop.
--# block
block = class()
function block:init(x,y,z,dim)
-- you can accept and set parameters here
--use parameters to define all vertices needed to make a cube
self.verts={
vec3(x+dim/2,y-dim/2,z+dim/2),
vec3(x-dim/2,y-dim/2,z+dim/2),
vec3(x+dim/2,y-dim/2,z-dim/2),
vec3(x-dim/2,y-dim/2,z-dim/2),
vec3(x+dim/2,y+dim/2,z+dim/2),
vec3(x-dim/2,y+dim/2,z+dim/2),
vec3(x+dim/2,y+dim/2,z-dim/2),
vec3(x-dim/2,y+dim/2,z-dim/2)
}
--set up our actual mesh
self.m=mesh()
--put the verts in order into our mesh
self.m.vertices={
self.verts[1],self.verts[5],self.verts[3],
self.verts[3],self.verts[7],self.verts[5],
self.verts[1],self.verts[5],self.verts[2],
self.verts[2],self.verts[6],self.verts[5],
self.verts[2],self.verts[6],self.verts[4],
self.verts[4],self.verts[8],self.verts[6],
self.verts[4],self.verts[8],self.verts[3],
self.verts[7],self.verts[8],self.verts[3],
self.verts[5],self.verts[6],self.verts[8],
self.verts[5],self.verts[7],self.verts[8],
self.verts[1],self.verts[2],self.verts[4],
self.verts[1],self.verts[3],self.verts[4]
}
--make sure there is no tint
self.m:setColors(255,0,0)
end
function block:draw()
-- Codea does not automatically call this method
--draw the mesh
self.m:draw()
end
function block:touched(touch)
-- Codea does not automatically call this method
end
--# dPad
dPad = class()
function dPad:init(x,y)
-- you can accept and set parameters here
self.x = x
self.y = y
self.tId=nil
end
function dPad:draw()
-- Codea does not automatically call this method
noStroke()
fill(153, 153, 153, 255)
rect(self.x-25,self.y-25,50,50)
end
function dPad:touched(touch)
-- Codea does not automatically call this method
if self.tId==nil and touch.state~=ENDED and touch.x>self.x-25 and touch.y>self.y-25 and touch.x<self.x+25 and touch.y<self.y+25 then
self.tId=touch.id
elseif touch.id==self.tId and touch.state==ENDED then self.tId=nil
end
end
--# Main
-- Rpg
-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN_NO_BUTTONS)
print("Hello World!")
m,i=generateDungeon(30)
blocks={}
for x,a in pairs(m) do
for y,b in pairs(a) do
if b==1 then
blocks[#blocks+1]=block(y*10,0,x*10,10,"Cargo Bot:Game Area",0)
end
end
end
pos=vec3(i.x*10,0,i.y*10)
look=vec3(0,0,0)
angleX=200
angleY=0
up=dPad(150,150)
acc=vec3(0,0,0)
touches={}
right=dPad(200,100)
left=dPad(100,100)
down=dPad(150,50)
floor=mesh()
floor.vertices={
vec3(5,-5,5),vec3(305,-5,5),vec3(305,-5,305),
vec3(5,-5,5),vec3(5,-5,305),vec3(305,-5,305)
}
floor:setColors(0,0,0)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(255, 255, 255, 255)
text(math.floor(1/DeltaTime), WIDTH-10,HEIGHT-10)
look.x,look.z=math.sin(math.rad(angleX))*10000,-math.cos(math.rad(angleX))*10000
look.y=math.sin(math.rad(angleY))*10000
-- This sets the line thickness
perspective(45,WIDTH/HEIGHT)
camera(pos.x,pos.y,pos.z, look.x,look.y,look.z, 0,1,0)
-- Do your drawing here
for x,c in pairs(blocks) do
c:draw()
end
floor:draw()
ortho()
viewMatrix(matrix())
up:draw()
right:draw()
left:draw()
down:draw()
pos=pos+acc
for i,v in pairs(touches) do
if v.state~=ENDED then
if v.id~=up.tId and v.id~=right.tId and v.id~=left.tId and v.id~=down.tId then
angleX = angleX + v.deltaX
angleY = angleY + v.deltaY/3
elseif v.id==up.tId then
acc=-vec2(pos.x-look.x,pos.z-look.z):normalize()
acc=vec3(acc.x,0,acc.y)
elseif v.id==right.tId then
acc=vec2(pos.x-look.x,pos.z-look.z):normalize()
acc=vec3(acc.y,0,-acc.x)
elseif v.id==left.tId then
acc=vec2(pos.x-look.x,pos.z-look.z):normalize()
acc=vec3(-acc.y,0,acc.x)
elseif v.id==down.tId then
acc=vec2(pos.x-look.x,pos.z-look.z):normalize()
acc=vec3(acc.x,0,acc.y)
end
end
end
if not up.tId and not right.tId and not left.tId and not down.tId then acc=vec3(0,0,0) end
end
function touched(t)
touches[t.id]=t
up:touched(t)
right:touched(t)
left:touched(t)
down:touched(t)
end
function generateDungeon(dim)
local map={}
for x=1,dim do
map[x]={}
for y=1,dim do
map[x][y]=1
end
end
initPos=vec2(1,1)
map[initPos.x][initPos.y]=0
curPos=initPos
count=0
player=initPos
while count<=500 do
prevPos=curPos
while prevPos==curPos or curPos.x<1 or curPos.x>dim or curPos.y<1 or curPos.y>dim do
curPos=prevPos
ranX=math.random(-1,1)
ranY=0
if ranX==0 then
while ranY==0 do
ranY=math.random(-1,1)
end
end
curPos=vec2(curPos.x+ranX, curPos.y+ranY)
end
if curPos.x>1 and curPos.x<30 and map[curPos.x+1][curPos.y]==nil then map[curPos.x+1][curPos.y]=1 end
if curPos.x>1 and curPos.x<30 and map[curPos.x-1][curPos.y]==nil then map[curPos.x-1][curPos.y]=1 end
if curPos.y>1 and curPos.y<30 and map[curPos.x][curPos.y+1]==nil then map[curPos.x][curPos.y+1]=1 end
if curPos.y>1 and curPos.y<30 and map[curPos.x][curPos.y-1]==nil then map[curPos.x][curPos.y-1]=1 end
count = count + 1
map[curPos.x][curPos.y]=0
endPos=curPos
if endPos.x>1 and endPos.x<30 and map[endPos.x+1][endPos.y]==nil then map[endPos.x+1][endPos.y]=1 end
if endPos.x>1 and endPos.x<30 and map[endPos.x-1][endPos.y]==nil then map[endPos.x-1][endPos.y]=1 end
if endPos.y>1 and endPos.y<30 and map[endPos.x][endPos.y+1]==nil then map[endPos.x][endPos.y+1]=1 end
if endPos.y>1 and endPos.y<30 and map[endPos.x][endPos.y-1]==nil then map[endPos.x][endPos.y-1]=1 end
end
return map,initPos
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment