Code: Select all
Tileset = love.graphics.newImage('tileset1.bmp')
TileW, TileH = 50,50
tilesetW, tilesetH = Tileset:getWidth(), Tileset:getHeight()
local tileString = [[
&&&&&&& sssss
&#####& swwws
&#####& swwws
&&&*&&& sssss
]]
local quadInfo = {
{ ' ', 0, 0 , 0 }, -- grass
{ 'p', 50, 0 , 0 }, -- spess
{ '&', 0, 50 , 1 }, -- wall
{ '*', 50, 50, 1 }, -- door ##<<<< See the fourth variable here? This is the one I want to control collision
{ 'w', 100, 0 , 1}, -- water
{ '#', 0, 100 , 0 }, -- tiles
{ 's', 100, 50 , 0 } -- sand
}
Quads = {}
for _,info in ipairs(quadInfo) do
-- info[1] = character, info[2]= x, info[3] = y
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 rowIndex,columnIndex = 1,1
for row in tileString:gmatch("[^\n]+") do
assert(#row == width, 'Map is not aligned: width of row ' .. tostring(rowIndex) .. ' should be ' .. tostring(width) .. ', but it is ' .. tostring(#row))
columnIndex = 1
for character in row:gmatch(".") do
TileTable[columnIndex][rowIndex] = character
columnIndex = columnIndex + 1
end
rowIndex=rowIndex+1
end
columntest = 1
rowtest = 1
function drawmap()
for columnIndex,column in ipairs(TileTable) do
for rowIndex,char in ipairs(column) do
local x,y = (columnIndex-columntest)*TileW, (rowIndex-rowtest)*TileH
love.graphics.drawq(Tileset, Quads[char], x, y)
end
end
end
If it is needed, here's my player movement code.
Code: Select all
gridx = 1
gridy = 1
pdt = dt
gridx = 1
gridy = 1
playerxx = gridx * 50
playeryy = gridy * 50
player = love.graphics.newImage("player.png")
0
function Drawplayer()
love.graphics.draw(player, playerxx, playeryy)
end
function playermove()
function love.keypressed(key)
if key=='right' then
gridx = gridx + 1
elseif key=='up' then
gridy = gridy - 1
elseif key=='down' then
gridy = gridy + 1
elseif key=='left' then
gridx = gridx - 1
end
end
playerxx = gridx * 50
playeryy = gridy * 50
end