31 lines
792 B
Lua
31 lines
792 B
Lua
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,
|
|
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,
|
|
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
|