From 8a8db93ac1769c6d83927dd1d64c4add6fd50278 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Wed, 25 Jun 2025 14:01:04 +0300 Subject: [PATCH] Little refactoring --- config.lua | 30 ++---------------------------- grid.lua | 5 +++-- gridpoint.lua | 16 +++++++++++++--- line.lua | 3 ++- main.lua | 28 +++++++++------------------- point.lua | 11 +++++++++++ tablefuncs.lua | 27 +++++++++++++++++++++++++++ 7 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 tablefuncs.lua diff --git a/config.lua b/config.lua index cf774fd..c582db6 100644 --- a/config.lua +++ b/config.lua @@ -1,3 +1,5 @@ +require 'tablefuncs' + -- Global config table ---@class Config ---@field pointRadius number Radius of each grid point @@ -27,31 +29,3 @@ Color = { green = { 0, 1, 0 }, blue = { 0, 0, 1 } } - --- Find key by value in table by predicate ----@param table table ----@param value any ----@param pred function ----@return any -function TableFind( table, value, pred ) - for key, innervalue in pairs( table ) do - if pred( value, innervalue ) then - return key - end - end - return nil -end - --- Find if value exists in table by predicate ----@param table table ----@param value any ----@param pred function ----@return boolean -function TableHas( table, value, pred ) - for _, innervalue in pairs( table ) do - if pred( value, innervalue ) then - return true - end - end - return false -end diff --git a/grid.lua b/grid.lua index c8561cf..e201477 100644 --- a/grid.lua +++ b/grid.lua @@ -1,5 +1,6 @@ -require "config" -require "point" +require 'tablefuncs' +require 'config' +require 'point' -- Game grid table, acts as "global level" of some sort ---@class Grid diff --git a/gridpoint.lua b/gridpoint.lua index 735390a..eff5a8f 100644 --- a/gridpoint.lua +++ b/gridpoint.lua @@ -1,5 +1,5 @@ -require "point" -require "config" +require 'point' +require 'config' -- Point table with grid-based coordinates ---@class GridPoint: Point @@ -17,5 +17,15 @@ end -- Same as coords, but converted to global coords ---@return number, number function GridPoint:globalCoords() - return self.x * Config.cellSize - Config.cellSize / 2, self.y * Config.cellSize - Config.cellSize / 2 + return self.x * Config.cellSize - Config.cellSize / 2, + self.y * Config.cellSize - Config.cellSize / 2 +end + +-- Returns local coords from global +---@param point Point +---@return GridPoint +function GridPoint.snapCoords( point ) + local x = math.ceil( point.x / Config.cellSize ) + local y = math.ceil( point.y / Config.cellSize ) + return GridPoint:new( x, y ) end diff --git a/line.lua b/line.lua index d0058ec..f992bcb 100644 --- a/line.lua +++ b/line.lua @@ -1,4 +1,5 @@ -require "gridpoint" +require 'tablefuncs' +require 'gridpoint' -- Interactive game object ---@class Line diff --git a/main.lua b/main.lua index 1907390..8820ecc 100644 --- a/main.lua +++ b/main.lua @@ -1,17 +1,6 @@ -require "grid" -require "point" -require "line" - --- Returns local coords from global -local function snapCoords( point ) - local x = math.ceil( point.x / Config.cellSize ) - local y = math.ceil( point.y / Config.cellSize ) - return GridPoint:new( x, y ) -end - -local function vectorLength( startpoint, endpoint ) - return math.sqrt( ( startpoint.x - endpoint.x ) ^ 2 + ( startpoint.y - endpoint.y ) ^ 2 ) -end +require 'grid' +require 'point' +require 'line' function love.load() love.graphics.setLineStyle( Config.lineStyle ) @@ -37,6 +26,9 @@ function love.load() ) GameGrid:push( line2 ) + ---@class Mouse + ---@field lastLine Line? + ---@field lastPoint Point? Mouse = { x = 0, y = 0, @@ -60,11 +52,11 @@ function love.update( dt ) Mouse.lastPoint = Mouse.point Mouse.lastPressed = Mouse.pressed Mouse.pressed = love.mouse.isDown( 1 ) - Mouse.point = snapCoords( Point:new( Mouse.x, Mouse.y ) ) + Mouse.point = GridPoint.snapCoords( Point:new( Mouse.x, Mouse.y ) ) if Mouse.lastLine ~= nil then local pointsLen = #Mouse.lastLine.points local lastLinePoint = Mouse.lastLine.points[pointsLen] - if vectorLength( Mouse.point, lastLinePoint ) == 1 + if Mouse.point:distanceTo( lastLinePoint ) == 1 and GameGrid:inBounds( Mouse.point ) and GameGrid:matchesLine( Mouse.point, true ) == nil and not lastLinePoint:equals( Mouse.lastLine.endpoint ) @@ -81,7 +73,7 @@ function love.update( dt ) Mouse.dragged = Mouse.pressed and Mouse.startX > 0 - and vectorLength( + and Point.distanceTo ( Point:new( Mouse.x, Mouse.y ), Point:new( Mouse.startX, Mouse.startY ) ) > Config.dragSensivity @@ -114,5 +106,3 @@ function love.draw() love.graphics.print( tostring( Mouse.lastLine ), 128, 300 ) end end - - diff --git a/point.lua b/point.lua index 4a85d06..3efb509 100644 --- a/point.lua +++ b/point.lua @@ -29,3 +29,14 @@ end function Point:coords() return self.x, self.y end + +-- Return vector length between points +---@param point Point +---@return number +function Point:distanceTo( point ) + return math.sqrt( + ( self.x - point.x ) ^ 2 + + ( self.y - point.y ) ^ 2 + ) +end + diff --git a/tablefuncs.lua b/tablefuncs.lua new file mode 100644 index 0000000..e478747 --- /dev/null +++ b/tablefuncs.lua @@ -0,0 +1,27 @@ +-- Find key by value in table by predicate +---@param table table +---@param value any +---@param pred function +---@return any +function TableFind( table, value, pred ) + for key, innervalue in pairs( table ) do + if pred( value, innervalue ) then + return key + end + end + return nil +end + +-- Find if value exists in table by predicate +---@param table table +---@param value any +---@param pred function +---@return boolean +function TableHas( table, value, pred ) + for _, innervalue in pairs( table ) do + if pred( value, innervalue ) then + return true + end + end + return false +end