Initial commit
This commit is contained in:
commit
f765ec74ef
6 changed files with 181 additions and 0 deletions
21
config.lua
Normal file
21
config.lua
Normal file
|
@ -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 }
|
||||
}
|
36
grid.lua
Normal file
36
grid.lua
Normal file
|
@ -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
|
18
gridpoint.lua
Normal file
18
gridpoint.lua
Normal file
|
@ -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
|
50
line.lua
Normal file
50
line.lua
Normal file
|
@ -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
|
32
main.lua
Normal file
32
main.lua
Normal file
|
@ -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
|
24
point.lua
Normal file
24
point.lua
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue