I like taking advantage of Löve built-in objects. Such a pity that
don't support 3D coordinates, even though the matrix is a complete 4x4 one. It's as if Löve is not made for isometric games.
Code: Select all
local map = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,2,0,1,0,0,3,0,0,0,1,1,1,0,1,
1,0,2,0,1,0,0,0,0,0,0,1,4,0,0,1,
1,0,2,0,1,0,0,3,0,0,0,1,1,0,0,1,
1,0,0,0,1,0,0,3,0,0,0,0,1,0,0,1,
1,0,0,0,0,0,0,0,0,1,0,0,1,1,0,1,
1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,1,1,0,0,2,2,0,1,
1,0,1,1,1,0,0,0,1,0,0,0,2,2,0,1,
1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
}
local tf = love.math.newTransform()
local x0, y0 = 400, 300 -- draw position
local ts = 16 -- tile size - better if even
local hix = -1
local hiy = -1
function love.keypressed(k) return k == "escape" and love.event.quit() end
function love.mousemoved(x, y)
hix, hiy = tf:inverseTransformPoint(x, y)
hix, hiy = math.floor(hix), math.floor(hiy)
end
function love.update(dt)
tf:setMatrix( ts, -ts, 0, x0,
.5*ts, .5*ts, -ts, y0,
0, 0, 1, 0,
0, 0, 0, 1)
end
function love.draw()
local p = 0
for y = 0, 11 do
for x = 0, 15 do
p = p + 1
local tile = map[p]
-- Corners of a rectangle, for drawing the tiles. Drawing rectangles
-- is much harder than drawing sprites in this way, because every
-- corner needs to be transformed, and not just the tile coords.
-- Tiles are drawn with a 2 pixel margin on each side (total 4 pixels
-- between tiles) before projection.
local left = x + 2/ts
local top = y + 2/ts
local right = x + 1 - 2/ts
local bottom = y + 1 - 2/ts
-- Transform them
local p1x, p1y = tf:transformPoint(left, top) -- top left corner
local p2x, p2y = tf:transformPoint(right, top) -- top right corner
local p3x, p3y = tf:transformPoint(right, bottom) -- bottom right corner
local p4x, p4y = tf:transformPoint(left, bottom) -- bottom left corner
if x == hix and y == hiy then
love.graphics.setColor(1, 1, 1)
elseif tile == 0 then
love.graphics.setColor(0.7, 0.7, 0.7)
elseif tile == 1 then
love.graphics.setColor(0.5, 0.5, 0)
elseif tile == 2 then
love.graphics.setColor(0, 0.7, 0)
elseif tile == 3 then
love.graphics.setColor(0, 0.5, 0.6)
elseif tile == 4 then
love.graphics.setColor(1, 0.4, 0.4)
else
print("Unknown tile")
love.event.quit()
end
love.graphics.polygon("fill", p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y)
end
end
love.graphics.setColor(1, 1, 1)
end
The transform is set in love.update with the idea that x0 and y0 can be scrolled at will, even if this short demo doesn't do it.