But I would like the map to be centered.
Here is the code I've got:
main.lua
Code: Select all
require "functionsmapa"
require "config"
function love.load()
loadMap('/mapas/mapa-teste1.lua')
end
function love.draw()
drawMap()
end
Code: Select all
local tileW, tileH, tileset, quads, tileTable
function loadMap(caminho)
love.filesystem.load(caminho)()
end
function newMap(tileWidth, tileHeight, tilesetCaminho, tileString, quadInfo)
tileW = tileWidth
tileH = tileHeight
Tileset = love.graphics.newImage(tilesetCaminho)
tilesetW, tilesetH = Tileset:getWidth(), Tileset:getHeight()
quads = {}
local tilesetW, tilesetH = Tileset:getWidth(), Tileset:getHeight()
for _, info in ipairs(quadInfo) do
quads[info[1]] = love.graphics.newQuad(info[2], info[3], tileW, tileH, tilesetW, tilesetH)
end
tileTable = {}
local width = #(tileString:match("[^\n]+"))
for x = 1, width, 1 do tileTable[x] = {} end
local x, y = 1, 1
for row in tileString:gmatch("[^\n]+") do
assert(#row == width, 'Map is not aligned: width of row ' .. tostring(y) .. ' should be ' .. tostring(width) .. ', but it is ' .. tostring(#row))
x = 1
for character in row:gmatch(".") do
tileTable[x][y] = character
x = x + 1
end
y = y + 1
end
end
function drawMap()
for columnIndex, column in ipairs(tileTable) do
for rowIndex, char in ipairs(column) do
x, y = (columnIndex-1)*tileW, (rowIndex-1)*tileH
love.graphics.draw(Tileset, quads[char], x, y)
end
end
end
Code: Select all
local tileString = [[
#########################
X 0 X
X 0 X
X 0 X
X X
X XX XX XXX X X X
X X X X X X X X X
X X X X X X X X X
X X X XXX XXX X X X
X X X X X X X 0 X
X 0 X X X X X X X
X X X X X X X X X
X XX XXX XXX X X
X X
X <><><><><><><><> X
X X
X 0 0 X
X#######################X
]]
local quadInfo = {
{' ', 0, 0 }, -- = chão
{'0', 16, 0 }, -- = alçapão fechado
{',', 32, 0 }, -- = borda superior-esquerda
{'-', 48, 0 }, -- = borda superior
{'*', 64, 0 }, -- = borda superior-direita
{'#', 0, 16 }, -- = chão gradeado
{'X', 0, 32 }, -- = caixa
{'C', 0, 48 }, -- = caixa superior-parede
{'c', 0, 64 }, -- = caixa inferior-chão
{'O', 16, 16 }, -- = alçapão aberto
{'<', 16, 32 }, -- = mesa esquerda
{'B', 16, 48 }, -- = caixa2 superior-parede
{'b', 16, 64 }, -- = caixa2 inferior-chão
{'e', 32, 16 }, -- = borda esquerda
{'>', 32, 32 }, -- = mesa direita
{'T', 32, 48 }, -- = caixa3 superior
{'t', 32, 64 }, -- = caixa3 inferior-chão
{'p', 48, 16 }, -- = borda inferior esquerda
{'P', 48, 32 }, -- = borda inferior
{'D', 48, 48 }, -- = porta aberta superior
{'d', 48, 64 }, -- = porta aberta inferior
{'r', 64, 16 }, -- = borda direita
{'R', 64, 32 }, -- = borda inferior direita
{'u', 64, 48 }, -- = parede superior
{'U', 64, 64 } -- = parede inferior
}
newMap(16, 16 , '/imagens/tileset001.png', tileString, quadInfo)
Code: Select all
function love.config(t)
t.version = '0.0.1'
t.screen.width = x
t.screen.height = y
end