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

260 lines
8.2 KiB
Lua
Raw Normal View History

2020-08-29 11:36:59 +02: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()
2020-10-03 14:33:10 +02:00
local ResumeWidget = menu.DualTextWidget:extend()
local DropWidget = menu.DualTextWidget:extend()
local UseWidget = menu.DualTextWidget:extend()
2020-08-29 11:36:59 +02:00
local DESC_SIZE = 48*4
local ConfirmDialog = require "game.modules.confirmdialog"
2021-03-13 14:11:04 +01:00
function ItemsScreen:new(scene, menuIndex, widgetIndex)
self.menuIndex = menuIndex or 1
self.widgetIndex = widgetIndex or 1
2020-08-29 11:36:59 +02:00
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)
2020-10-03 14:33:10 +02:00
if (self.scene.menusystem.menus["useMenu"] == nil) then
local keys = self.scene:getKeys(1)
if (keys["left"].isPressed) then
2021-03-13 14:11:04 +01:00
self.widgetIndex = 1
2020-10-03 14:33:10 +02:00
self:getMenu(self.menuIndex - 1)
self.scene.assets.sfx["mBeep"]:play()
2020-10-03 14:33:10 +02:00
end
if (keys["right"].isPressed) then
2021-03-13 14:11:04 +01:00
self.widgetIndex = 1
2020-10-03 14:33:10 +02:00
self:getMenu(self.menuIndex + 1)
self.scene.assets.sfx["mBeep"]:play()
2020-10-03 14:33:10 +02:00
end
2020-08-29 11:36:59 +02:00
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
2021-03-13 14:11:04 +01:00
function ItemsScreen:setMenu(widgetId)
2020-08-29 11:36:59 +02:00
self.pocket = game.loot:getPocketById(self.menuIndex)
local w = 128+32
2020-10-03 14:33:10 +02:00
menu.ListMenu(self.scene, "menu", const.X2 - w, const.Y + 8, w, 9, true)
2020-08-29 11:36:59 +02:00
for i,item in ipairs(self.pocket.list) do
--menu.DualTextWidget(self.scene, "menu", item.name, "x" .. item.number)
2021-03-13 14:11:04 +01:00
ItemWidget(self.scene, self.pocket.name, item, i)
2020-08-29 11:36:59 +02:00
end
BackWidget(self.scene)
self.scene.menusystem.menus["menu"]:setCancelWidget()
self.scene.menusystem:switchMenu("menu")
2020-10-03 14:33:10 +02:00
self.scene.menusystem.menus["menu"].isLocked = true
2021-03-13 14:11:04 +01:00
self.scene.menusystem.menus["menu"].widget.selected = math.max(1, self.widgetIndex)
2021-03-23 14:07:58 +01:00
self.scene.menusystem.menus["menu"].cursorTransition = math.max(0, self.widgetIndex - 1)
self.scene.menusystem:setSoundFromSceneAssets("mBeep")
2020-10-03 14:33:10 +02:00
end
2021-03-13 14:11:04 +01:00
function ItemsScreen:setUseMenu(item, widgetId)
2020-10-03 14:33:10 +02:00
local w = 96
menu.ListMenu(self.scene, "useMenu", const.X2 + 16 - w, const.Y2 - 48, w, 3, true)
2021-03-13 14:11:04 +01:00
UseWidget(self.scene, self.pocket.name, item, widgetId)
DropWidget(self.scene, self.pocket.name, item, widgetId)
2020-10-03 14:33:10 +02:00
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")
2020-08-29 11:36:59 +02:00
end
function ItemsScreen:draw()
self.scene.assets.fonts["small"]:setLineHeight(16/18)
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
2020-10-03 14:33:10 +02:00
-- Items Widgets
2021-03-13 14:11:04 +01:00
function ItemWidget:new(scene, pocket, item, widgetId)
2020-08-29 11:36:59 +02:00
self.item = item
self.itemData = core.datas:get("items", self.item.name)
2020-09-13 13:25:44 +02:00
self.pocket = pocket
2021-03-13 14:11:04 +01:00
self.widgetId = widgetId
2020-10-03 14:33:10 +02:00
ItemWidget.super.new(self, scene, "menu", self.itemData.fullname, "x" .. utils.math.numberToString(self.item.number, 2))
2020-08-29 11:36:59 +02:00
end
function ItemWidget:selectAction()
self.scene.currentScreen.desc = self.itemData.description
2020-09-13 13:25:44 +02:00
self.scene.currentScreen.effects = game.loot:getEffectStrings(self.pocket, self.item.name)
2020-08-29 11:36:59 +02:00
end
function ItemWidget:action()
self.scene.assets:playSFX("mSelect")
2021-03-13 14:11:04 +01:00
self.scene.currentScreen:setUseMenu(self.item, self.widgetId)
2020-10-03 14:33:10 +02:00
end
2020-08-29 11:36:59 +02:00
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")
2020-08-29 11:36:59 +02:00
self.scene.screens.mainmenu["pause"](self.scene, "main", 3)
end
2020-10-03 14:33:10 +02:00
-- Use Menu Widget
2021-03-13 14:11:04 +01:00
function UseWidget:new(scene, pocket, item, widgetId)
2020-10-03 14:33:10 +02:00
self.item = item
self.itemData = core.datas:get("items", self.item.name)
2020-10-03 14:33:10 +02:00
self.pocket = pocket
2021-04-22 23:41:20 +02:00
self.pocketData = game.loot:getPocketByName(self.pocket)
2021-03-13 14:11:04 +01:00
self.widgetId = widgetId
2020-10-03 14:33:10 +02:00
UseWidget.super.new(self, scene, "useMenu", "Use", "")
2021-04-22 23:41:20 +02:00
if (not (self.itemData.usableOnMap or self.pocketData.isEquipement)) then
self.color = {0.6, 0.6, 0.6}
end
2020-10-03 14:33:10 +02:00
end
2021-03-13 14:11:04 +01:00
function UseWidget:action()
2021-04-22 23:41:20 +02:00
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
2021-03-13 14:11:04 +01:00
end
function DropWidget:new(scene, pocket, item, widgetId)
2020-10-03 14:33:10 +02:00
self.item = item
self.itemData = core.datas:get("items", self.item.name)
2020-10-03 14:33:10 +02:00
self.pocket = pocket
self.max = self.item.number
2021-03-13 14:11:04 +01:00
self.widgetId = widgetId
2020-10-03 14:33:10 +02:00
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
2021-03-13 14:11:04 +01:00
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()
2021-03-13 14:11:04 +01:00
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
2020-10-03 14:33:10 +02:00
function ResumeWidget:new(scene)
ResumeWidget.super.new(self, scene, "useMenu", "Back", "")
end
function ResumeWidget:action()
self.scene.assets:playSFX("mBack")
2020-10-03 14:33:10 +02:00
self.scene.currentScreen:removeUseMenu()
end
2020-08-29 11:36:59 +02:00
return ItemsScreen