lines-lua/line.lua
2025-05-20 02:51:17 +03:00

50 lines
1.2 KiB
Lua

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 )
-- 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