feat: port battle menu to new menusystem

Fix #38
This commit is contained in:
Kazhnuz 2020-08-20 15:33:28 +02:00
parent 508b044669
commit 38a1d4b07d
8 changed files with 337 additions and 333 deletions

View file

@ -0,0 +1,74 @@
local List = require "game.modules.menus.list"
local fancy = {}
fancy.FancyMenu = List.ListMenu:extend()
fancy.BaseWidget = List.CenteredWidget:extend()
fancy.SubMenuWidget = fancy.BaseWidget:extend()
local MENU_ITEM_HEIGHT = 17
local gui = require "game.modules.gui"
function fancy.FancyMenu:new(scene, name, x, y, w, itemNumber, haveBiseau)
fancy.FancyMenu.super.new(self, scene, name, x, y, w, itemNumber, false)
self.haveBiseau = haveBiseau
end
function fancy.FancyMenu:getCursorPosition()
if (self.haveBiseau) then
local addition = MENU_ITEM_HEIGHT
local x = self.x + 4 + ((self.cursorTransition) * addition)/2
local y = self.y + ((self.cursorTransition) * addition)
return x, y
else
return fancy.FancyMenu.super.getCursorPosition(self)
end
end
function fancy.FancyMenu:getNextPosition(x, y, h)
if (self.haveBiseau) then
return (x + (h/2)), y+h
else
return fancy.FancyMenu.super.getNextPosition(self, x, y, h)
end
end
function fancy.FancyMenu:clone(name)
return fancy.FancyMenu(self.scene, name, self.x, self.y, self.w, self.itemNumber, self.haveBiseau)
end
function fancy.FancyMenu:addSubMenuWidget(newmenu, name)
fancy.SubMenuWidget(self.scene, self.name, newmenu, name)
end
-- FancyWidgets
-- Add Fancy Widgets
function fancy.BaseWidget:new(scene, menu_name, label, label2)
self.label2 = label2
fancy.BaseWidget.super.new(self, scene, menu_name, label)
self.box = gui.newChoiceBack(self.menu.w + 24)
end
function fancy.BaseWidget:drawCanvas()
love.graphics.draw(self.box, 0, 0)
local h = math.floor(self.height / 2) - (self.font:getHeight() / 2) - 1
self.font:draw(self.label, 16, h, -1, "left")
self.font:draw(self.label2, self.width -8, h, -1, "right")
end
-- Widget de sous-menu
function fancy.SubMenuWidget:new(scene, menu_name, newmenu, label)
local label2 = ""
if (label ~= "Back") then
label2 = ">"
end
fancy.SubMenuWidget.super.new(self, scene, menu_name, label, label2)
self.newmenu = newmenu
end
function fancy.SubMenuWidget:action()
self.scene.menusystem:switchMenu(self.newmenu)
self.scene.menusystem.menus[self.newmenu]:activationAction()
end
return fancy

View file

@ -17,20 +17,17 @@ function list.ListMenu:new(scene, name, x, y, w, itemNumber, isBoxed)
list.ListMenu.super.new(self, scene, name, x, y, w, h, itemNumber)
self.cursorTexture = love.graphics.newImage("assets/gui/cursor-menulist.png")
self.cursorTransition = 0
self.parent = ""
self.submenus = {}
self.itemNumber = itemNumber
self.isBoxed = isBoxed
if (self.isBoxed) then
self.box = gui.newTextBox("assets/gui/dialogbox.png", w+16, h+16)
self.box = gui.newTextBox("assets/gui/dialogbox.png", w, h+16)
end
end
function list.ListMenu:addSubMenu(name, nicerName)
print("adding submenu", name, "to", self.name)
local submenu = self:clone(name)
submenu:setAsSubmenu(self.name)
self:addSubMenuWidget(name, nicerName)
table.insert(self.submenus, name)
end
@ -39,16 +36,15 @@ function list.ListMenu:addSubMenuWidget(newmenu, name)
list.SubMenuWidget(self.scene, self.name, newmenu, name)
end
function list.ListMenu:finalize()
print("finalizing", self.name)
if (self.parent ~= "") then
self:addSubMenuWidget(self.parent, "Back")
function list.ListMenu:finalize(parent)
if (parent ~= "" and parent ~= nil) then
self:addSubMenuWidget(parent, "Back")
self:setCancelWidget()
end
for i,name in ipairs(self.submenus) do
if (self.menusystem.menus[name] ~= nil) then
self.menusystem.menus[name]:finalize()
self.menusystem.menus[name]:finalize(self.name)
end
end
end
@ -57,14 +53,6 @@ function list.ListMenu:clone(name)
return list.ListMenu(self.scene, name, self.x, self.y, self.w, self.itemNumber, self.isBoxed)
end
function list.ListMenu:getSubmenuWidget()
return nil
end
function list.ListMenu:setAsSubmenu(parent)
self.parent = parent
end
function list.ListMenu:update(dt)
list.ListMenu.super.update(self, dt)
self:updateCursorPosition(dt)
@ -90,14 +78,14 @@ end
function list.ListMenu:getCursorPosition()
local addition = MENU_ITEM_HEIGHT
local x = self.x + 4
local y = self.y + ((self.cursorTransition) * addition)
local x = self.x + 6
local y = self.y + ((self.cursorTransition) * addition) - 1
return x, y
end
function list.ListMenu:draw()
if (self.isBoxed) then
love.graphics.draw(self.box, self.x - 8, self.y - 8)
love.graphics.draw(self.box, self.x, self.y - 8)
end
self:updateView()
local widgety = self.y
@ -132,7 +120,7 @@ function list.CenteredWidget:new(scene, menu_name, label)
end
function list.CenteredWidget:drawCanvas()
local h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
local h = math.floor(self.height / 2) - (self.font:getHeight() / 2) - 1
self.font:draw(self.label, self.width / 2, h, -1, "center")
end
@ -144,8 +132,8 @@ end
function list.DualTextWidget:drawCanvas()
local h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
self.font:draw(self.label, 0, h, -1, "left")
self.font:draw(self.label2, self.width, h, -1, "right")
self.font:draw(self.label, 16, h, -1, "left")
self.font:draw(self.label2, self.width - 8, h, -1, "right")
end
-- Widget de sous-menu

View file

@ -0,0 +1,13 @@
local Widget = require "core.modules.menusystem.widgets"
local RadianceWidget = Widget.Text:extend()
function RadianceWidget:new(scene, name, label)
self.scene = scene
local label = label or ""
local font = scene.assets.fonts["small"]
font:setFilter("shadow")
local widgetMenu = scene.menusystem.menus[name]
RadianceWidget.super.new(self, widgetMenu, font, label)
end
return RadianceWidget

View file

@ -3,7 +3,7 @@ local Scene = require "core.modules.scenes"
local BattleSystem = Scene:extend()
local World = require "scenes.battlesystem.world"
local MenuSystem = require "scenes.battlesystem.menu"
local MenuSystem = require "scenes.battlesystem.menus.controller"
local Turns = require "scenes.battlesystem.controllers"
local VictoryScreen = require "scenes.battlesystem.screens.victory"

View file

