added menu handler

This commit is contained in:
Alexey 2025-10-22 13:45:09 +03:00
commit d7dcda1ada
3 changed files with 150 additions and 25 deletions

View file

@ -4,56 +4,56 @@ require 'line'
require 'mouse'
require 'levelhandler'
require 'button'
require 'menu'
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()
love.window.setMode( 800, 480 )
local menuButtonReleased = function()
InMenu = false
GameGrid = LevelHandler:first()
updateCellSize()
end
MenuStartButton = Button:new( Point:new( 240, 120 ), Point:new( 320, 80 ), nil, nil, menuButtonReleased )
InMenu = true
love.graphics.setLineStyle( Config.lineStyle )
end
function love.update( dt )
InMenu = Menu.current_state ~= MenuStateIndex.hidden
Mouse:update()
Input:update()
if InMenu then
MenuStartButton:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed )
else
if Input:actionReleased( 'exit' ) then
love.event.quit()
end
local exit = Input:actionReleased( 'exit' )
if InMenu then
Menu:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed )
else
if Input:actionReleased( 'nextlevel' ) then
if GameGrid:isCompleted() then
GameGrid = LevelHandler:next()
updateCellSize()
Menu.updateCellSize()
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
function love.draw()
if InMenu then
MenuStartButton:draw()
love.graphics.setLineWidth( Config.menuLineWidth )
Menu:draw()
else
love.graphics.setLineWidth( Config.cellSize * Config.lineWidth )
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 )