Leveling system

This commit is contained in:
Alexey 2025-06-25 16:51:23 +03:00
commit efe9f386bd
6 changed files with 83 additions and 26 deletions

View file

@ -1,4 +1,3 @@
require 'tablefuncs'
require 'config' require 'config'
require 'point' require 'point'
@ -107,3 +106,16 @@ end
function Grid:inBounds( point ) function Grid:inBounds( point )
return point.x <= self.size.x and point.y <= self.size.y return point.x <= self.size.x and point.y <= self.size.y
end 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

9
levels/test.lua Normal file
View file

@ -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" }
}
}

9
levels/test2.lua Normal file
View file

@ -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" }
}
}

View file

@ -8,7 +8,6 @@ require 'gridpoint'
---@field points GridPoint[] ---@field points GridPoint[]
---@field color Color ---@field color Color
Line = { Line = {
startpoint = GridPoint:new( GridPoint:coords() ), startpoint = GridPoint:new( GridPoint:coords() ),
endpoint = GridPoint:new( GridPoint:coords() ), endpoint = GridPoint:new( GridPoint:coords() ),
points = { }, points = { },
@ -95,3 +94,8 @@ function Line:has( point )
end end
) )
end end
-- Check if line's endpoints are connected
function Line:endpointsConnected()
return self.points[#self.points]:equals( self.endpoint )
end

View file

@ -2,35 +2,15 @@ require 'grid'
require 'point' require 'point'
require 'line' require 'line'
require 'mouse' require 'mouse'
require 'makegrid'
function love.load() function love.load()
love.graphics.setLineStyle( Config.lineStyle ) love.graphics.setLineStyle( Config.lineStyle )
love.graphics.setLineWidth( Config.lineWidth ) love.graphics.setLineWidth( Config.lineWidth )
GameGrid = Grid:new( Point:new( 5, 5 ) ) GameGrid = MakeGrid( require 'levels/test2' )
local line = Line:new( end
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 )
end
-- TODO: move input interactions into module
-- TODO: fix collision with other line's endpoint
function love.update( dt ) function love.update( dt )
Mouse:update() Mouse:update()
end end
@ -45,6 +25,10 @@ function love.draw()
love.graphics.print( "drag", 64, 300 ) love.graphics.print( "drag", 64, 300 )
end end
if Mouse.lastLine ~= nil then 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
end end

39
makegrid.lua Normal file
View file

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