Snapping mouse to grid
This commit is contained in:
parent
f765ec74ef
commit
74fdcd51f7
4 changed files with 29 additions and 11 deletions
32
main.lua
32
main.lua
|
|
@ -12,21 +12,35 @@ function love.load()
|
|||
GridPoint:new( 3, 3 ),
|
||||
Color.red
|
||||
)
|
||||
line:push( GridPoint:new( 1, 2 ) )
|
||||
line:push( GridPoint:new( 1, 3 ) )
|
||||
line:push( GridPoint:new( 2, 3 ) )
|
||||
line:push( GridPoint:new( 2, 2 ) )
|
||||
line:push( GridPoint:new( 2, 1 ) )
|
||||
line:push( GridPoint:new( 3, 1 ) )
|
||||
line:push( GridPoint:new( 3, 2 ) )
|
||||
line:push( GridPoint:new( 3, 3 ) )
|
||||
gameGrid:push( line )
|
||||
mouse = {
|
||||
x = 0,
|
||||
y = 0,
|
||||
pressed = false,
|
||||
lastPressed = false
|
||||
}
|
||||
end
|
||||
|
||||
-- TODO: move input interactions into module
|
||||
function love.update( dt )
|
||||
|
||||
mouse.x, mouse.y = love.mouse.getPosition()
|
||||
mouse.lastPressed = pressed
|
||||
mouse.pressed = love.mouse.isDown( 1 )
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
gameGrid:draw()
|
||||
|
||||
if mouse.pressed then
|
||||
local x, y = snapCoords( Point:new( mouse.x, mouse.y ) ):coords()
|
||||
local text = string.format( "%d:%d global\n%d:%d local", mouse.x, mouse.y, x, y )
|
||||
love.graphics.print( text, 64, 256 )
|
||||
end
|
||||
end
|
||||
|
||||
-- Returns local coords from global
|
||||
function snapCoords( point )
|
||||
local x = math.ceil( point.x / Config.cellSize )
|
||||
local y = math.ceil( point.y / Config.cellSize )
|
||||
return GridPoint:new( x, y )
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue