Added lua-language-server annotations
This commit is contained in:
parent
178043bac3
commit
c7d8fa4f8b
7 changed files with 151 additions and 87 deletions
4
.luarc.json
Normal file
4
.luarc.json
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"$SCHEMA": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
|
||||||
|
"diagnostics.globals": [ "love" ]
|
||||||
|
}
|
29
config.lua
29
config.lua
|
@ -1,21 +1,26 @@
|
||||||
-- Global config table
|
-- Global config table
|
||||||
|
---@class Config
|
||||||
|
---@field pointRadius number Radius of each grid point
|
||||||
|
---@field linePointRadius number Radius of line start/end point
|
||||||
|
---@field cellSize number Size of each grid cell
|
||||||
|
---@field lineStyle string love2d line style setting
|
||||||
|
---@field lineWidth number love2d line width setting
|
||||||
|
---@field dragSensivity number drag sensivity, px
|
||||||
Config = {
|
Config = {
|
||||||
-- Radius of each grid point
|
|
||||||
pointRadius = 7,
|
pointRadius = 7,
|
||||||
-- Radius of line start/end point
|
|
||||||
linePointRadius = 10,
|
linePointRadius = 10,
|
||||||
-- Size of each grid cell
|
|
||||||
cellSize = 30,
|
cellSize = 30,
|
||||||
|
|
||||||
-- love2d line drawing settings
|
|
||||||
lineStyle = "smooth",
|
lineStyle = "smooth",
|
||||||
lineWidth = 5,
|
lineWidth = 5,
|
||||||
|
|
||||||
-- drag sensivity, px
|
|
||||||
dragSensivity = 5
|
dragSensivity = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Colors table
|
-- Colors table
|
||||||
|
---@enum Color
|
||||||
Color = {
|
Color = {
|
||||||
white = { 1, 1, 1 },
|
white = { 1, 1, 1 },
|
||||||
red = { 1, 0, 0 },
|
red = { 1, 0, 0 },
|
||||||
|
@ -24,7 +29,11 @@ Color = {
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Find key by value in table by predicate
|
-- Find key by value in table by predicate
|
||||||
function tableFind( table, value, pred )
|
---@param table table
|
||||||
|
---@param value any
|
||||||
|
---@param pred function
|
||||||
|
---@return any
|
||||||
|
function TableFind( table, value, pred )
|
||||||
for key, innervalue in pairs( table ) do
|
for key, innervalue in pairs( table ) do
|
||||||
if pred( value, innervalue ) then
|
if pred( value, innervalue ) then
|
||||||
return key
|
return key
|
||||||
|
@ -34,8 +43,12 @@ function tableFind( table, value, pred )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Find if value exists in table by predicate
|
-- Find if value exists in table by predicate
|
||||||
function tableHas( table, value, pred )
|
---@param table table
|
||||||
for key, innervalue in pairs( table ) do
|
---@param value any
|
||||||
|
---@param pred function
|
||||||
|
---@return boolean
|
||||||
|
function TableHas( table, value, pred )
|
||||||
|
for _, innervalue in pairs( table ) do
|
||||||
if pred( value, innervalue ) then
|
if pred( value, innervalue ) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
26
grid.lua
26
grid.lua
|
@ -1,9 +1,17 @@
|
||||||
require "config"
|
require "config"
|
||||||
|
|
||||||
-- Game grid table, acts as "global level" of some sort
|
-- Game grid table, acts as "global level" of some sort
|
||||||
Grid = {}
|
---@class Grid
|
||||||
|
---@field lines Line[]
|
||||||
|
---@field size Point
|
||||||
|
Grid = {
|
||||||
|
lines = {},
|
||||||
|
size = Point:new( Point:coords() )
|
||||||
|
}
|
||||||
|
|
||||||
-- Factory function
|
-- Factory function
|
||||||
|
---@param size Point
|
||||||
|
---@returns Grid
|
||||||
function Grid:new( size )
|
function Grid:new( size )
|
||||||
local grid = {
|
local grid = {
|
||||||
lines = {},
|
lines = {},
|
||||||
|
@ -16,6 +24,7 @@ function Grid:new( size )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add new line to grid
|
-- Add new line to grid
|
||||||
|
---@param line Line
|
||||||
function Grid:push( line )
|
function Grid:push( line )
|
||||||
table.insert( self.lines, line )
|
table.insert( self.lines, line )
|
||||||
end
|
end
|
||||||
|
@ -42,8 +51,10 @@ end
|
||||||
|
|
||||||
-- Check all lines and return one, which contains point (if any)
|
-- Check all lines and return one, which contains point (if any)
|
||||||
-- TODO: rewrite this function to extract clear/reverse behavior
|
-- TODO: rewrite this function to extract clear/reverse behavior
|
||||||
|
---@param point Point
|
||||||
|
---@param ignoreEnd boolean
|
||||||
function Grid:matchesLine( point, ignoreEnd )
|
function Grid:matchesLine( point, ignoreEnd )
|
||||||
local line = self.lines[tableFind( self.lines, point,
|
local line = self.lines[TableFind( self.lines, point,
|
||||||
function( value, innervalue )
|
function( value, innervalue )
|
||||||
return innervalue:has( value )
|
return innervalue:has( value )
|
||||||
end
|
end
|
||||||
|
@ -63,7 +74,7 @@ function Grid:matchesLine( point, ignoreEnd )
|
||||||
|
|
||||||
-- Try to check endpoint
|
-- Try to check endpoint
|
||||||
if line == nil and not ignoreEnd then
|
if line == nil and not ignoreEnd then
|
||||||
line = self.lines[tableFind( self.lines, point,
|
line = self.lines[TableFind( self.lines, point,
|
||||||
function( value, innervalue )
|
function( value, innervalue )
|
||||||
if innervalue.endpoint:equals( value ) then
|
if innervalue.endpoint:equals( value ) then
|
||||||
innervalue:clear()
|
innervalue:clear()
|
||||||
|
@ -77,13 +88,20 @@ function Grid:matchesLine( point, ignoreEnd )
|
||||||
return line
|
return line
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Checks if specified point does belong to any line endpoint but not specified line
|
||||||
|
---@param line Line
|
||||||
|
---@param point GridPoint
|
||||||
|
---@return boolean
|
||||||
function Grid:isOtherEndpoint( line, point )
|
function Grid:isOtherEndpoint( line, point )
|
||||||
return tableHas( self.lines, { line=line, point=point }, function( value, innervalue )
|
return TableHas( self.lines, { line=line, point=point }, function( value, innervalue )
|
||||||
return innervalue ~= value.line and innervalue.endpoint:equals( value.point )
|
return innervalue ~= value.line and innervalue.endpoint:equals( value.point )
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Checks if point is in grid bounds
|
||||||
|
---@param point GridPoint
|
||||||
|
---@return boolean
|
||||||
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
|
||||||
|
|
|
@ -2,9 +2,11 @@ require "point"
|
||||||
require "config"
|
require "config"
|
||||||
|
|
||||||
-- Point table with grid-based coordinates
|
-- Point table with grid-based coordinates
|
||||||
GridPoint = Point:new()
|
---@class GridPoint: Point
|
||||||
|
GridPoint = Point
|
||||||
|
|
||||||
-- Convert grid x, y to global x, y
|
-- Convert grid x, y to global x, y
|
||||||
|
---@return Point
|
||||||
function GridPoint:absolute()
|
function GridPoint:absolute()
|
||||||
return Point:new(
|
return Point:new(
|
||||||
self.x * Config.cellSize,
|
self.x * Config.cellSize,
|
||||||
|
@ -13,6 +15,7 @@ function GridPoint:absolute()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Same as coords, but converted to global coords
|
-- Same as coords, but converted to global coords
|
||||||
|
---@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
|
end
|
||||||
|
|
34
line.lua
34
line.lua
|
@ -1,18 +1,32 @@
|
||||||
require "gridpoint"
|
require "gridpoint"
|
||||||
|
|
||||||
-- Line table, interactive game object
|
-- Interactive game object
|
||||||
-- NOTE: uses GridPoint
|
---@class Line
|
||||||
Line = {}
|
---@field startpoint GridPoint
|
||||||
|
---@field endpoint GridPoint
|
||||||
|
---@field points GridPoint[]
|
||||||
|
---@field color Color
|
||||||
|
Line = {
|
||||||
|
|
||||||
|
startpoint = GridPoint:new( GridPoint:coords() ),
|
||||||
|
endpoint = GridPoint:new( GridPoint:coords() ),
|
||||||
|
points = { },
|
||||||
|
color = Color.red
|
||||||
|
}
|
||||||
|
|
||||||
-- Factory function
|
-- Factory function
|
||||||
|
---@param startpoint GridPoint
|
||||||
|
---@param endpoint GridPoint
|
||||||
|
---@param color Color
|
||||||
|
---@return Line
|
||||||
function Line:new( startpoint, endpoint, color )
|
function Line:new( startpoint, endpoint, color )
|
||||||
local line = {
|
local line = {
|
||||||
startpoint = startpoint,
|
startpoint = startpoint or self.startpoint,
|
||||||
endpoint = endpoint,
|
endpoint = endpoint or self.endpoint,
|
||||||
points = {
|
points = {
|
||||||
startpoint
|
startpoint or self.startpoint
|
||||||
},
|
},
|
||||||
color = color
|
color = color or self.color
|
||||||
}
|
}
|
||||||
|
|
||||||
setmetatable( line, { __index = self } )
|
setmetatable( line, { __index = self } )
|
||||||
|
@ -21,11 +35,13 @@ function Line:new( startpoint, endpoint, color )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Push point to the end of points stack
|
-- Push point to the end of points stack
|
||||||
|
---@param point GridPoint
|
||||||
function Line:push( point )
|
function Line:push( point )
|
||||||
table.insert( self.points, point )
|
table.insert( self.points, point )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Pop point from points stack
|
-- Pop point from points stack
|
||||||
|
---@return GridPoint
|
||||||
function Line:pop()
|
function Line:pop()
|
||||||
return table.remove( self.points )
|
return table.remove( self.points )
|
||||||
end
|
end
|
||||||
|
@ -69,8 +85,10 @@ function Line:reverse()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if line has the exact point
|
-- Check if line has the exact point
|
||||||
|
---@param point GridPoint
|
||||||
|
---@return boolean
|
||||||
function Line:has( point )
|
function Line:has( point )
|
||||||
return tableHas( self.points, point,
|
return TableHas( self.points, point,
|
||||||
function( value, innervalue )
|
function( value, innervalue )
|
||||||
return innervalue:equals( value )
|
return innervalue:equals( value )
|
||||||
end
|
end
|
||||||
|
|
109
main.lua
109
main.lua
|
@ -2,31 +2,42 @@ 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 )
|
||||||
love.graphics.setLineWidth( Config.lineWidth )
|
love.graphics.setLineWidth( Config.lineWidth )
|
||||||
|
|
||||||
gameGrid = Grid:new( Point:new( 5, 5 ) )
|
GameGrid = Grid:new( Point:new( 5, 5 ) )
|
||||||
local line = Line:new(
|
local line = Line:new(
|
||||||
GridPoint:new( 1, 1 ),
|
GridPoint:new( 1, 1 ),
|
||||||
GridPoint:new( 3, 3 ),
|
GridPoint:new( 3, 3 ),
|
||||||
Color.red
|
Color.red
|
||||||
)
|
)
|
||||||
gameGrid:push( line )
|
GameGrid:push( line )
|
||||||
local line1 = Line:new(
|
local line1 = Line:new(
|
||||||
GridPoint:new( 1, 5 ),
|
GridPoint:new( 1, 5 ),
|
||||||
GridPoint:new( 5, 2 ),
|
GridPoint:new( 5, 2 ),
|
||||||
Color.green
|
Color.green
|
||||||
)
|
)
|
||||||
gameGrid:push( line1 )
|
GameGrid:push( line1 )
|
||||||
local line2 = Line:new(
|
local line2 = Line:new(
|
||||||
GridPoint:new( 2, 5 ),
|
GridPoint:new( 2, 5 ),
|
||||||
GridPoint:new( 5, 3 ),
|
GridPoint:new( 5, 3 ),
|
||||||
Color.blue
|
Color.blue
|
||||||
)
|
)
|
||||||
gameGrid:push( line2 )
|
GameGrid:push( line2 )
|
||||||
|
|
||||||
mouse = {
|
Mouse = {
|
||||||
x = 0,
|
x = 0,
|
||||||
y = 0,
|
y = 0,
|
||||||
pressed = false,
|
pressed = false,
|
||||||
|
@ -43,75 +54,65 @@ end
|
||||||
-- TODO: move input interactions into module
|
-- TODO: move input interactions into module
|
||||||
-- TODO: fix collision with other line's endpoint
|
-- TODO: fix collision with other line's endpoint
|
||||||
function love.update( dt )
|
function love.update( dt )
|
||||||
mouse.x, mouse.y = love.mouse.getPosition()
|
Mouse.x, Mouse.y = love.mouse.getPosition()
|
||||||
mouse.x = mouse.x + 1
|
Mouse.x = Mouse.x + 1
|
||||||
mouse.y = mouse.y + 1
|
Mouse.y = Mouse.y + 1
|
||||||
mouse.lastPoint = mouse.point
|
Mouse.lastPoint = Mouse.point
|
||||||
mouse.lastPressed = 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 = 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 vectorLength( Mouse.point, 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 )
|
||||||
and not gameGrid:isOtherEndpoint( mouse.lastLine, mouse.point ) then
|
and not GameGrid:isOtherEndpoint( Mouse.lastLine, Mouse.point ) then
|
||||||
mouse.lastLine:push( mouse.point )
|
Mouse.lastLine:push( Mouse.point )
|
||||||
elseif mouse.lastLine:has( mouse.point )
|
elseif Mouse.lastLine:has( Mouse.point )
|
||||||
and not mouse.point:equals( lastLinePoint ) then
|
and not Mouse.point:equals( lastLinePoint ) then
|
||||||
while not mouse.lastLine.points[#mouse.lastLine.points]:equals( mouse.point ) do
|
while not Mouse.lastLine.points[#Mouse.lastLine.points]:equals( Mouse.point ) do
|
||||||
mouse.lastLine:pop()
|
Mouse.lastLine:pop()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
mouse.dragged = mouse.pressed
|
Mouse.dragged = Mouse.pressed
|
||||||
and mouse.startX > 0
|
and Mouse.startX > 0
|
||||||
and vectorLength(
|
and vectorLength(
|
||||||
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
|
||||||
|
|
||||||
if not mouse.pressed and mouse.startX > 0 then
|
if not Mouse.pressed and Mouse.startX > 0 then
|
||||||
mouse.startX = -1
|
Mouse.startX = -1
|
||||||
mouse.startY = -1
|
Mouse.startY = -1
|
||||||
mouse.lastLine = nil
|
Mouse.lastLine = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
if not mouse.lastPressed
|
if not Mouse.lastPressed
|
||||||
and mouse.pressed
|
and Mouse.pressed
|
||||||
and mouse.startX < 0 then
|
and Mouse.startX < 0 then
|
||||||
mouse.startX = mouse.x
|
Mouse.startX = Mouse.x
|
||||||
mouse.startY = mouse.y
|
Mouse.startY = Mouse.y
|
||||||
mouse.lastLine = gameGrid:matchesLine( mouse.point, false )
|
Mouse.lastLine = GameGrid:matchesLine( Mouse.point, false )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
gameGrid:draw()
|
GameGrid:draw()
|
||||||
|
|
||||||
local text = string.format( "%d:%d global\n%d:%d local\n%d:%d from start", mouse.x, mouse.y, mouse.point.x, mouse.point.y, mouse.startX - mouse.x, mouse.startY - mouse.y )
|
local text = string.format( "%d:%d global\n%d:%d local\n%d:%d from start", Mouse.x, Mouse.y, Mouse.point.x, Mouse.point.y, Mouse.startX - Mouse.x, Mouse.startY - Mouse.y )
|
||||||
love.graphics.print( text, 64, 256 )
|
love.graphics.print( text, 64, 256 )
|
||||||
|
|
||||||
if mouse.dragged then
|
if Mouse.dragged then
|
||||||
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
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Returns local coords from global
|
|
||||||
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
|
|
||||||
|
|
||||||
function vectorLength( startpoint, endpoint )
|
|
||||||
return math.sqrt( ( startpoint.x - endpoint.x ) ^ 2 + ( startpoint.y - endpoint.y ) ^ 2 )
|
|
||||||
end
|
|
||||||
|
|
17
point.lua
17
point.lua
|
@ -1,11 +1,15 @@
|
||||||
-- Point table
|
-- Represents 2D point
|
||||||
Point = {}
|
---@class Point
|
||||||
|
Point = { x = 1, y = 1 }
|
||||||
|
|
||||||
-- Factory function
|
-- Point factory
|
||||||
|
---@param x number
|
||||||
|
---@param y number
|
||||||
|
---@return any
|
||||||
function Point:new( x, y )
|
function Point:new( x, y )
|
||||||
local point = {
|
local point = {
|
||||||
x = x or 1,
|
x = x or x,
|
||||||
y = y or 1
|
y = y or y
|
||||||
}
|
}
|
||||||
|
|
||||||
setmetatable( point, { __index = self } )
|
setmetatable( point, { __index = self } )
|
||||||
|
@ -14,11 +18,14 @@ function Point:new( x, y )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Comparing function
|
-- Comparing function
|
||||||
|
---@param point self
|
||||||
|
---@return boolean
|
||||||
function Point:equals( point )
|
function Point:equals( point )
|
||||||
return self.x == point.x and self.y == point.y
|
return self.x == point.x and self.y == point.y
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Get self coordinates
|
-- Get self coordinates
|
||||||
|
---@return number, number
|
||||||
function Point:coords()
|
function Point:coords()
|
||||||
return self.x, self.y
|
return self.x, self.y
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue