diff --git a/grid.lua b/grid.lua index e201477..5faeab4 100644 --- a/grid.lua +++ b/grid.lua @@ -1,4 +1,3 @@ -require 'tablefuncs' require 'config' require 'point' @@ -107,3 +106,16 @@ end function Grid:inBounds( point ) return point.x <= self.size.x and point.y <= self.size.y end + +-- Checks if all lines have been connected and all dots are fulfilled +---@return boolean +function Grid:isCompleted() + local requiredCount, totalCount = self.size.x * self.size.y, 0 + for _, line in ipairs( self.lines ) do + if not line:endpointsConnected() then + return false + end + totalCount = totalCount + #line.points + end + return totalCount == requiredCount +end diff --git a/levels/test.lua b/levels/test.lua new file mode 100644 index 0000000..507f0c3 --- /dev/null +++ b/levels/test.lua @@ -0,0 +1,9 @@ +return { + width = 5, + height = 5, + lines = { + { 1, 1, 3, 3, "green" }, + { 1, 5, 5, 2, "red" }, + { 2, 5, 5, 3, "blue" } + } +} diff --git a/levels/test2.lua b/levels/test2.lua new file mode 100644 index 0000000..b7fb17b --- /dev/null +++ b/levels/test2.lua @@ -0,0 +1,9 @@ +return { + width = 4, + height = 4, + lines = { + { 1, 1, 2, 2, "red" }, + { 4, 1, 4, 4, "green" }, + { 1, 2, 1, 4, "blue" } + } +} diff --git a/line.lua b/line.lua index f992bcb..4208a66 100644 --- a/line.lua +++ b/line.lua @@ -8,7 +8,6 @@ require 'gridpoint' ---@field points GridPoint[] ---@field color Color Line = { - startpoint = GridPoint:new( GridPoint:coords() ), endpoint = GridPoint:new( GridPoint:coords() ), points = { }, @@ -95,3 +94,8 @@ function Line:has( point ) end ) end + +-- Check if line's endpoints are connected +function Line:endpointsConnected() + return self.points[#self.points]:equals( self.endpoint ) +end diff --git a/main.lua b/main.lua index 2232ea2..93d81f9 100644 --- a/main.lua +++ b/main.lua @@ -2,35 +2,15 @@ require 'grid' require 'point' require 'line' require 'mouse' +require 'makegrid' function love.load() love.graphics.setLineStyle( Config.lineStyle ) love.graphics.setLineWidth( Config.lineWidth ) - GameGrid = Grid:new( Point:new( 5, 5 ) ) - local line = Line:new( - GridPoint:new( 1, 1 ), - GridPoint:new( 3, 3 ), - Color.red - ) - GameGrid:push( line ) - local line1 = Line:new( - GridPoint:new( 1, 5 ), - GridPoint:new( 5, 2 ), - Color.green - ) - GameGrid:push( line1 ) - local line2 = Line:new( - GridPoint:new( 2, 5 ), - GridPoint:new( 5, 3 ), - Color.blue - ) - GameGrid:push( line2 ) + GameGrid = MakeGrid( require 'levels/test2' ) +end - end - --- TODO: move input interactions into module --- TODO: fix collision with other line's endpoint function love.update( dt ) Mouse:update() end @@ -45,6 +25,10 @@ function love.draw() love.graphics.print( "drag", 64, 300 ) end if Mouse.lastLine ~= nil then - love.graphics.print( tostring( Mouse.lastLine ), 128, 300 ) + love.graphics.print( tostring( Mouse.lastLine ), 128, 300 ) + end + + if GameGrid:isCompleted() then + love.graphics.print( "Grid completed", 64, 320 ) end end diff --git a/makegrid.lua b/makegrid.lua new file mode 100644 index 0000000..c1a6747 --- /dev/null +++ b/makegrid.lua @@ -0,0 +1,39 @@ +require 'grid' +require 'line' +require 'point' + +-- Constructs line from { start x, start y, end x, end y, color } +---@param table table +---@return Line +local function makeLine( table ) + local startpoint = GridPoint:new( table[1], table[2] ) + local endpoint = GridPoint:new( table[3], table[4] ) + return Line:new( startpoint, endpoint, Color[table[5]] ) +end + +-- Constructs all game objects using data table +-- table = { +-- -- Size +-- width = 5, +-- height = 5, +-- -- Lines +-- lines = { +-- -- start x, start y, end x, end y, color +-- { 1, 1, 3, 3, "red" }, +-- { 1, 5, 5, 2, "green" }, +-- { 2, 5, 5, 3, "blue" } +-- } +-- } +---@param table table +---@return Grid +function MakeGrid( table ) + local size = Point:new( table.width, table.height ) + local grid = Grid:new( size ) + + for _, lineData in ipairs( table.lines ) do + local line = makeLine( lineData ) + grid:push( line ) + end + + return grid +end