@ -1,309 +0,0 @@
local ListBox = require "core.modules.menusystem.listbox"
local Widget = require "core.modules.menusystem.widgets"
local MenuConstructor = Object:extend()
local CharacterMenu = ListBox:extend()
local BattleWidget = Widget.Text:extend()
local ActionWidget = BattleWidget:extend()
local SubMenuWidget = BattleWidget:extend()
local BackMenuWidget = BattleWidget:extend()
local SkillWidget = BattleWidget:extend()
local ItemWidget = BattleWidget:extend()
local MENUPOS_X1, MENUPOS_X2, MENUPOS_Y = 88, 32, 72
local MENU_WIDTH, MENU_ITEM_HEIGHT = 180, 17
local MENU_ITEM_NUMBER = 6
gui = require "game.modules.gui"
function MenuConstructor:new( controller )
self.scene = controller
end
function MenuConstructor:reconstruct(character)
core.debug:print("cbs/menu", "Reconstructing the menu")
self.scene.menusystem:reset()
self:build(character)
self.scene.menusystem:switchMenu("BaseMenu")
end
function MenuConstructor:build(character)
core.debug:print("cbs/menu", "Building the menu")
self:buildBaseMenu(character)
self:buildSkillMenu(character)
self:buildObjectMenu(character)
self.scene.menusystem:setSoundFromSceneAssets("mBeep")
self.scene.menusystem:activate()
end
function MenuConstructor:buildBaseMenu(character)
CharacterMenu(self.scene, "BaseMenu", MENUPOS_X1 - 16, MENUPOS_Y)
ActionWidget(character, "BaseMenu", "attack")
SubMenuWidget(character, "BaseMenu", "skills", "SkillMenu")
SubMenuWidget(character, "BaseMenu", "objects", "ObjectMenu")
ActionWidget(character, "BaseMenu", "defend")
ActionWidget(character, "BaseMenu", "flee")
end
function MenuConstructor:set(currentCharacter)
self:reconstruct(currentCharacter)
end
function MenuConstructor:buildSkillMenu(character)
CharacterMenu(self.scene, "SkillMenu", MENUPOS_X1 - 16, MENUPOS_Y)
local list = character.abstract.skills
for k, skill in pairs(list) do
SkillWidget(character, "SkillMenu", skill.name, "")
end
SubMenuWidget(character, "SkillMenu", "back", "BaseMenu")
self.scene.menusystem.menus["SkillMenu"]:setCancelWidget()
end
function MenuConstructor:buildObjectMenu(character)
CharacterMenu(self.scene, "ObjectMenu", MENUPOS_X1 - 16, MENUPOS_Y)
for i,pocket in ipairs(game.loot.inventory) do
if (pocket.inBattle) then
CharacterMenu(self.scene, pocket.name, MENUPOS_X1 - 16, MENUPOS_Y)
SubMenuWidget(character, "ObjectMenu", pocket.fullname, pocket.name)
for j,itemData in ipairs(pocket.list) do
ItemWidget(character, pocket.name, itemData.name)
end
SubMenuWidget(character, pocket.name, "back", "ObjectMenu")
self.scene.menusystem.menus[pocket.name]:setCancelWidget()
end
end
SubMenuWidget(character, "ObjectMenu", "back", "BaseMenu")
self.scene.menusystem.menus["ObjectMenu"]:setCancelWidget()
end
function MenuConstructor:unset( )
self.isActive = false
end
-- CHARACTER_MENU
-- The actuals menus in the character menu
function CharacterMenu:new(scene, name, x, y)
self.scene = scene
local w, h = MENU_WIDTH, MENU_ITEM_NUMBER * MENU_ITEM_HEIGHT
CharacterMenu.super.new(self, scene.menusystem, name, x, y, w, h, MENU_ITEM_NUMBER)
self.cursorTexture = love.graphics.newImage("assets/gui/cursor-menulist.png")
self.cursorTransition = 0
end
function CharacterMenu:update(dt)
CharacterMenu.super.update(self, dt)
local relativecursor = self.widget.selected - self.view.firstSlot
local transition = self.cursorTransition - relativecursor
if math.abs(transition) < 0.1 then
self.cursorTransition = relativecursor
else
self.cursorTransition = (self.cursorTransition) + ((relativecursor) - (self.cursorTransition)) * dt*45
end
end
function CharacterMenu:drawCursor()
local addition = 17
local x = self.x + 4 + ((self.cursorTransition) * addition * 0.5)
local y = self.y + ((self.cursorTransition) * addition)
love.graphics.draw(self.cursorTexture, x, y)
end
function CharacterMenu:draw()
self:updateView()
local widgety = self.y
local widgetx = self.x
for i,v in ipairs(self.widget.list) do
if (i >= self.view.firstSlot) and (i < self.view.firstSlot + self.view.slotNumber) then
v:draw(widgetx, widgety, self.w, self.widget.h)
if self.widget.selected == i and self:haveFocus() == true then
v:drawSelected(widgetx, widgety, self.w, self.widget.h)
else
v:draw(widgetx, widgety, self.w, self.widget.h)
end
widgety = widgety + self.widget.h
widgetx = widgetx + (self.widget.h/2)
end
end
end
-- WIDGETS
-- All widgets used by the Characters menus
-- Basic Battle Widget
-- The base used by all battle widgets
function BattleWidget:new(character, menu_name, label1, label2, translationdata)
local menu = self:getControllers(character, menu_name)
local translationdata = translationdata or "battle"
local label1 = label1 or ""
local label2 = label2 or ""
label1 = core.lang:translate(translationdata, label1)
local font = self.assets.fonts["small"]
BattleWidget.super.new(self, menu, font, label1)
self.back = gui.newChoiceBack( (MENU_WIDTH - 16) )
self.label2 = label2
end
function BattleWidget:getControllers(character, menu_name)
self.character = character or core.debug:error("cbs/widget", "character must not be nil")
self.scene = self.character.turnSystem.scene
self.assets = self.character.assets
self.menusystem = self.scene.menusystem
self.menuname = menu_name
local menu = self.menusystem.menus[menu_name] or error("menu " ..menu_name .. " doesn't exist")
return menu
end
-- Internal functions
function BattleWidget:update(dt)
BattleWidget.super.update(self, dt)
end
function BattleWidget:selectAction()
-- Rien de base, à voir ensuite comment je gère
end
function BattleWidget:drawCanvas()
love.graphics.draw(self.back, 0, 0)
h = math.floor(self.height / 2) - (self.font:getHeight() / 2) - 1
love.graphics.setColor(0, 0, 0, .8)
self.font:print(self.label, 17, h, "left")
self.font:print(self.label2, self.width - 48 + 1, h, "right")
utils.graphics.resetColor()
self.font:print(self.label, 16, h, "left")
self.font:print(self.label2, self.width - 48, h, "right")
end
function BattleWidget:action()
self.scene:flushKeys()
self.scene.menusystem:deactivate()
self:sendCharacterData()
end
-- External functions
function BattleWidget:sendCharacterData()
self.character:doNothing()
end
-- ActionWidget
-- The basic action widget
function ActionWidget:new(character, menu_name, action)
self.actionType = action or ""
ActionWidget.super.new(self, character, menu_name, action, "")
end
function ActionWidget:sendCharacterData()
self.character:doBasicAction(self.actionType)
end
-- SubMenuWidget
-- A simple widget to change menu
function SubMenuWidget:new(character, menu_name, label, newmenu)
local label2 = ""
self.sfx = "mBack"
if label ~= "back" then
label2 = ">"
self.sfx = "mBeep"
end
SubMenuWidget.super.new(self, character, menu_name, label, label2)
self.newmenu = newmenu or "BaseMenu"
end
function SubMenuWidget:action()
self.assets.sfx[self.sfx]:play()
self.scene.menusystem:switchMenu(self.newmenu)
end
-- BackMenuWidget
-- Quit the menu
function BackMenuWidget:new(character, menu_name)
BackMenuWidget.super.new(self, character, menu_name, "back", "")
end
function BackMenuWidget:sendCharacterData()
self.assets.sfx["mBack"]:play()
self.character:receiveBackSignal()
end
-- SkillWidget
-- A widget to handle skills
function SkillWidget:new(character, menu_name, skill)
self.skillname = skill
local label2 = "00"
self.skilldata = game.skills:getSkillData(skill)
if self.skilldata ~= nil then
label2 = self.skilldata.cost or 0
if label2 < 10 then
label2 = "0" .. label2
end
end
SkillWidget.super.new(self, character, menu_name, self.skillname, "-" .. label2, "skills")
end
function SkillWidget:sendCharacterData()
if self.skilldata ~= nil then
self.assets.sfx["mSelect"]:play()
self.character:useSkill(self.skillname)
else
core.debug:warning("cbs/menu", "skill " .. self.skillname .. " doesn't exist")
self.character:doNothing()
self.assets.sfx["mError"]:play()
end
end
-- ItemWidget
-- A widget to handle items
function ItemWidget:new(character, menu_name, item)
self.itemname = item
local label2 = "00"
self.number = game.loot:getItemNumber(menu_name, item)
self.itemdata = game.loot:getItemData(menu_name, item)
label2 = self.number
ItemWidget.super.new(self, character, menu_name, self.itemdata.fullname, "x" .. label2, "skills")
end
function ItemWidget:sendCharacterData()
self.character:doNothing()
self.assets.sfx["mError"]:play()
end
return MenuConstructor

