sonic-bluestreak/sonic-bluestreak.love/scenes/overworld/screens/mainmenu/items.lua

259 lines
8.2 KiB
Lua
Raw Permalink Normal View History

2021-11-25 10:57:13 +01:00
local ParentScreen = require "scenes.overworld.screens.parent"
local ItemsScreen = ParentScreen:extend()
local menu = require "game.modules.menus.list"
local const = require "scenes.overworld.screens.mainmenu.const"
local gui = require "game.modules.gui"
local ItemWidget = menu.DualTextWidget:extend()
local BackWidget = menu.DualTextWidget:extend()
local ResumeWidget = menu.DualTextWidget:extend()
local DropWidget = menu.DualTextWidget:extend()
local UseWidget = menu.DualTextWidget:extend()
local DESC_SIZE = 48*4
local ConfirmDialog = require "game.modules.confirmdialog"
function ItemsScreen:new(scene, menuIndex, widgetIndex)
self.menuIndex = menuIndex or 1
self.widgetIndex = widgetIndex or 1
self.choiceBack = gui.newChoiceBack(DESC_SIZE)
self.descBox = gui.newTextBox("assets/gui/dialogbox.png", DESC_SIZE, 40+16)
self.effectBox = gui.newTextBox("assets/gui/dialogbox.png", DESC_SIZE, 40)
self.desc = ""
self.effects = ""
ItemsScreen.super.new(self, scene, "Items")
end
function ItemsScreen:update(dt)
if (self.scene.menusystem.menus["useMenu"] == nil) then
local keys = self.scene:getKeys(1)
if (keys["left"].isPressed) then
self.widgetIndex = 1
self:getMenu(self.menuIndex - 1)
self.scene.assets.sfx["mBeep"]:play()
end
if (keys["right"].isPressed) then
self.widgetIndex = 1
self:getMenu(self.menuIndex + 1)
self.scene.assets.sfx["mBeep"]:play()
end
end
end
function ItemsScreen:getMenu(newMenuIndex)
local newMenuIndex = newMenuIndex or 1
if (newMenuIndex > #game.loot.inventory) then
newMenuIndex = 1
end
if (newMenuIndex < 1) then
newMenuIndex = #game.loot.inventory
end
self.menuIndex = newMenuIndex
self.scene.menusystem:reset()
self:setMenu()
end
function ItemsScreen:setMenu(widgetId)
self.pocket = game.loot:getPocketById(self.menuIndex)
local w = 128+32
menu.ListMenu(self.scene, "menu", const.X2 - w, const.Y + 8, w, 9, true)
for i,item in ipairs(self.pocket.list) do
--menu.DualTextWidget(self.scene, "menu", item.name, "x" .. item.number)
ItemWidget(self.scene, self.pocket.name, item, i)
end
BackWidget(self.scene)
self.scene.menusystem.menus["menu"]:setCancelWidget()
self.scene.menusystem:switchMenu("menu")
self.scene.menusystem.menus["menu"].isLocked = true
self.scene.menusystem.menus["menu"].widget.selected = math.max(1, self.widgetIndex)
self.scene.menusystem.menus["menu"].cursorTransition = math.max(0, self.widgetIndex - 1)
self.scene.menusystem:setSoundFromSceneAssets("mBeep")
end
function ItemsScreen:setUseMenu(item, widgetId)
local w = 96
menu.ListMenu(self.scene, "useMenu", const.X2 + 16 - w, const.Y2 - 48, w, 3, true)
UseWidget(self.scene, self.pocket.name, item, widgetId)
DropWidget(self.scene, self.pocket.name, item, widgetId)
ResumeWidget(self.scene)
self.scene.menusystem.menus["useMenu"]:setCancelWidget()
self.scene.menusystem:switchMenu("useMenu")
self.scene.menusystem.menus["useMenu"]:setDepth(-1)
end
function ItemsScreen:removeUseMenu()
self.scene.menusystem.menus["useMenu"]:destroy()
self.scene.menusystem:switchMenu("menu")
end
function ItemsScreen:draw()
self:drawPocket()
self:drawDescription(const.X, const.Y2 - (88+16))
self:drawEffects(const.X, const.Y2 - 40)
end
function ItemsScreen:drawDescription(x, y)
love.graphics.draw(self.descBox, x, y)
local xx, yy, ww = x + 6, y + 4, DESC_SIZE - 12
self.scene.assets.fonts["small"]:draw(self.desc, xx, yy, ww, "left")
end
function ItemsScreen:drawEffects(x, y)
love.graphics.draw(self.effectBox, x, y)
local xx, yy, ww = x + 6, y + 4, DESC_SIZE - 12
self.scene.assets.fonts["small"]:draw(self.effects, xx, yy, ww, "left")
end
function ItemsScreen:drawPocket()
local middleX = ((16) + (DESC_SIZE - 24)) / 2
love.graphics.draw(self.choiceBack, const.X, const.Y)
self.scene.assets.fonts["small"]:draw("<", const.X + 16, const.Y - 2, -1, "left")
self.scene.assets.fonts["small"]:draw(">", const.X + DESC_SIZE - 24, const.Y - 2, -1, "right")
self.scene.assets.fonts["small"]:draw(self.pocket.fullname, const.X + middleX, const.Y - 2, -1, "center")
self:drawPocketRoll(const.X + 48*2, const.Y + 20)
end
function ItemsScreen:drawPocketRoll(x, y)
core.screen:setScissor(const.X, const.Y+ 16, 48*4, 48)
local trueX = x - ((self.menuIndex - 1) * 32)
for i, pocket in ipairs(game.loot.inventory) do
local trueIndex = i - self.menuIndex
if (trueIndex > 4) then
trueIndex = trueIndex - 8
end
if (trueIndex < -4) then
trueIndex = trueIndex + 8
end
if (trueIndex ~= 0) then
love.graphics.setColor(.3, .3, .3, .6)
end
self.scene.assets.tileset["itembox"]:drawTile(i, x + ((trueIndex)*32), y, 0, 1, 1, 14, 0)
utils.graphics.resetColor()
end
core.screen:resetScissor()
end
-- Items Widgets
function ItemWidget:new(scene, pocket, item, widgetId)
self.item = item
self.itemData = core.datas:get("items", self.item.name)
self.pocket = pocket
self.widgetId = widgetId
ItemWidget.super.new(self, scene, "menu", self.itemData.fullname, "x" .. utils.math.numberToString(self.item.number, 2))
end
function ItemWidget:selectAction()
self.scene.currentScreen.desc = self.itemData.description
self.scene.currentScreen.effects = game.loot:getEffectStrings(self.pocket, self.item.name)
end
function ItemWidget:action()
self.scene.assets:playSFX("mSelect")
self.scene.currentScreen:setUseMenu(self.item, self.widgetId)
end
function BackWidget:new(scene)
ItemWidget.super.new(self, scene, "menu", "Back", "")
end
function BackWidget:selectAction()
self.scene.currentScreen.desc = ""
self.scene.currentScreen.effects = ""
end
function BackWidget:action()
self.scene.assets:playSFX("mBack")
self.scene.screens.mainmenu["pause"](self.scene, "main", 3)
end
-- Use Menu Widget
function UseWidget:new(scene, pocket, item, widgetId)
self.item = item
self.itemData = core.datas:get("items", self.item.name)
self.pocket = pocket
self.pocketData = game.loot:getPocketByName(self.pocket)
self.widgetId = widgetId
UseWidget.super.new(self, scene, "useMenu", "Use", "")
if (not (self.itemData.usableOnMap or self.pocketData.isEquipement)) then
self.color = {0.6, 0.6, 0.6}
end
end
function UseWidget:action()
if (self.pocketData.isEquipement) then
self.scene.screens.mainmenu["equip"](self.scene, self.pocket, self.item, self.widgetId)
self.scene.assets:playSFX("mSelect")
elseif (self.itemData.usableOnMap) then
self.scene.screens.mainmenu["useItems"](self.scene, self.pocket, self.item, self.widgetId)
self.scene.assets:playSFX("mSelect")
else
self.scene.assets:playSFX("mError")
end
end
function DropWidget:new(scene, pocket, item, widgetId)
self.item = item
self.itemData = core.datas:get("items", self.item.name)
self.pocket = pocket
self.max = self.item.number
self.widgetId = widgetId
self.number = 1
DropWidget.super.new(self, scene, "useMenu", "Drop", "<" .. utils.math.numberToString(self.number, 2) .. ">")
end
function DropWidget:updateSelected()
local keys = self.scene:getKeys(1)
if (keys["left"].isPressed) then
if (self.number > 1) then
self.number = self.number - 1
else
self.number = self.max
end
self.label2 = "<" .. utils.math.numberToString(self.number, 2) .. ">"
self:invalidateCanvas()
end
if (keys["right"].isPressed) then
if (self.number < self.max) then
self.number = self.number + 1
else
self.number = 1
end
self.label2 = "<" .. utils.math.numberToString(self.number, 2) .. ">"
self:invalidateCanvas()
end
end
function DropWidget:action()
self.scene.assets:playSFX("mSelect")
local confirm = ConfirmDialog(self.scene, "Do you want to drop these items ? \nYou won't be able to recover them.",
function() self:drop() end)
confirm:setCancelChoice(2)
confirm.autoDismiss = true
end
function DropWidget:drop()
game.loot:removeItem(self.pocket, self.item.name, self.number)
self.scene.currentScreen.widgetIndex = self.widgetId
self.scene.currentScreen:removeUseMenu()
self.scene.currentScreen:setMenu()
end
function ResumeWidget:new(scene)
ResumeWidget.super.new(self, scene, "useMenu", "Back", "")
end
function ResumeWidget:action()
self.scene.assets:playSFX("mBack")
self.scene.currentScreen:removeUseMenu()
end
return ItemsScreen