Added lua-language-server annotations

This commit is contained in:
Alexey 2025-06-25 13:05:41 +03:00
commit c7d8fa4f8b
7 changed files with 151 additions and 87 deletions

117
main.lua
View file

@ -2,31 +2,42 @@ require "grid"
require "point"
require "line"
-- Returns local coords from global
local 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
local function vectorLength( startpoint, endpoint )
return math.sqrt( ( startpoint.x - endpoint.x ) ^ 2 + ( startpoint.y - endpoint.y ) ^ 2 )
end
function love.load()
love.graphics.setLineStyle( Config.lineStyle )
love.graphics.setLineWidth( Config.lineWidth )
gameGrid = Grid:new( Point:new( 5, 5 ) )
GameGrid = Grid:new( Point:new( 5, 5 ) )
local line = Line:new(
GridPoint:new( 1, 1 ),
GridPoint:new( 3, 3 ),
Color.red
)
gameGrid:push( line )
GameGrid:push( line )
local line1 = Line:new(
GridPoint:new( 1, 5 ),
GridPoint:new( 5, 2 ),
Color.green
)
gameGrid:push( line1 )
GameGrid:push( line1 )
local line2 = Line:new(
GridPoint:new( 2, 5 ),
GridPoint:new( 5, 3 ),
Color.blue
)
gameGrid:push( line2 )
GameGrid:push( line2 )
mouse = {
Mouse = {
x = 0,
y = 0,
pressed = false,
@ -43,75 +54,65 @@ end
-- TODO: move input interactions into module
-- TODO: fix collision with other line's endpoint
function love.update( dt )
mouse.x, mouse.y = love.mouse.getPosition()
mouse.x = mouse.x + 1
mouse.y = mouse.y + 1
mouse.lastPoint = mouse.point
mouse.lastPressed = pressed
mouse.pressed = love.mouse.isDown( 1 )
mouse.point = snapCoords( Point:new( mouse.x, mouse.y ) )
if mouse.lastLine ~= nil then
local pointsLen = #mouse.lastLine.points
local lastLinePoint = mouse.lastLine.points[pointsLen]
if vectorLength( mouse.point, lastLinePoint ) == 1
and gameGrid:inBounds( mouse.point )
and gameGrid:matchesLine( mouse.point, true ) == nil
and not lastLinePoint:equals( mouse.lastLine.endpoint )
and not gameGrid:isOtherEndpoint( mouse.lastLine, mouse.point ) then
mouse.lastLine:push( mouse.point )
elseif mouse.lastLine:has( mouse.point )
and not mouse.point:equals( lastLinePoint ) then
while not mouse.lastLine.points[#mouse.lastLine.points]:equals( mouse.point ) do
mouse.lastLine:pop()
Mouse.x, Mouse.y = love.mouse.getPosition()
Mouse.x = Mouse.x + 1
Mouse.y = Mouse.y + 1
Mouse.lastPoint = Mouse.point
Mouse.lastPressed = Mouse.pressed
Mouse.pressed = love.mouse.isDown( 1 )
Mouse.point = snapCoords( Point:new( Mouse.x, Mouse.y ) )
if Mouse.lastLine ~= nil then
local pointsLen = #Mouse.lastLine.points
local lastLinePoint = Mouse.lastLine.points[pointsLen]
if vectorLength( Mouse.point, lastLinePoint ) == 1
and GameGrid:inBounds( Mouse.point )
and GameGrid:matchesLine( Mouse.point, true ) == nil
and not lastLinePoint:equals( Mouse.lastLine.endpoint )
and not GameGrid:isOtherEndpoint( Mouse.lastLine, Mouse.point ) then
Mouse.lastLine:push( Mouse.point )
elseif Mouse.lastLine:has( Mouse.point )
and not Mouse.point:equals( lastLinePoint ) then
while not Mouse.lastLine.points[#Mouse.lastLine.points]:equals( Mouse.point ) do
Mouse.lastLine:pop()
end
end
end
mouse.dragged = mouse.pressed
and mouse.startX > 0
and vectorLength(
Point:new( mouse.x, mouse.y ),
Point:new( mouse.startX, mouse.startY )
Mouse.dragged = Mouse.pressed
and Mouse.startX > 0
and vectorLength(
Point:new( Mouse.x, Mouse.y ),
Point:new( Mouse.startX, Mouse.startY )
) > Config.dragSensivity
if not mouse.pressed and mouse.startX > 0 then
mouse.startX = -1
mouse.startY = -1
mouse.lastLine = nil
if not Mouse.pressed and Mouse.startX > 0 then
Mouse.startX = -1
Mouse.startY = -1
Mouse.lastLine = nil
end
if not mouse.lastPressed
and mouse.pressed
and mouse.startX < 0 then
mouse.startX = mouse.x
mouse.startY = mouse.y
mouse.lastLine = gameGrid:matchesLine( mouse.point, false )
if not Mouse.lastPressed
and Mouse.pressed
and Mouse.startX < 0 then
Mouse.startX = Mouse.x
Mouse.startY = Mouse.y
Mouse.lastLine = GameGrid:matchesLine( Mouse.point, false )
end
end
function love.draw()
gameGrid:draw()
GameGrid:draw()
local text = string.format( "%d:%d global\n%d:%d local\n%d:%d from start", mouse.x, mouse.y, mouse.point.x, mouse.point.y, mouse.startX - mouse.x, mouse.startY - mouse.y )
local text = string.format( "%d:%d global\n%d:%d local\n%d:%d from start", Mouse.x, Mouse.y, Mouse.point.x, Mouse.point.y, Mouse.startX - Mouse.x, Mouse.startY - Mouse.y )
love.graphics.print( text, 64, 256 )
if mouse.dragged then
love.graphics.print( "drag", 64, 300 )
if Mouse.dragged then
love.graphics.print( "drag", 64, 300 )
end
if mouse.lastLine ~= nil then
love.graphics.print( tostring( mouse.lastLine ), 128, 300 )
if Mouse.lastLine ~= nil then
love.graphics.print( tostring( Mouse.lastLine ), 128, 300 )
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
function vectorLength( startpoint, endpoint )
return math.sqrt( ( startpoint.x - endpoint.x ) ^ 2 + ( startpoint.y - endpoint.y ) ^ 2 )
end