Little refactoring
This commit is contained in:
parent
d2308917b4
commit
8a8db93ac1
7 changed files with 67 additions and 53 deletions
30
config.lua
30
config.lua
|
@ -1,3 +1,5 @@
|
||||||
|
require 'tablefuncs'
|
||||||
|
|
||||||
-- Global config table
|
-- Global config table
|
||||||
---@class Config
|
---@class Config
|
||||||
---@field pointRadius number Radius of each grid point
|
---@field pointRadius number Radius of each grid point
|
||||||
|
@ -27,31 +29,3 @@ Color = {
|
||||||
green = { 0, 1, 0 },
|
green = { 0, 1, 0 },
|
||||||
blue = { 0, 0, 1 }
|
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
|
|
||||||
|
|
5
grid.lua
5
grid.lua
|
@ -1,5 +1,6 @@
|
||||||
require "config"
|
require 'tablefuncs'
|
||||||
require "point"
|
require 'config'
|
||||||
|
require 'point'
|
||||||
|
|
||||||
-- Game grid table, acts as "global level" of some sort
|
-- Game grid table, acts as "global level" of some sort
|
||||||
---@class Grid
|
---@class Grid
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
require "point"
|
require 'point'
|
||||||
require "config"
|
require 'config'
|
||||||
|
|
||||||
-- Point table with grid-based coordinates
|
-- Point table with grid-based coordinates
|
||||||
---@class GridPoint: Point
|
---@class GridPoint: Point
|
||||||
|
@ -17,5 +17,15 @@ end
|
||||||
-- Same as coords, but converted to global coords
|
-- Same as coords, but converted to global coords
|
||||||
---@return number, number
|
---@return number, number
|
||||||
function GridPoint:globalCoords()
|
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
|
end
|
||||||
|
|
3
line.lua
3
line.lua
|
@ -1,4 +1,5 @@
|
||||||
require "gridpoint"
|
require 'tablefuncs'
|
||||||
|
require 'gridpoint'
|
||||||
|
|
||||||
-- Interactive game object
|
-- Interactive game object
|
||||||
---@class Line
|
---@class Line
|
||||||
|
|
28
main.lua
28
main.lua
|
@ -1,17 +1,6 @@
|
||||||
require "grid"
|
require 'grid'
|
||||||
require "point"
|
require 'point'
|
||||||
require "line"
|
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
|
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
love.graphics.setLineStyle( Config.lineStyle )
|
love.graphics.setLineStyle( Config.lineStyle )
|
||||||
|
@ -37,6 +26,9 @@ function love.load()
|
||||||
)
|
)
|
||||||
GameGrid:push( line2 )
|
GameGrid:push( line2 )
|
||||||
|
|
||||||
|
---@class Mouse
|
||||||
|
---@field lastLine Line?
|
||||||
|
---@field lastPoint Point?
|
||||||
Mouse = {
|
Mouse = {
|
||||||
x = 0,
|
x = 0,
|
||||||
y = 0,
|
y = 0,
|
||||||
|
@ -60,11 +52,11 @@ function love.update( dt )
|
||||||
Mouse.lastPoint = Mouse.point
|
Mouse.lastPoint = Mouse.point
|
||||||
Mouse.lastPressed = Mouse.pressed
|
Mouse.lastPressed = Mouse.pressed
|
||||||
Mouse.pressed = love.mouse.isDown( 1 )
|
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
|
if Mouse.lastLine ~= nil then
|
||||||
local pointsLen = #Mouse.lastLine.points
|
local pointsLen = #Mouse.lastLine.points
|
||||||
local lastLinePoint = Mouse.lastLine.points[pointsLen]
|
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:inBounds( Mouse.point )
|
||||||
and GameGrid:matchesLine( Mouse.point, true ) == nil
|
and GameGrid:matchesLine( Mouse.point, true ) == nil
|
||||||
and not lastLinePoint:equals( Mouse.lastLine.endpoint )
|
and not lastLinePoint:equals( Mouse.lastLine.endpoint )
|
||||||
|
@ -81,7 +73,7 @@ function love.update( dt )
|
||||||
|
|
||||||
Mouse.dragged = Mouse.pressed
|
Mouse.dragged = Mouse.pressed
|
||||||
and Mouse.startX > 0
|
and Mouse.startX > 0
|
||||||
and vectorLength(
|
and Point.distanceTo (
|
||||||
Point:new( Mouse.x, Mouse.y ),
|
Point:new( Mouse.x, Mouse.y ),
|
||||||
Point:new( Mouse.startX, Mouse.startY )
|
Point:new( Mouse.startX, Mouse.startY )
|
||||||
) > Config.dragSensivity
|
) > Config.dragSensivity
|
||||||
|
@ -114,5 +106,3 @@ function love.draw()
|
||||||
love.graphics.print( tostring( Mouse.lastLine ), 128, 300 )
|
love.graphics.print( tostring( Mouse.lastLine ), 128, 300 )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
11
point.lua
11
point.lua
|
@ -29,3 +29,14 @@ end
|
||||||
function Point:coords()
|
function Point:coords()
|
||||||
return self.x, self.y
|
return self.x, self.y
|
||||||
end
|
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
27
tablefuncs.lua
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue