From f765ec74efdfe06e14464069be03c34df93567fa Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Tue, 20 May 2025 02:51:17 +0300 Subject: [PATCH] Initial commit --- config.lua | 21 +++++++++++++++++++++ grid.lua | 36 ++++++++++++++++++++++++++++++++++++ gridpoint.lua | 18 ++++++++++++++++++ line.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 32 ++++++++++++++++++++++++++++++++ point.lua | 24 ++++++++++++++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 config.lua create mode 100644 grid.lua create mode 100644 gridpoint.lua create mode 100644 line.lua create mode 100644 main.lua create mode 100644 point.lua diff --git a/config.lua b/config.lua new file mode 100644 index 0000000..b1f8bd4 --- /dev/null +++ b/config.lua @@ -0,0 +1,21 @@ +-- Global config table +Config = { + -- Radius of each grid point + pointRadius = 7, + -- Radius of line start/end point + linePointRadius = 10, + -- Size of each grid cell + cellSize = 30, + + -- love2d line drawing settings + lineStyle = "smooth", + lineWidth = 5 +} + +-- Colors table +Color = { + white = { 1, 1, 1 }, + red = { 1, 0, 0 }, + green = { 0, 1, 0 }, + blue = { 0, 0, 1 } +} diff --git a/grid.lua b/grid.lua new file mode 100644 index 0000000..2f4f9a6 --- /dev/null +++ b/grid.lua @@ -0,0 +1,36 @@ +require "config" + +-- Game grid table, acts as "global level" of some sort +Grid = {} + +-- Factory function +function Grid:new( size ) + local grid = { + lines = {}, + size = size + } + + setmetatable( grid, { __index = self } ) + + return grid +end + +-- Add new line to grid +function Grid:push( line ) + table.insert( self.lines, line ) +end + +-- Draw lines and the whole grid +function Grid:draw() + -- Draw lines + for _, line in ipairs( self.lines ) do + line:draw() + end + -- Draw grid + for x = 1, self.size.x do + for y = 1, self.size.y do + local px, py = x * Config.cellSize, y * Config.cellSize + love.graphics.circle( "fill", px, py, Config.pointRadius ) + end + end +end diff --git a/gridpoint.lua b/gridpoint.lua new file mode 100644 index 0000000..6522f8c --- /dev/null +++ b/gridpoint.lua @@ -0,0 +1,18 @@ +require "point" +require "config" + +-- Point table with grid-based coordinates +GridPoint = Point:new() + +-- Convert grid x, y to global x, y +function GridPoint:absolute() + return Point:new( + self.x * Config.cellSize, + self.y * Config.cellSize + ) +end + +-- Same as coords, but converted to global coords +function GridPoint:globalCoords() + return self.x * Config.cellSize, self.y * Config.cellSize +end diff --git a/line.lua b/line.lua new file mode 100644 index 0000000..cce2ccb --- /dev/null +++ b/line.lua @@ -0,0 +1,50 @@ +require "gridpoint" + +-- Line table, interactive game object +-- NOTE: uses GridPoint +Line = {} + +-- Factory function +function Line:new( startpoint, endpoint, color ) + local line = { + startpoint = startpoint, + endpoint = endpoint, + points = { + startpoint + }, + color = color + } + + setmetatable( line, { __index = self } ) + + return line +end + +-- Push point to the end of points stack +function Line:push( point ) + table.insert( self.points, point ) +end + +-- Pop point from points stack +function Line:pop() + return table.remove( self.points ) +end + +-- Draw line with connected dots +function Line:draw() + love.graphics.setColor( self.color ) + -- Draw start and end points (should look like rings) + local sx, sy = self.startpoint:globalCoords() + love.graphics.circle( "fill", sx, sy, Config.linePointRadius ) + local ex, ey = self.endpoint:globalCoords() + love.graphics.circle( "fill", ex, ey, Config.linePointRadius ) + -- Draw line + local points = {} + for _, point in ipairs( self.points ) do + local x, y = point:globalCoords() + table.insert( points, x ) + table.insert( points, y ) + end + love.graphics.line( points ) + love.graphics.setColor( Color.white ) +end diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..ffc5182 --- /dev/null +++ b/main.lua @@ -0,0 +1,32 @@ +require "grid" +require "point" +require "line" + +function love.load() + love.graphics.setLineStyle( Config.lineStyle ) + love.graphics.setLineWidth( Config.lineWidth ) + + gameGrid = Grid:new( Point:new( 5, 5 ) ) + local line = Line:new( + GridPoint:new( 1, 1 ), + GridPoint:new( 3, 3 ), + Color.red + ) + line:push( GridPoint:new( 1, 2 ) ) + line:push( GridPoint:new( 1, 3 ) ) + line:push( GridPoint:new( 2, 3 ) ) + line:push( GridPoint:new( 2, 2 ) ) + line:push( GridPoint:new( 2, 1 ) ) + line:push( GridPoint:new( 3, 1 ) ) + line:push( GridPoint:new( 3, 2 ) ) + line:push( GridPoint:new( 3, 3 ) ) + gameGrid:push( line ) +end + +function love.update( dt ) + +end + +function love.draw() + gameGrid:draw() +end diff --git a/point.lua b/point.lua new file mode 100644 index 0000000..d474252 --- /dev/null +++ b/point.lua @@ -0,0 +1,24 @@ +-- Point table +Point = {} + +-- Factory function +function Point:new( x, y ) + local point = { + x = x or 1, + y = y or 1 + } + + setmetatable( point, { __index = self } ) + + return point +end + +-- Comparing function +function Point:equals( point ) + return self.x == point.x and self.y == point.y +end + +-- Get self coordinates +function Point:coords() + return self.x, self.y +end