Compare commits
No commits in common. "0eb2138a75f6ee39186ef22303af2471cd5107a7" and "d7dcda1adaf004a25a0037433f1737d27c376b41" have entirely different histories.
0eb2138a75
...
d7dcda1ada
4 changed files with 32 additions and 99 deletions
18
button.lua
18
button.lua
|
|
@ -1,5 +1,4 @@
|
|||
require 'point'
|
||||
require 'text'
|
||||
|
||||
-- Clickable UI button
|
||||
---@class Button
|
||||
|
|
@ -7,35 +6,29 @@ require 'text'
|
|||
---@field size Point
|
||||
---@field prevPressed boolean
|
||||
---@field lastPressed boolean
|
||||
---@field text Text
|
||||
Button = {
|
||||
position = Point:new( 1, 1 ),
|
||||
size = Point:new( 1, 1 ),
|
||||
prevPressed = false,
|
||||
lastPressed = false,
|
||||
text = Text:new()
|
||||
lastPressed = false
|
||||
}
|
||||
|
||||
-- Factory function
|
||||
---@param position Point | nil
|
||||
---@param size Point | nil
|
||||
---@param text string | nil
|
||||
---@param pressed function | nil
|
||||
---@param held function | nil
|
||||
---@param released function | nil
|
||||
---@return Button
|
||||
function Button:new( position, size, text, pressed, held, released )
|
||||
local pos = position or Button.position
|
||||
local siz = size or Button.size
|
||||
function Button:new( position, size, pressed, held, released )
|
||||
local button = {
|
||||
position = pos,
|
||||
size = siz,
|
||||
position = position or Button.position,
|
||||
size = size or Button.size,
|
||||
prevPressed = false,
|
||||
lastPressed = false,
|
||||
pressed = pressed or Button.pressed,
|
||||
held = held or Button.held,
|
||||
released = released or Button.released,
|
||||
text = Text:new( pos, siz, text)
|
||||
released = released or Button.released
|
||||
}
|
||||
|
||||
setmetatable( button, { __index = self } )
|
||||
|
|
@ -56,7 +49,6 @@ function Button:draw()
|
|||
local x, y = self.position:coords()
|
||||
local w, h = self.size:coords()
|
||||
love.graphics.rectangle( 'line', x, y, w, h )
|
||||
self.text:draw()
|
||||
end
|
||||
|
||||
-- Check if given point is in bounds of button
|
||||
|
|
|
|||
21
main.lua
21
main.lua
|
|
@ -27,8 +27,11 @@ function love.update( dt )
|
|||
if InMenu then
|
||||
Menu:update( dt, Point:new( Mouse.x, Mouse.y ), Mouse.pressed )
|
||||
else
|
||||
if GameGrid:isCompleted() and not Mouse.pressed then
|
||||
Menu.current_state = MenuStateIndex.completed
|
||||
if Input:actionReleased( 'nextlevel' ) then
|
||||
if GameGrid:isCompleted() then
|
||||
GameGrid = LevelHandler:next()
|
||||
Menu.updateCellSize()
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Switch menu state or exit
|
||||
|
|
@ -52,5 +55,19 @@ function love.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 )
|
||||
love.graphics.print( text, 64, 256 )
|
||||
|
||||
if Mouse.dragged then
|
||||
love.graphics.print( "drag", 64, 300 )
|
||||
end
|
||||
if Mouse.lastLine ~= nil then
|
||||
love.graphics.print( tostring( Mouse.lastLine ), 128, 300 )
|
||||
end
|
||||
|
||||
if GameGrid:isCompleted() then
|
||||
love.graphics.print( "Grid completed. Press space to proceed to next", 64, 320 )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
52
menu.lua
52
menu.lua
|
|
@ -43,18 +43,12 @@ MenuStateIndex = {
|
|||
start = 'start',
|
||||
pause = 'pause',
|
||||
levels = 'levels',
|
||||
hidden = 'hidden',
|
||||
completed = 'completed'
|
||||
hidden = 'hidden'
|
||||
}
|
||||
|
||||
local buttonTopPos = Point:new( 240, 120 )
|
||||
local buttonBotPos = Point:new( 240, 240 )
|
||||
local buttonSize = Point:new( 320, 80 )
|
||||
|
||||
local startButton = Button:new(
|
||||
buttonTopPos,
|
||||
buttonSize,
|
||||
'Start',
|
||||
Point:new( 240, 120 ),
|
||||
Point:new( 320, 80 ),
|
||||
nil,
|
||||
nil,
|
||||
function()
|
||||
|
|
@ -65,9 +59,8 @@ local startButton = Button:new(
|
|||
)
|
||||
|
||||
local exitToMenuButton = Button:new(
|
||||
buttonBotPos,
|
||||
buttonSize,
|
||||
'Return to menu',
|
||||
Point:new( 240, 200 ),
|
||||
Point:new( 320, 80 ),
|
||||
nil,
|
||||
nil,
|
||||
function()
|
||||
|
|
@ -76,9 +69,8 @@ local exitToMenuButton = Button:new(
|
|||
)
|
||||
|
||||
local exitGameButton = Button:new(
|
||||
buttonBotPos,
|
||||
buttonSize,
|
||||
'Exit',
|
||||
Point:new( 240, 240 ),
|
||||
Point:new( 320, 80 ),
|
||||
nil,
|
||||
nil,
|
||||
function()
|
||||
|
|
@ -86,30 +78,6 @@ local exitGameButton = Button:new(
|
|||
end
|
||||
)
|
||||
|
||||
local nextLevelButton = Button:new(
|
||||
buttonTopPos,
|
||||
buttonSize,
|
||||
'Next level',
|
||||
nil,
|
||||
nil,
|
||||
function()
|
||||
GameGrid = LevelHandler:next()
|
||||
Menu.updateCellSize()
|
||||
Menu.current_state = MenuStateIndex.hidden
|
||||
end
|
||||
)
|
||||
|
||||
local backToGameButton = Button:new(
|
||||
buttonTopPos,
|
||||
buttonSize,
|
||||
'Continue',
|
||||
nil,
|
||||
nil,
|
||||
function()
|
||||
Menu.current_state = MenuStateIndex.hidden
|
||||
end
|
||||
)
|
||||
|
||||
-- Menu handler
|
||||
---@class Menu
|
||||
---@field states MenuState[]
|
||||
|
|
@ -122,15 +90,11 @@ Menu = {
|
|||
}),
|
||||
-- Pause menu
|
||||
[MenuStateIndex.pause] = MenuState:new({
|
||||
backToGameButton, exitToMenuButton
|
||||
exitToMenuButton
|
||||
}),
|
||||
-- Level selector
|
||||
[MenuStateIndex.levels] = MenuState:new({
|
||||
}),
|
||||
-- Level completed menu
|
||||
[MenuStateIndex.completed] = MenuState:new({
|
||||
nextLevelButton, exitToMenuButton
|
||||
}),
|
||||
-- Empty state
|
||||
[MenuStateIndex.hidden] = MenuState:new()
|
||||
},
|
||||
|
|
|
|||
40
text.lua
40
text.lua
|
|
@ -1,40 +0,0 @@
|
|||
require 'point'
|
||||
|
||||
-- default font size
|
||||
local fontsize = 12
|
||||
|
||||
-- UI Text
|
||||
---@class Text
|
||||
---@field position Point
|
||||
---@field size Point
|
||||
---@field text string
|
||||
---@field align love.AlignMode
|
||||
Text = {
|
||||
position = Point:new( 1, 1 ),
|
||||
size = Point:new( 1, 1 ),
|
||||
text = "Sample text",
|
||||
align = "center"
|
||||
}
|
||||
|
||||
function Text:new( position, size, text, align )
|
||||
local txt = {
|
||||
position = position or Text.position,
|
||||
size = size or Text.size,
|
||||
text = text or Text.text,
|
||||
align = align or Text.align
|
||||
}
|
||||
|
||||
setmetatable( txt, { __index = self } )
|
||||
|
||||
return txt
|
||||
end
|
||||
|
||||
function Text:draw()
|
||||
local x, y = self.position:coords()
|
||||
local w, h = self.size:coords()
|
||||
local oy = 0
|
||||
if self.align == "center" then
|
||||
oy = -h / 2 + fontsize / 2
|
||||
end
|
||||
love.graphics.printf(self.text, x, y, w, self.align, 0, 1, 1, 0, oy )
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue