Almost working from 1st try

This commit is contained in:
Alexey 2025-10-29 18:12:43 +03:00
commit c6547c1121
8 changed files with 297 additions and 0 deletions

31
gridpoint.lua Normal file
View file

@ -0,0 +1,31 @@
require 'point'
require 'config'
-- Point table with grid-based coordinates
---@class GridPoint: Point
GridPoint = Point
-- Convert grid x, y to global x, y
---@return Point
function GridPoint:absolute()
return Point:new(
self.x * Config.cellSize + Config.gridOffset,
self.y * Config.cellSize
)
end
-- Same as coords, but converted to global coords
---@return number, number
function GridPoint:globalCoords()
return self.x * Config.cellSize - Config.cellSize / 2 + Config.gridOffset,
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.gridOffset) / Config.cellSize )
local y = math.ceil( point.y / Config.cellSize )
return GridPoint:new( x, y )
end