From abc415c7f3bc41f5b7ddd273e414b1c91d7032f5 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 15 Aug 2020 22:43:46 +0200 Subject: [PATCH] feat: initial work on inventory system --- .../datas/gamedata/items/init.lua | 51 ++++++++++ .../gamedata/items/medicines/chilidog.lua | 14 +++ sonic-radiance.love/game/loot.lua | 94 +++++++++++++++++-- sonic-radiance.love/game/utils/items.lua | 45 +++++++++ .../scenes/battlesystem/menu.lua | 52 ++++++---- .../scenes/debug/menu/init.lua | 15 +++ .../scenes/debug/menu/menu.lua | 13 +++ 7 files changed, 255 insertions(+), 29 deletions(-) create mode 100644 sonic-radiance.love/datas/gamedata/items/init.lua create mode 100644 sonic-radiance.love/datas/gamedata/items/medicines/chilidog.lua create mode 100644 sonic-radiance.love/game/utils/items.lua diff --git a/sonic-radiance.love/datas/gamedata/items/init.lua b/sonic-radiance.love/datas/gamedata/items/init.lua new file mode 100644 index 0000000..cecf8a9 --- /dev/null +++ b/sonic-radiance.love/datas/gamedata/items/init.lua @@ -0,0 +1,51 @@ +return { + [1] = { + name = "medicines", + fullname = "Medicines", + description = "Healing items", + inBattle = true + }, + [2] = { + name = "powerups", + fullname = "Powerups", + description = "Helping items usable in battle", + inBattle = true + }, + [3] = { + name = "wisps", + fullname = "Wisps", + description = "Little alien creatures that can attack ennemies", + inBattle = true + }, + [4] = { + name = "gloves", + fullname = "Gloves", + description = "All equipables gloves", + inBattle = false + }, + [5] = { + name = "shoes", + fullname = "Shoes", + description = "All equipables shoes", + inBattle = false + }, + [6] = { + name = "accessories", + fullname = "Accessories", + description = "All equipables accessories", + inBattle = false + }, + [7] = { + name = "chao", + fullname = "Chao's Items", + description = "All chao-related items", + inBattle = false + }, + [8] = { + name = "quest", + fullname = "Quest Items", + description = "All quest items", + inBattle = false + }, + +} diff --git a/sonic-radiance.love/datas/gamedata/items/medicines/chilidog.lua b/sonic-radiance.love/datas/gamedata/items/medicines/chilidog.lua new file mode 100644 index 0000000..165847a --- /dev/null +++ b/sonic-radiance.love/datas/gamedata/items/medicines/chilidog.lua @@ -0,0 +1,14 @@ +return { + name = "chilidog", + fullname = "Chili Dog", + description = "Sonic's favorite food.", + conditions = { + {"status", "ko", false} + }, + effects= { + {"heal", "hp", "fixed", 10} + }, + usableInBattle=true, + usableOnMap=true, + affectEverybody=false, +} diff --git a/sonic-radiance.love/game/loot.lua b/sonic-radiance.love/game/loot.lua index ee2997b..2cafdb7 100644 --- a/sonic-radiance.love/game/loot.lua +++ b/sonic-radiance.love/game/loot.lua @@ -1,21 +1,21 @@ local LootManager = Object:extend() +local Pocket = Object:extend() + +local itemutils = require "game.utils.items" function LootManager:new(controller) self.controller = controller self.rings = 0 self.inventory = {} - self.materials = {} - self.chaosDrives = {} - self.animals = {} + self.pocketIndex = {} + + self:generatePockets() end function LootManager:getData() local data = {} data.rings = self.rings data.inventory = self.inventory - data.materials = self.materials - data.chaosDrives = self.chaosDrives - data.animals = self.animals return data end @@ -23,9 +23,85 @@ function LootManager:setData(data) local data = data self.rings = data.rings self.inventory = data.inventory - self.materials = data.materials - self.chaosDrives = data.chaosDrives - self.animals = data.animals +end + +function LootManager:generatePockets() + local structure = require "datas.gamedata.items" + for i,pocketdata in ipairs(structure) do + local pocket = Pocket(pocketdata) + self.pocketIndex[pocketdata.name] = i + table.insert(self.inventory, pocket) + end +end + +function LootManager:getPocketById(id) + return self.inventory[id] +end + +function LootManager:getPocketByName(name) + return self.inventory[self.pocketIndex[name]] +end + +function LootManager:addItem(type, item, number) + local pocket = self:getPocketByName(type) + if (itemutils.itemExists(type, item)) then + pocket:addItem(item, number) + end +end + +function LootManager:getItemData(type, item) + return itemutils.getItemData(type, item) +end + +function LootManager:getItemNumber(type, item) + local pocket = self:getPocketByName(type) + return pocket:getItemNumber(item) +end + +-- POCKET OBJECT + +function Pocket:new(pocketdata) + self.name = pocketdata.name + self.fullname = pocketdata.fullname + self.inBattle = pocketdata.inBattle + self.description = pocketdata.description + self.list = {} +end + +function Pocket:addItem(item, number) + local success = false + + for i,itemData in ipairs(self.list) do + if (itemData.name == item) then + itemData.number = itemData.number + number + success = true + end + end + + if (not success) then + itemData = {} + itemData.name = item + itemData.number = number + table.insert(self.list, itemData) + end +end + +function Pocket:getItem(name) + for i, itemData in ipairs(self.list) do + if (itemData.name == name) then + return itemData + end + end + return nil +end + +function Pocket:getItemNumber(name) + local item = self:getItem(name) + if (item == nil) then + return 0 + else + return item.number + end end return LootManager diff --git a/sonic-radiance.love/game/utils/items.lua b/sonic-radiance.love/game/utils/items.lua new file mode 100644 index 0000000..bcae584 --- /dev/null +++ b/sonic-radiance.love/game/utils/items.lua @@ -0,0 +1,45 @@ +local ItemUtils = {} + +local datasutils = require "game.utils.datas" + +local DIR = "items" + +function ItemUtils.getBaseDirectory(lua) + return datasutils.concatDataFolder(DIR, lua) +end + +function ItemUtils.listCategories() + return require(ItemUtils.getBaseDirectory(true)) +end + +function ItemUtils.getCategoryDirectory(directory, lua) + return datasutils.concatFolder(ItemUtils.getBaseDirectory(lua), directory, lua) +end + +function ItemUtils.getItemsFromCategory(directory) + local folder = ItemUtils.getCategoryDirectory(directory, false) + local baseTable = love.filesystem.getDirectoryItems(folder) + return datasutils.luaFileListToModuleList(baseTable) +end + +function ItemUtils.getItemFile(category, item, lua) + local dir = ItemUtils.getCategoryDirectory(category, lua) + local path = datasutils.concatFolder(dir, item, lua) + if (not lua) then + path = path .. ".lua" + end + return path +end + +function ItemUtils.itemExists(category, item) + local path = ItemUtils.getItemFile(category, item, false) + local fileinfo = love.filesystem.getInfo(path) + return (fileinfo ~= nil) +end + +function ItemUtils.getItemData(category, item) + local path = ItemUtils.getItemFile(category, item, true) + return datasutils.copy(path) +end + +return ItemUtils diff --git a/sonic-radiance.love/scenes/battlesystem/menu.lua b/sonic-radiance.love/scenes/battlesystem/menu.lua index cc07e12..e64033a 100644 --- a/sonic-radiance.love/scenes/battlesystem/menu.lua +++ b/sonic-radiance.love/scenes/battlesystem/menu.lua @@ -9,6 +9,7 @@ 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 @@ -63,30 +64,21 @@ end function MenuConstructor:buildObjectMenu(character) CharacterMenu(self.scene, "ObjectMenu", MENUPOS_X1 - 16, MENUPOS_Y) - CharacterMenu(self.scene, "MedMenu", MENUPOS_X1 - 16, MENUPOS_Y) - CharacterMenu(self.scene, "RingMenu", MENUPOS_X1 - 16, MENUPOS_Y) - CharacterMenu(self.scene, "WispMenu", MENUPOS_X1 - 16, MENUPOS_Y) - CharacterMenu(self.scene, "OtherMenu", MENUPOS_X1 - 16, MENUPOS_Y) - - - SubMenuWidget(character, "ObjectMenu", "heal", "MedMenu") - SubMenuWidget(character, "ObjectMenu", "rings", "RingMenu") - SubMenuWidget(character, "ObjectMenu", "wisps", "WispMenu") - SubMenuWidget(character, "ObjectMenu", "other", "OtherMenu") + 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") - SubMenuWidget(character, "MedMenu", "back", "ObjectMenu") - SubMenuWidget(character, "RingMenu", "back", "ObjectMenu") - SubMenuWidget(character, "WispMenu", "back", "ObjectMenu") - SubMenuWidget(character, "OtherMenu", "back", "ObjectMenu") - self.scene.menusystem.menus["ObjectMenu"]:setCancelWidget() - - self.scene.menusystem.menus["MedMenu"]:setCancelWidget() - self.scene.menusystem.menus["RingMenu"]:setCancelWidget() - self.scene.menusystem.menus["WispMenu"]:setCancelWidget() - self.scene.menusystem.menus["OtherMenu"]:setCancelWidget() end @@ -307,4 +299,24 @@ function SkillWidget:sendCharacterData() 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 diff --git a/sonic-radiance.love/scenes/debug/menu/init.lua b/sonic-radiance.love/scenes/debug/menu/init.lua index 71ac027..9883846 100644 --- a/sonic-radiance.love/scenes/debug/menu/init.lua +++ b/sonic-radiance.love/scenes/debug/menu/init.lua @@ -4,6 +4,7 @@ local menu = require "scenes.debug.menu.menu" local DebugMenu = Scene:extend() local panels = require "scenes.debug.menu.infopanel" local battleutils = require "game.utils.battle" +local itemutils = require "game.utils.items" function DebugMenu:new() DebugMenu.super.new(self) @@ -67,6 +68,8 @@ function DebugMenu:buildSaveMenu() menu.AddRemoveWidget(self, "team", data) end + self:addInventory() + self:addSubMenu("load", "save", "Load Saves") for i=1, game.slotNumber do menu.LoadWidget(self, "load", i) @@ -86,6 +89,18 @@ function DebugMenu:addCharacterMenu(name, data) menu.SubMenuWidget(self, name, "characters", "Back", panels.Gamedata, nil) end +function DebugMenu:addInventory() + self:addSubMenu("inventory", "save", "Inventory") + for i,pocket in ipairs(game.loot.inventory) do + self:addSubMenu(pocket.name, "inventory", pocket.fullname) + for j, item in ipairs(itemutils.getItemsFromCategory(pocket.name)) do + menu.ItemWidget(self, pocket.name, itemutils.getItemData(pocket.name, item)) + end + menu.commons.SubMenuWidget(self, pocket.name, "inventory", "Back") + end + menu.commons.SubMenuWidget(self, "inventory", "save", "Back") +end + function DebugMenu:buildOtherMenu() self:addSubMenu("other", "BaseMenu", "Other gameplay") self:addSubMenu("battle", "other", "Sonic Battle Maps") diff --git a/sonic-radiance.love/scenes/debug/menu/menu.lua b/sonic-radiance.love/scenes/debug/menu/menu.lua index 7f49ddf..e48c41a 100644 --- a/sonic-radiance.love/scenes/debug/menu/menu.lua +++ b/sonic-radiance.love/scenes/debug/menu/menu.lua @@ -9,6 +9,7 @@ menu.LevelUpWidget = commons.DebugWidget:extend() menu.AddRemoveWidget = commons.DebugWidget:extend() menu.ChangeLeaderWidget = commons.DebugWidget:extend() menu.SubMenuWidget = commons.SubMenuWidget:extend() +menu.ItemWidget = commons.DebugWidget:extend() -- ExitWidget function menu.ExitWidget:new(scene, menuName) @@ -88,5 +89,17 @@ function menu.ChangeLeaderWidget:action() game.characters:setActiveCharacter(1) end +-- ItemWidget +function menu.ItemWidget:new(scene, menuName, itemdata) + menu.ItemWidget.super.new(self, scene, menuName, itemdata.fullname) + self.category = menuName + self.itemName = itemdata.name +end + +function menu.ItemWidget:action() + game.loot:addItem(self.category, self.itemName, 1) +end + + return menu