Let me explain:
I've got a dungeontest.lua file, generated by Tiled and then reworked by another program in order to be used with love2d, which contains the raw data concerning the dungeon, something like this:
Mini-disclaimer: let's suppose all the variables you below are initialised with the correct values
Code: Select all
return
{
layers = {
data = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}
}
}
Code: Select all
local function newAtlas(tileSizeX, tileSizeY, dungeon)
--this function returns a Quad table from the tileset loaded
end
local function updateTilesetBatch() --this function creates a triple dimensional matrix which will be used in dungeon.draw()
local temp = love.graphics.newQuad(0,0,0,0,0,0)
local temp_loc = {}
for i=1, #dungeon.layers do
tilesetBatch[i]={}
for x=tilesDisplayWidth, 1, -1 do
tilesetBatch[i][x] = {}
for y=tilesDisplayHeight, 1, -1 do
if tileMap[i].data[y][x]~= 0 then
tilesetBatch[i][x][y] = tileQuads[tileMap[i].data[y][x]]
else
tilesetBatch[i][x][y] = quadZero
end
end
end
end
end
local function setupTiles()
--initializes many variables and calls newAtlas(..) and updateTilesetBatch()
updateTilesetBatch()
end
function dungeon:draw() --Draws the dungeon, as simple as that, start_x and start_y are ad-hoc values needed in order for the dungeon to be drawn starting from (0,0)
cam:attach()
for i=1, #dungeon.layers do
for j = #tilesetBatch[i], 1, -1 do
for k = #tilesetBatch[i][j], 1, -1 do
if tilesetBatch[i][j][k] ~= quadZero then
love.graphics.draw(dungeon.spritesheet,tilesetBatch[i][j][k],math.floor(-zoomX*dungeon.tilewidth*0.5*(k-j))-start_x, math.floor(-zoomY*dungeon.tileheight*0.4*(j+k))-start_y,0, zoomX, zoomY)
end
end
end
end
entities:draw()
cam:detach()
ui:draw()
end
Code: Select all
for i=1, #dungeon.layers do
for x=1, math.floor(tilesDisplayWidth/2) do
temp_loc = tilesetBatch[i][x]
tilesetBatch[i][x] = tilesetBatch[i][tilesDisplayWidth-x+1]
tilesetBatch[i][tilesDisplayWidth-x+1] = temp_loc
end
for x=1, tilesDisplayWidth do
for y=1, math.floor(tilesDisplayHeight/2) do
temp = tilesetBatch[i][x][y]
tilesetBatch[i][x][y] = tilesetBatch[i][x][tilesDisplayHeight-y+1]
tilesetBatch[i][x][tilesDisplayHeight-y+1] = temp
end
end
end