View file

@ -0,0 +1,19 @@
local fancy = require "game.modules.menus.fancy"
local BattleMenu = fancy.FancyMenu:extend()
local MENUPOS_X1, MENUPOS_Y = 88, 72
local MENU_WIDTH = 180-24
local MENU_ITEM_NUMBER = 6
function BattleMenu:new(scene, name)
local x, y = MENUPOS_X1, MENUPOS_Y
local w = MENU_WIDTH
BattleMenu.super.new(self, scene, name, x, y, w, MENU_ITEM_NUMBER, true)
end
function BattleMenu:clone(name)
return BattleMenu(self.scene, name)
end
return BattleMenu

View file

@ -0,0 +1,75 @@
local ListBox = require "core.modules.menusystem.listbox"
local Widget = require "core.modules.menusystem.widgets"
local MenuConstructor = Object:extend()
local CharacterMenu = require "scenes.battlesystem.menus.battlemenu"
local widgets = require "scenes.battlesystem.menus.widgets"
function MenuConstructor:new( controller )
self.scene = controller
end
function MenuConstructor:reconstruct(character)
core.debug:print("cbs/menu", "Reconstructing the menu")
self.scene.menusystem:reset()
self:build(character)
self.scene.menusystem:switchMenu("BaseMenu")
end
function MenuConstructor:build(character)
core.debug:print("cbs/menu", "Building the menu")
self:buildBaseMenu(character)
self:buildSkillMenu(character)
self:buildObjectMenu(character)
self.scene.menusystem.menus["BaseMenu"]:finalize()
self.scene.menusystem:setSoundFromSceneAssets("mBeep")
self.scene.menusystem:activate()
end
function MenuConstructor:buildBaseMenu(character)
CharacterMenu(self.scene, "BaseMenu")
widgets.ActionWidget(character, "BaseMenu", "attack")
self:addSubMenu("BaseMenu", "SkillMenu", "Skills")
self:addSubMenu("BaseMenu", "ObjectMenu", "Objects")
widgets.ActionWidget(character, "BaseMenu", "defend")
widgets.ActionWidget(character, "BaseMenu", "flee")
end
function MenuConstructor:set(currentCharacter)
self:reconstruct(currentCharacter)
end
function MenuConstructor:buildSkillMenu(character)
local list = character.abstract.skills
for k, skill in pairs(list) do
widgets.SkillWidget(character, "SkillMenu", skill.name, "")
end
--SubMenuWidget(character, "SkillMenu", "back", "BaseMenu")
self.scene.menusystem.menus["SkillMenu"]:setCancelWidget()
end
function MenuConstructor:buildObjectMenu(character)
for i,pocket in ipairs(game.loot.inventory) do
if (pocket.inBattle) then
CharacterMenu(self.scene, pocket.name)
self:addSubMenu("ObjectMenu", pocket.name, pocket.fullname)
for j,itemData in ipairs(pocket.list) do
widgets.ItemWidget(character, pocket.name, itemData.name)
end
end
end
self.scene.menusystem.menus["ObjectMenu"]:setCancelWidget()
end
function MenuConstructor:addSubMenu(menu, submenu, widgetName)
self.scene.menusystem.menus[menu]:addSubMenu(submenu, widgetName)
end
function MenuConstructor:unset( )
self.isActive = false
end
return MenuConstructor

