Little refactoring

This commit is contained in:
Alexey 2025-06-25 14:01:04 +03:00
commit 8a8db93ac1
7 changed files with 67 additions and 53 deletions

View file

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

View file

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

View file

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

View file

@ -1,4 +1,5 @@
require "gridpoint"
require 'tablefuncs'
require 'gridpoint'
-- Interactive game object
---@class Line

View file

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

View file

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

27
tablefuncs.lua Normal file
View file

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