From 693150029c3a37f5d74466f5af383d850ad35fa9 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 2 Aug 2020 23:47:42 +0200 Subject: [PATCH] feat: add a team management panel Fix #10 --- sonic-radiance.love/game/characters.lua | 50 ++++++++++++++++++- .../game/modules/world/actors/player.lua | 2 +- .../scenes/debug/menu/infopanel/init.lua | 1 + .../scenes/debug/menu/infopanel/team.lua | 22 ++++++++ .../scenes/debug/menu/init.lua | 5 ++ .../scenes/debug/menu/menu.lua | 31 ++++++++++++ 6 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 sonic-radiance.love/scenes/debug/menu/infopanel/team.lua diff --git a/sonic-radiance.love/game/characters.lua b/sonic-radiance.love/game/characters.lua index 6b1dac7..ec91a66 100644 --- a/sonic-radiance.love/game/characters.lua +++ b/sonic-radiance.love/game/characters.lua @@ -86,11 +86,57 @@ end function CharacterManager:addToTeam(name) self:heal(name) - table.insert(self.team, name) + if (#self.team < 4) then + table.insert(self.team, name) + end end function CharacterManager:removeToTeam(teamid) - self.team[teamid] = "" + if (#self.team > 1) then + table.remove(self.team, teamid) + end +end + +function CharacterManager:getPositionInTeam(charName) + local charId = -1 + for i,name in ipairs(self.team) do + if (name == charName) then + charId = i + end + end + return charId +end + +function CharacterManager:addOrRemoveToTeam(charName) + local charId = self:getPositionInTeam(charName) + if (charId == -1) then + self:addToTeam(charName) + else + self:removeToTeam(charId) + if (charId < self.active) then + self.active = self.active - 1 + end + end + self:fixActiveCharacter() +end + +function CharacterManager:setActiveCharacter(direction) + local direction = direction or 1 + self.active = self.active + utils.math.sign(direction) + if (self.active > #self.team) then + self.active = 1 + end + if (self.active < 1) then + self.active = #self.team + end +end + +function CharacterManager:fixActiveCharacter() + if (self.active < 1) then + self.active = 1 + elseif (self.active > #self.team) then + self.active = #self.team + end end function CharacterManager:getActiveCharacter() diff --git a/sonic-radiance.love/game/modules/world/actors/player.lua b/sonic-radiance.love/game/modules/world/actors/player.lua index 5eea73b..c361ba9 100644 --- a/sonic-radiance.love/game/modules/world/actors/player.lua +++ b/sonic-radiance.love/game/modules/world/actors/player.lua @@ -6,7 +6,7 @@ function Player:new(world, x, y, z, id) Player.super.new(self, world, "player", x, y, 0, 16, 12, 24, true) self:setGravity(480*2) - self.charName = game.characters.team[1] + self.charName = game.characters:getActiveCharacter() self.assets:addSprite("player", "datas/gamedata/characters/" .. self.charName .. "/sprites") self:setSprite("player", 8, 10) self:cloneSprite() diff --git a/sonic-radiance.love/scenes/debug/menu/infopanel/init.lua b/sonic-radiance.love/scenes/debug/menu/infopanel/init.lua index d3b96a1..418e25e 100644 --- a/sonic-radiance.love/scenes/debug/menu/infopanel/init.lua +++ b/sonic-radiance.love/scenes/debug/menu/infopanel/init.lua @@ -3,4 +3,5 @@ local folder = "scenes.debug.menu.infopanel." return { Gamedata = require(folder .. "gamedata"), Character = require(folder .. "character"), + Team = require(folder .. "team"), } diff --git a/sonic-radiance.love/scenes/debug/menu/infopanel/team.lua b/sonic-radiance.love/scenes/debug/menu/infopanel/team.lua new file mode 100644 index 0000000..62aa68b --- /dev/null +++ b/sonic-radiance.love/scenes/debug/menu/infopanel/team.lua @@ -0,0 +1,22 @@ +local Parent = require "scenes.debug.menu.infopanel.parent" +local TeamPanel = Parent:extend() + +function TeamPanel:new() + TeamPanel.super.new(self) +end + +function TeamPanel:drawContent(x, y) + local debugString = "# Team" .. "\n" + for i,name in ipairs(game.characters.team) do + local char = game.characters.list[name] + debugString = debugString .. "- " .. char.name + if (game.characters.active == i) then + debugString = debugString .. " (Active) " + end + debugString = debugString .. "\n" + end + + love.graphics.print(debugString, x, y) +end + +return TeamPanel diff --git a/sonic-radiance.love/scenes/debug/menu/init.lua b/sonic-radiance.love/scenes/debug/menu/init.lua index 189e9d4..579348f 100644 --- a/sonic-radiance.love/scenes/debug/menu/init.lua +++ b/sonic-radiance.love/scenes/debug/menu/init.lua @@ -39,8 +39,11 @@ end function DebugMenu:buildSaveMenu() self:addSubMenu("save", "BaseMenu", "Save System") self:addSubMenu("characters", "save", "Characters") + self:addSubMenu("team", "save", "Team Formation", panels.Team, nil) + menu.ChangeLeaderWidget(self, "team") for name, data in pairs(game.characters.list) do self:addCharacterMenu(name, data) + menu.AddRemoveWidget(self, "team", data) end self:addSubMenu("load", "save", "Load Saves") @@ -53,10 +56,12 @@ function DebugMenu:buildSaveMenu() menu.commons.SubMenuWidget(self, "save", "BaseMenu", "Back") menu.commons.SubMenuWidget(self, "characters", "save", "Back") menu.commons.SubMenuWidget(self, "load", "save", "Back") + menu.SubMenuWidget(self, "team", "save", "Back", panels.Gamedata, nil) end function DebugMenu:addCharacterMenu(name, data) self:addSubMenu(name, "characters", data.fullname, panels.Character, data) + menu.LevelUpWidget(self, name, data) menu.SubMenuWidget(self, name, "characters", "Back", panels.Gamedata, nil) end diff --git a/sonic-radiance.love/scenes/debug/menu/menu.lua b/sonic-radiance.love/scenes/debug/menu/menu.lua index 0ba4c1c..7f49ddf 100644 --- a/sonic-radiance.love/scenes/debug/menu/menu.lua +++ b/sonic-radiance.love/scenes/debug/menu/menu.lua @@ -5,6 +5,9 @@ menu.ExitWidget = commons.DebugWidget:extend() menu.ShowBackgroundWidget = commons.DebugWidget:extend() menu.SaveWidget = commons.DebugWidget:extend() menu.LoadWidget = commons.DebugWidget:extend() +menu.LevelUpWidget = commons.DebugWidget:extend() +menu.AddRemoveWidget = commons.DebugWidget:extend() +menu.ChangeLeaderWidget = commons.DebugWidget:extend() menu.SubMenuWidget = commons.SubMenuWidget:extend() -- ExitWidget @@ -56,6 +59,34 @@ function menu.SubMenuWidget:action() self.scene.panel = self.panel(self.panelArgument) end +-- Level Up +function menu.LevelUpWidget:new(scene, menuName, character) + menu.ShowBackgroundWidget.super.new(self, scene, menuName, "Level Up") + self.character = character +end + +function menu.LevelUpWidget:action() + self.character:levelUp() +end + +-- Add Remove Widget +function menu.AddRemoveWidget:new(scene, menuName, character) + menu.AddRemoveWidget.super.new(self, scene, menuName, "Add/Remove " .. character.name) + self.character = character +end + +function menu.AddRemoveWidget:action() + game.characters:addOrRemoveToTeam(self.character.simplename) +end + +--Change Team Leader +function menu.ChangeLeaderWidget:new(scene, menuName) + menu.ShowBackgroundWidget.super.new(self, scene, menuName, "Change active character") +end + +function menu.ChangeLeaderWidget:action() + game.characters:setActiveCharacter(1) +end return menu