diff --git a/grid.lua b/grid.lua index 3981712..8f822e9 100644 --- a/grid.lua +++ b/grid.lua @@ -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 diff --git a/main.lua b/main.lua index 2eda39e..99d0704 100644 --- a/main.lua +++ b/main.lua @@ -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