Changed drawing order and updated line connecting

This commit is contained in:
Alexey 2025-05-22 17:15:32 +03:00
commit 178043bac3
2 changed files with 23 additions and 4 deletions

View file

@ -22,20 +22,26 @@ end
-- Draw lines and the whole grid
function Grid:draw()
-- Draw lines
-- Draw grid
for x = 1, self.size.x do-- Draw lines
for _, line in ipairs( self.lines ) do
line:draw()
end
-- Draw grid
for x = 1, self.size.x do
for y = 1, self.size.y do
local px, py = x * Config.cellSize, y * Config.cellSize
love.graphics.circle( "fill", px - Config.cellSize / 2, py - Config.cellSize / 2, Config.pointRadius)
end
end
-- Draw lines
for _, line in ipairs( self.lines ) do
line:draw()
end
end
-- Check all lines and return one, which contains point (if any)
-- TODO: rewrite this function to extract clear/reverse behavior
function Grid:matchesLine( point, ignoreEnd )
local line = self.lines[tableFind( self.lines, point,
function( value, innervalue )
@ -71,6 +77,13 @@ function Grid:matchesLine( point, ignoreEnd )
return line
end
function Grid:isOtherEndpoint( line, point )
return tableHas( self.lines, { line=line, point=point }, function( value, innervalue )
return innervalue ~= value.line and innervalue.endpoint:equals( value.point )
end
)
end
function Grid:inBounds( point )
return point.x <= self.size.x and point.y <= self.size.y
end

View file

@ -56,8 +56,14 @@ function love.update( dt )
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 ) then
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