require "gridpoint" -- Line table, interactive game object -- NOTE: uses GridPoint Line = {} -- Factory function function Line:new( startpoint, endpoint, color ) local line = { startpoint = startpoint, endpoint = endpoint, points = { startpoint }, color = color } setmetatable( line, { __index = self } ) return line end -- Push point to the end of points stack function Line:push( point ) table.insert( self.points, point ) end -- Pop point from points stack function Line:pop() return table.remove( self.points ) end -- Draw line with connected dots function Line:draw() love.graphics.setColor( self.color ) -- Draw start and end points (should look like rings) local sx, sy = self.startpoint:globalCoords() love.graphics.circle( "fill", sx, sy, Config.linePointRadius ) local ex, ey = self.endpoint:globalCoords() love.graphics.circle( "fill", ex, ey, Config.linePointRadius ) if #self.points <= 1 then love.graphics.setColor( Color.white ) return end -- Draw line local points = {} for _, point in ipairs( self.points ) do local x, y = point:globalCoords() table.insert( points, x ) table.insert( points, y ) end love.graphics.line( points ) love.graphics.setColor( Color.white ) end -- Clear points function Line:clear() for k, _ in pairs( self.points ) do self.points[k] = nil end end -- Swap startpoint and endpoint function Line:reverse() local swap = self.startpoint self.startpoint = self.endpoint self.endpoint = swap self:push( self.startpoint ) end -- Check if line has the exact point function Line:has( point ) return tableHas( self.points, point, function( value, innervalue ) return innervalue:equals( value ) end ) end