Skip to content

Instantly share code, notes, and snippets.

@sheenobu
Created September 27, 2015 06:24
Show Gist options
  • Save sheenobu/47e80e5c25cc68a6802e to your computer and use it in GitHub Desktop.
Save sheenobu/47e80e5c25cc68a6802e to your computer and use it in GitHub Desktop.
Love2D isometric rendering and picking
TileTable = {
{ 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1 },
{ 2, 2, 2, 2, 2, 2, 2 },
{ 3, 3, 3, 3, 3, 3, 3 },
{ 4, 4, 4, 4, 4, 4, 4 },
{ 5, 5, 5, 5, 5, 5, 5 },
{ 5, 5, 5, 5, 5, 5, 5 },
{ 5, 5, 5, 5, 5, 5, 5 },
{ 5, 5, 5, 5, 5, 5, 5 },
}
HeightTable = {
{ 5, 4, 3, 8, 0, 0, 0 },
{ 4, 1, 2, 6, 0, 0, 0 },
{ 3, 1, 3, 4, 0, 0, 0 },
{ 2, 1, 0, 3, 0, 0, 0 },
{ 1, 0, 1, 2, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
}
function love.load()
canvas = love.graphics.newCanvas()
tileset = love.graphics.newImage("tilesetmelkas.png")
Quads = {
love.graphics.newQuad(147, 73, 64, 37, 411, 234),
love.graphics.newQuad(147, 73+37, 64, 37, 411, 234),
love.graphics.newQuad(147, 73+37*2, 64, 37, 411, 234),
love.graphics.newQuad(147-64, 73+37*2, 64, 37, 411, 234),
love.graphics.newQuad(147-64*2, 73+37*2, 64, 37, 411, 234),
love.graphics.newQuad(147+64*1, 73+37*1, 64, 37, 411, 234),
}
Sides = {
love.graphics.newQuad(147+64*1, 73+37*1, 64, 37, 411, 234),
}
White = love.graphics.newQuad(147-64*2, 73+37*3, 64, 38, 411, 234)
end
Height = 0
MouseX = 0
MouseY = 0
rowX = 0
rowY = 0
function love.mousemoved(x, y)
MouseX = x
MouseY = y
r,g,b,a = canvas:getPixel(MouseX, MouseY)
if r > 0 and b > 0 and g > 0 then
Height = (255 - r) / 15
end
x = MouseX - 400
y = MouseY - 10 + (Height*5)
rowX = math.floor((y + x/2)/32 + .5) - 1
rowY = math.floor((y - x/2)/32 + .5)
end
function love.draw()
love.graphics.clear(120, 120, 120)
love.graphics.setColor( 255, 255, 255, 255)
love.graphics.print("Height: " .. tostring(Height), 0, 0)
love.graphics.print("Mouse: " .. tostring(MouseX) .. " x " .. tostring(MouseY), 0, 10)
love.graphics.print("Tile: " .. tostring(rowX) .. " x " .. tostring(rowY), 0, 20)
love.graphics.setColor( 120, 120, 120, 255)
for rowIndex=1, #TileTable do
local row = TileTable[rowIndex]
for columnIndex=1, #row do
number = row[columnIndex]
h = HeightTable[rowIndex][columnIndex]
local xpos = 400+32*columnIndex - 32*rowIndex
local ypos = 10+16*rowIndex + 16*columnIndex
love.graphics.setColor( 255, 255, 255, 255)
if h > 0 then
for x=0, h do
love.graphics.draw(tileset, Sides[1], xpos, ypos- (x*5) )
end
end
ypos = ypos - h*5
love.graphics.draw(tileset, Quads[number], xpos, ypos)
if h > 0 then
love.graphics.setColor( 0, 120-h*15, 0, 255)
if not (HeightTable[rowIndex][columnIndex-1] == nil) and (HeightTable[rowIndex][columnIndex-1] < h) then
love.graphics.line(xpos, ypos+16, xpos+32, ypos)
love.graphics.line(xpos, ypos+16, xpos, ypos+16+h*5 )
end
if not (HeightTable[rowIndex-1] == nil) and (HeightTable[rowIndex-1][columnIndex] < h) then
love.graphics.line(xpos+32, ypos, xpos+64, ypos+16 )
love.graphics.line(xpos+64, ypos+16, xpos+64, ypos+16+h*5 )
end
-- love.graphics.line(xpos+32, ypos , xpos+64, ypos+16, xpos+32, ypos+32, xpos, ypos+16, xpos+32, ypos )
-- love.graphics.line(xpos, ypos+16, xpos+32, ypos, xpos+64, ypos+16 )
end
end
end
if not (HeightTable[rowY] == nil) then
local ht = HeightTable[rowY][rowX]
if not (ht == nil) then
local cx = 400+32*rowX - 32*rowY
local cy = 10+16*rowY + 16*rowX - (ht*5)
love.graphics.setColor( 255, 255, 255, 255)
love.graphics.line(cx, cy+16, cx+32, cy)
love.graphics.line(cx, cy+16, cx, cy+16+h*5 )
love.graphics.line(cx+32, cy, cx+64, cy+16 )
love.graphics.line(cx+64, cy+16, cx+64, cy+16+h*5 )
love.graphics.line(cx+32, cy , cx+64, cy+16, cx+32, cy+32, cx, cy+16, cx+32, cy )
love.graphics.line(cx, cy+16, cx+32, cy, cx+64, cy+16 )
end
end
for rowIndex=1, #TileTable do
local row = TileTable[rowIndex]
for columnIndex=1, #row do
h = HeightTable[rowIndex][columnIndex]
local xpos = 400+32*columnIndex - 32*rowIndex
local ypos = 250+10+16*rowIndex + 16*columnIndex
love.graphics.setColor( 255-h*15, 255-h*15, 255-h*15, 255)
if h > 0 then
for x=0, h do
love.graphics.draw(tileset, White, xpos, ypos- (x*5) )
end
end
ypos = ypos - h*5
love.graphics.draw(tileset, White, xpos, ypos)
end
end
love.graphics.setCanvas(canvas)
for rowIndex=1, #TileTable do
local row = TileTable[rowIndex]
for columnIndex=1, #row do
h = HeightTable[rowIndex][columnIndex]
local xpos = 400+32*columnIndex - 32*rowIndex
local ypos = 10+16*rowIndex + 16*columnIndex
love.graphics.setColor( 255-h*15, 255-h*15, 255-h*15, 255)
if h > 0 then
for x=0, h do
love.graphics.draw(tileset, White, xpos, ypos- (x*5) )
end
end
ypos = ypos - h*5
love.graphics.draw(tileset, White, xpos, ypos)
end
end
love.graphics.setCanvas()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment