Move MultiMenu to its own file

This commit is contained in:
mars 2023-04-19 16:33:18 -04:00
parent c450fc0efb
commit 4bbdb426ea
2 changed files with 58 additions and 65 deletions

View File

@ -1,5 +1,6 @@
local Atlas = require "atlas"
local Font = require "font"
local MultiMenu = require "multimenu"
local push = require "lib/push"
local gameWidth, gameHeight = 320, 200
@ -12,71 +13,6 @@ local pushOpts = {
push:setupScreen(gameWidth, gameHeight, windowWidth, windowHeight, pushOpts)
MultiMenu = {}
MultiMenu.__index = MultiMenu
setmetatable(
MultiMenu,
{
__call = function(self, options)
setmetatable({}, self)
self:initialize(options)
return self
end
}
)
function MultiMenu:initialize(options)
self.options = options
self.selected = 1
end
function MultiMenu:draw()
-- option drawing constants
local x = 400
local y_anchor = 200
local spacing = 50
y_anchor = y_anchor - spacing
-- draw all the options
for idx, option in ipairs(self.options) do
local y = y_anchor + idx * spacing
love.graphics.print(option, x, y)
end
-- cursor metric constants
local cursor_width = 10
local cursor_depth = 20
local cursor_spacing = 20
-- calculate the cursor's position
local cursor_x = x - cursor_spacing
local cursor_y = y_anchor + spacing * self.selected
-- draw the cursor
love.graphics.polygon("fill",
cursor_x, cursor_y,
cursor_x - cursor_depth, cursor_y - cursor_width,
cursor_x - cursor_depth, cursor_y + cursor_width
)
end
function MultiMenu:up()
if self.selected == 1 then
self.selected = #self.options
else
self.selected = self.selected - 1
end
end
function MultiMenu:down()
if self.selected == #self.options then
self.selected = 1
else
self.selected = self.selected + 1
end
end
function love.load()
Menu = MultiMenu({ "Fight", "Use", "Pkmn", "Flee" })

57
multimenu.lua Normal file
View File

@ -0,0 +1,57 @@
local Object = require "lib/classic"
---@class MultiMenu
local MultiMenu = Object:extend()
function MultiMenu:new(options)
self.options = options
self.selected = 1
end
function MultiMenu:draw()
-- option drawing constants
local x = 400
local y_anchor = 200
local spacing = 50
y_anchor = y_anchor - spacing
-- draw all the options
for idx, option in ipairs(self.options) do
local y = y_anchor + idx * spacing
love.graphics.print(option, x, y)
end
-- cursor metric constants
local cursor_width = 10
local cursor_depth = 20
local cursor_spacing = 20
-- calculate the cursor's position
local cursor_x = x - cursor_spacing
local cursor_y = y_anchor + spacing * self.selected
-- draw the cursor
love.graphics.polygon("fill",
cursor_x, cursor_y,
cursor_x - cursor_depth, cursor_y - cursor_width,
cursor_x - cursor_depth, cursor_y + cursor_width
)
end
function MultiMenu:up()
if self.selected == 1 then
self.selected = #self.options
else
self.selected = self.selected - 1
end
end
function MultiMenu:down()
if self.selected == #self.options then
self.selected = 1
else
self.selected = self.selected + 1
end
end
return MultiMenu