View file

@ -0,0 +1,144 @@
local fancy = require "game.modules.menus.fancy"
local widgets = {}
widgets.BattleWidget = fancy.BaseWidget:extend()
widgets.ActionWidget = widgets.BattleWidget:extend()
widgets.SubMenuWidget = widgets.BattleWidget:extend()
widgets.BackMenuWidget = widgets.BattleWidget:extend()
widgets.SkillWidget = widgets.BattleWidget:extend()
widgets.ItemWidget = widgets.BattleWidget:extend()
-- WIDGETS
-- All widgets used by the Characters menus
-- Basic Battle Widget
-- The base used by all battle widgets
function widgets.BattleWidget:new(character, menu_name, label1, label2)
local label1 = label1 or ""
local label2 = label2 or ""
self.character = character
local scene = self.character.turnSystem.scene
widgets.BattleWidget.super.new(self, scene, menu_name, label1, label2)
self.assets = self.scene.assets
end
-- Internal functions
function widgets.BattleWidget:selectAction()
-- Rien de base, à voir ensuite comment je gère
end
function widgets.BattleWidget:action()
self.scene:flushKeys()
self.scene.menusystem:deactivate()
self:sendCharacterData()
end
-- External functions
function widgets.BattleWidget:sendCharacterData()
self.character:doNothing()
end
-- ActionWidget
-- The basic action widget
function widgets.ActionWidget:new(character, menu_name, action)
self.actionType = action or ""
widgets.ActionWidget.super.new(self, character, menu_name, action, "")
end
function widgets.ActionWidget:sendCharacterData()
self.character:doBasicAction(self.actionType)
end
-- SubMenuWidget
-- A simple widget to change menu
function widgets.SubMenuWidget:new(character, menu_name, label, newmenu)
local label2 = ""
self.sfx = "mBack"
if label ~= "back" then
label2 = ">"
self.sfx = "mBeep"
end
SubMenuWidget.super.new(self, character, menu_name, label, label2)
self.newmenu = newmenu or "BaseMenu"
end
function widgets.SubMenuWidget:action()
self.assets.sfx[self.sfx]:play()
self.scene.menusystem:switchMenu(self.newmenu)
end
-- BackMenuWidget
-- Quit the menu
function widgets.BackMenuWidget:new(character, menu_name)
BackMenuWidget.super.new(self, character, menu_name, "back", "")
end
function widgets.BackMenuWidget:sendCharacterData()
self.assets.sfx["mBack"]:play()
self.character:receiveBackSignal()
end
-- SkillWidget
-- A widget to handle skills
function widgets.SkillWidget:new(character, menu_name, skill)
self.skillname = skill
local label2 = "00"
self.skilldata = game.skills:getSkillData(skill)
if self.skilldata ~= nil then
label2 = self.skilldata.cost or 0
if label2 < 10 then
label2 = "0" .. label2
end
end
widgets.SkillWidget.super.new(self, character, menu_name, self.skillname, "-" .. label2, "skills")
end
function widgets.SkillWidget:sendCharacterData()
if self.skilldata ~= nil then
self.assets.sfx["mSelect"]:play()
self.character:useSkill(self.skillname)
else
core.debug:warning("cbs/menu", "skill " .. self.skillname .. " doesn't exist")
self.character:doNothing()
self.assets.sfx["mError"]:play()
end
end
-- ItemWidget
-- A widget to handle items
function widgets.ItemWidget:new(character, menu_name, item)
self.itemname = item
local label2 = "00"
self.number = game.loot:getItemNumber(menu_name, item)
self.itemdata = game.loot:getItemData(menu_name, item)
label2 = self.number
widgets.ItemWidget.super.new(self, character, menu_name, self.itemdata.fullname, "x" .. label2, "skills")
end
function widgets.ItemWidget:sendCharacterData()
self.character:doNothing()
self.assets.sfx["mError"]:play()
end
return widgets