Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dungeon
-- AAAA
function setup()
parameter.integer("Distance",0,400,40)
tileSize=50
mapSize=10 --maximum size of map
tiles={}
tiles['f']=readImage("Dropbox:3D-plaster1")
tiles['w']=readImage("Dropbox:3D-stone1")
char={}
char.img=readImage("Planet Cute:Character Pink Girl")
char.x,char.y=2,3
map={
{'w','w','f','w','w','w','w'},
{'w','f','f','f','f','f','w'},
{'w','f','f','f','f','f','w'},
{'w','f','f','f','f','f','w'},
{'w','w','w','w','w','w','w'}
}
mapWidth,mapHeight=#map[1],#map
offsetX,offsetY=150,600
end
function draw()
background(220)
local mm={}
local dd=Distance*Distance
for x=1,mapWidth do
for y=1,mapHeight do
local t=map[y][x] --because loop will read rows first, this keeps map looking like table above
if mm[t]==nil then
mm[t]=mesh()
mm[t].texture=tiles[t] --assumes images are in tiles table
end
local u=mm[t]:addRect((x-1)*tileSize,-(y-1)*tileSize,tileSize,tileSize)
mm[t]:setRectTex(u,0,0,1,1)
--assign shader, give it current char pos and visibility distance (pixels)
mm[t].shader=shader(darkShader.vertexShader, darkShader.fragmentShader)
mm[t].shader.pos=vec2((char.x-1)*tileSize,-(char.y-1)*tileSize)
mm[t].shader.dist=Distance
end
end
pushMatrix()
pushStyle()
translate(offsetX,offsetY)
--black out entire map to start with
fill(0)
rectMode(CORNER)
rect(0,0,mapSize*tileSize,-mapSize*tileSize)
--end of map blacked
for i,m in pairs(mm) do
m:draw()
end
--draw our char
sprite(char.img,(char.x-1)*tileSize,-(char.y-1)*tileSize,tileSize)
popStyle()
popMatrix()
end
--nothing clever here
--just moves char in one of four directions if user touches somewhere on the map
--choose x or y direction based on which is further from current position
function touched(touch)
if touch.state==ENDED then
if touch.x>offsetX and touch.x<offsetX+mapSize*tileSize and
touch.y<offsetY and touch.y>=offsetY-mapSize*tileSize then
local dx,dy=touch.x-(offsetX+(char.x-1)*tileSize),touch.y-(offsetY-(char.y-1)*tileSize)
local nx,ny=char.x,char.y
if math.abs(dx)>math.abs(dy) then
if dx>0 then nx=char.x+1 else nx=char.x-1 end
else
if dy>0 then ny=char.y-1 else ny=char.y+1 end
end
if map[ny][nx]=='f' then char.x,char.y=nx,ny end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.