Leveling system
This commit is contained in:
parent
362a4e39ee
commit
efe9f386bd
6 changed files with 83 additions and 26 deletions
14
grid.lua
14
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
|
||||
|
|
9
levels/test.lua
Normal file
9
levels/test.lua
Normal 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
9
levels/test2.lua
Normal 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" }
|
||||
}
|
||||
}
|
6
line.lua
6
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
|
||||
|
|
28
main.lua
28
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
|
||||
|
||||
-- TODO: move input interactions into module
|
||||
-- TODO: fix collision with other line's endpoint
|
||||
function love.update( dt )
|
||||
Mouse:update()
|
||||
end
|
||||
|
@ -47,4 +27,8 @@ function love.draw()
|
|||
if Mouse.lastLine ~= nil then
|
||||
love.graphics.print( tostring( Mouse.lastLine ), 128, 300 )
|
||||
end
|
||||
|
||||
if GameGrid:isCompleted() then
|
||||
love.graphics.print( "Grid completed", 64, 320 )
|
||||
end
|
||||
end
|
||||
|
|
39
makegrid.lua
Normal file
39
makegrid.lua
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue