I now have a basic drag and drop. It's NOT great but t's a start.
Any pointers would be great before I move further into learning this code.
The code is below but probably wont work without the tiles image so I have also attached the .love file.
Code: Select all
function love.load()
mouse_dx = nil
mouse_dy = nil
mouse_down = false
mouse_quad = nil
tiles = love.graphics.newImage("tiles.png") -- a 64px*64px canvas with 16 x 16px*16px tiles arragnged as 4*4 tiles
quads = {} -- 16 quads of 16px x 16px
local x
local y
for y = 0, 3 do
for x = 0, 3 do
quads[y * 4 + x + 1] = love.graphics.newQuad(x * 16, y * 16 , 16, 16, 64, 64)
end
end
display = {} -- 50 tiles x 37.5 tiles of 16px x 16px for screen res 800px x 600px
for y = 0, 37 do
for x = 0, 49 do
display[y * 50 + x + 1] = quads[1] -- set the quad to the default background quad
if x > 16 and x < 33 and y > 9 and y < 27 then -- if in the droppable area
display[y * 50 + x + 1] = quads[2] -- set the quad to the grid quad
end
if x > 45 and x < 48 and y > 13 and y < 22 then -- if in the pickup area
display[y * 50 + x + 1] = quads[(y - 14) * 2 + x - 45] -- set the quad to one of the sixteen
end
end
end
end
function love.update()
end
function love.draw()
love.graphics.draw(tiles, 0, 0, 0, 1, 1, 0, 0, 0, 0)
local x
local y
for y = 0, 37 do
for x = 0, 49 do
love.graphics.drawq(tiles, display[y * 50 + x + 1], x * 16, y * 16 , 0, 1, 1, 0, 0, 0, 0)
end
end
if mouse_down == true then
love.graphics.drawq(tiles, mouse_quad, love.mouse.getX() + mouse_dx, love.mouse.getY() + mouse_dy, 0, 1, 1, 0, 0, 0, 0)
end
end
function love.mousepressed(x, y, button)
if button == 'l' then
local tile_x = math.floor(x / 16)
local tile_y = math.floor(y / 16)
if tile_x > 45 and tile_x < 48 and tile_y > 13 and tile_y < 22 then -- if in the pickup area
mouse_quad = quads[(tile_y - 14) * 2 + tile_x - 45]
mouse_dx = tile_x * 16 - x
mouse_dy = tile_y * 16 - y
mouse_down = true
end
end
end
function love.mousereleased(x, y, button)
if button == 'l' and mouse_down == true then
mouse_down = false
local tile_x = math.floor(x / 16)
local tile_y = math.floor(y / 16)
if tile_x > 16 and tile_x < 33 and tile_y > 9 and tile_y < 27 then -- if in the droppable area
display[tile_y * 50 + tile_x + 1] = mouse_quad -- drop the quad into the display grid
end
end
end