Compare commits
No commits in common. "d7dcda1adaf004a25a0037433f1737d27c376b41" and "319a64268802ef0ebc04c779271c077dccc124d6" have entirely different histories.
d7dcda1ada
...
319a642688
5 changed files with 37 additions and 157 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" ]
|
||||||
|
}
|
||||||
17
button.lua
17
button.lua
|
|
@ -57,7 +57,7 @@ end
|
||||||
function Button:checkPoint( point )
|
function Button:checkPoint( point )
|
||||||
local px, py = point:coords()
|
local px, py = point:coords()
|
||||||
local sx, sy = self.position:coords()
|
local sx, sy = self.position:coords()
|
||||||
local ex, ey = self.size:coords()
|
local ex, ey = self.position:coords()
|
||||||
ex = ex + sx
|
ex = ex + sx
|
||||||
ey = ey + sy
|
ey = ey + sy
|
||||||
return px >= sx and px <= ex and py >= sy and py <= ey
|
return px >= sx and px <= ex and py >= sy and py <= ey
|
||||||
|
|
@ -69,14 +69,15 @@ end
|
||||||
function Button:update( dt, point, pressed )
|
function Button:update( dt, point, pressed )
|
||||||
self.prevPressed = self.lastPressed
|
self.prevPressed = self.lastPressed
|
||||||
self.lastPressed = pressed
|
self.lastPressed = pressed
|
||||||
local inBounds = self:checkPoint( point )
|
|
||||||
if inBounds then
|
if self.lastPressed and not self.prevPressed then
|
||||||
if self.lastPressed and not self.prevPressed then
|
local inBounds = self:checkPoint( point )
|
||||||
|
if inBounds then
|
||||||
self:pressed()
|
self:pressed()
|
||||||
elseif self.lastPressed and self.prevPressed then
|
|
||||||
self:held( dt )
|
|
||||||
elseif not self.lastPressed and self.prevPressed then
|
|
||||||
self:released()
|
|
||||||
end
|
end
|
||||||
|
elseif self.lastPressed and self.prevPressed then
|
||||||
|
self:held( dt )
|
||||||
|
elseif not self.lastPressed and self.prevPressed then
|
||||||
|
self:released()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ require 'tablefuncs'
|
||||||
---@field cellSize number Size of each grid cell
|
---@field cellSize number Size of each grid cell
|
||||||
---@field lineStyle string love2d line style setting
|
---@field lineStyle string love2d line style setting
|
||||||
---@field lineWidth number love2d line width setting
|
---@field lineWidth number love2d line width setting
|
||||||
---@field menuLineWidth number love2d line width for buttons
|
|
||||||
---@field dragSensivity number drag sensivity, px
|
---@field dragSensivity number drag sensivity, px
|
||||||
Config = {
|
Config = {
|
||||||
pointRadius = 0.3,
|
pointRadius = 0.3,
|
||||||
|
|
@ -18,7 +17,6 @@ Config = {
|
||||||
|
|
||||||
lineStyle = "smooth",
|
lineStyle = "smooth",
|
||||||
lineWidth = 0.1,
|
lineWidth = 0.1,
|
||||||
menuLineWidth = 4,
|
|
||||||
|
|
||||||
dragSensivity = 5
|
dragSensivity = 5
|
||||||
}
|
}
|
||||||
|
|
|
||||||
48
main.lua
48
main.lua
|
|
@ -4,56 +4,56 @@ require 'line'
|
||||||
require 'mouse'
|
require 'mouse'
|
||||||
require 'levelhandler'
|
require 'levelhandler'
|
||||||
require 'button'
|
require 'button'
|
||||||
require 'menu'
|
|
||||||
|
|
||||||
Input = require 'input'
|
Input = require 'input'
|
||||||
|
|
||||||
|
InMenu = true
|
||||||
|
|
||||||
|
local function updateCellSize()
|
||||||
|
local width, height = love.graphics.getDimensions()
|
||||||
|
local gridX, gridY = GameGrid.size:coords();
|
||||||
|
local isWidthBased = (width / height) / (gridX / gridY)
|
||||||
|
Config.cellSize = math.floor(isWidthBased and height / gridY or width / gridX)
|
||||||
|
love.graphics.setLineWidth( Config.cellSize * Config.lineWidth )
|
||||||
|
end
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
love.window.setMode( 800, 480 )
|
love.window.setMode( 800, 480 )
|
||||||
|
|
||||||
InMenu = true
|
local menuButtonReleased = function()
|
||||||
|
InMenu = false
|
||||||
|
GameGrid = LevelHandler:first()
|
||||||
|
updateCellSize()
|
||||||
|
end
|
||||||
|
|
||||||
|
MenuStartButton = Button:new( Point:new( 240, 120 ), Point:new( 320, 80 ), nil, nil, menuButtonReleased )
|
||||||
|
|
||||||
|
|
||||||
love.graphics.setLineStyle( Config.lineStyle )
|
love.graphics.setLineStyle( Config.lineStyle )
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update( dt )
|
function love.update( dt )
|
||||||
InMenu = Menu.current_state ~= MenuStateIndex.hidden
|
|
||||||
Mouse:update()
|
Mouse:update()
|
||||||
Input:update()
|
Input:update()
|
||||||
|
|
||||||
local exit = Input:actionReleased( 'exit' )
|
|
||||||
|
|
||||||
if InMenu then
|
if InMenu then
|
||||||
Menu:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed )
|
MenuStartButton:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed )
|
||||||
else
|
else
|
||||||
|
if Input:actionReleased( 'exit' ) then
|
||||||
|
love.event.quit()
|
||||||
|
end
|
||||||
|
|
||||||
if Input:actionReleased( 'nextlevel' ) then
|
if Input:actionReleased( 'nextlevel' ) then
|
||||||
if GameGrid:isCompleted() then
|
if GameGrid:isCompleted() then
|
||||||
GameGrid = LevelHandler:next()
|
GameGrid = LevelHandler:next()
|
||||||
Menu.updateCellSize()
|
updateCellSize()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Switch menu state or exit
|
|
||||||
if exit then
|
|
||||||
if not InMenu then
|
|
||||||
Menu.current_state = MenuStateIndex.pause
|
|
||||||
elseif Menu.current_state == MenuStateIndex.start then
|
|
||||||
love.event.quit()
|
|
||||||
elseif Menu.current_state == MenuStateIndex.pause then
|
|
||||||
Menu.current_state = MenuStateIndex.hidden
|
|
||||||
elseif Menu.current_state == MenuStateIndex.levels then
|
|
||||||
Menu.current_state = MenuStateIndex.start
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
if InMenu then
|
if InMenu then
|
||||||
love.graphics.setLineWidth( Config.menuLineWidth )
|
MenuStartButton:draw()
|
||||||
Menu:draw()
|
|
||||||
else
|
else
|
||||||
love.graphics.setLineWidth( Config.cellSize * Config.lineWidth )
|
|
||||||
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 )
|
||||||
|
|
|
||||||
123
menu.lua
123
menu.lua
|
|
@ -1,123 +0,0 @@
|
||||||
require 'config'
|
|
||||||
require 'button'
|
|
||||||
|
|
||||||
-- Menu state class
|
|
||||||
---@class MenuState
|
|
||||||
---@field buttons Button[]
|
|
||||||
MenuState = {
|
|
||||||
buttons = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
-- Factory function
|
|
||||||
---@param buttons Button[] | nil
|
|
||||||
---@return MenuState
|
|
||||||
function MenuState:new( buttons )
|
|
||||||
local state = {
|
|
||||||
buttons = buttons or MenuState.buttons
|
|
||||||
}
|
|
||||||
|
|
||||||
setmetatable( state, { __index = self } )
|
|
||||||
|
|
||||||
return state
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Draw buttons
|
|
||||||
function MenuState:draw()
|
|
||||||
for _, button in ipairs(self.buttons) do
|
|
||||||
button:draw()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Update buttons
|
|
||||||
---@param dt number
|
|
||||||
---@param point Point
|
|
||||||
---@param pressed boolean
|
|
||||||
function MenuState:update( dt, point, pressed )
|
|
||||||
for _, button in pairs(self.buttons) do
|
|
||||||
button:update( dt, point, pressed )
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@enum MenuStateIndex
|
|
||||||
MenuStateIndex = {
|
|
||||||
start = 'start',
|
|
||||||
pause = 'pause',
|
|
||||||
levels = 'levels',
|
|
||||||
hidden = 'hidden'
|
|
||||||
}
|
|
||||||
|
|
||||||
local startButton = Button:new(
|
|
||||||
Point:new( 240, 120 ),
|
|
||||||
Point:new( 320, 80 ),
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
function()
|
|
||||||
Menu.current_state = MenuStateIndex.hidden
|
|
||||||
GameGrid = LevelHandler:first()
|
|
||||||
Menu.updateCellSize()
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
local exitToMenuButton = Button:new(
|
|
||||||
Point:new( 240, 200 ),
|
|
||||||
Point:new( 320, 80 ),
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
function()
|
|
||||||
Menu.current_state = MenuStateIndex.start
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
local exitGameButton = Button:new(
|
|
||||||
Point:new( 240, 240 ),
|
|
||||||
Point:new( 320, 80 ),
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
function()
|
|
||||||
love.event.quit()
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Menu handler
|
|
||||||
---@class Menu
|
|
||||||
---@field states MenuState[]
|
|
||||||
---@field current_state MenuStateIndex
|
|
||||||
Menu = {
|
|
||||||
states = {
|
|
||||||
-- Main menu
|
|
||||||
[MenuStateIndex.start] = MenuState:new({
|
|
||||||
startButton, exitGameButton
|
|
||||||
}),
|
|
||||||
-- Pause menu
|
|
||||||
[MenuStateIndex.pause] = MenuState:new({
|
|
||||||
exitToMenuButton
|
|
||||||
}),
|
|
||||||
-- Level selector
|
|
||||||
[MenuStateIndex.levels] = MenuState:new({
|
|
||||||
}),
|
|
||||||
-- Empty state
|
|
||||||
[MenuStateIndex.hidden] = MenuState:new()
|
|
||||||
},
|
|
||||||
current_state = MenuStateIndex.start
|
|
||||||
}
|
|
||||||
|
|
||||||
-- Draw current state
|
|
||||||
function Menu:draw()
|
|
||||||
self.states[self.current_state]:draw()
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Update current state
|
|
||||||
---@param dt number
|
|
||||||
---@param point Point
|
|
||||||
---@param pressed boolean
|
|
||||||
function Menu:update( dt, point, pressed )
|
|
||||||
self.states[self.current_state]:update( dt, point, pressed )
|
|
||||||
end
|
|
||||||
|
|
||||||
function Menu.updateCellSize()
|
|
||||||
local width, height = love.graphics.getDimensions()
|
|
||||||
local gridX, gridY = GameGrid.size:coords();
|
|
||||||
local isWidthBased = (width / height) / (gridX / gridY)
|
|
||||||
Config.cellSize = math.floor(isWidthBased and height / gridY or width / gridX)
|
|
||||||
love.graphics.setLineWidth( Config.cellSize * Config.lineWidth )
|
|
||||||
end
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue