From 1f3d07c223a69a8839e1f01ff39edfa48498d6a8 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 19 Jul 2020 16:20:57 +0200 Subject: [PATCH] improvement(cbs): let the turn controller handle the hud --- .../controllers/fighters/character.lua | 18 ++++++++++ .../controllers/fighters/parent.lua | 12 +++++++ .../controllers/fighters/villain.lua | 20 +++++++++++ .../scenes/battlesystem/controllers/init.lua | 33 ++++++++++++++----- .../battlesystem/controllers/parent.lua | 8 ++++- .../battlesystem/controllers/player.lua | 6 ++++ .../scenes/battlesystem/gui/hud.lua | 18 ++++------ .../scenes/battlesystem/gui/simplehpbar.lua | 29 ++++++++++++++++ .../scenes/battlesystem/gui/statusbar.lua | 23 ++++++------- .../scenes/battlesystem/init.lua | 9 +---- 10 files changed, 137 insertions(+), 39 deletions(-) create mode 100644 sonic-radiance.love/scenes/battlesystem/gui/simplehpbar.lua diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/character.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/character.lua index ffcfd33..96f5b1f 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/character.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/character.lua @@ -1,12 +1,20 @@ local FighterParent = require "scenes.battlesystem.controllers.fighters.parent" local HeroFighter = FighterParent:extend() +local StatusBar = require "scenes.battlesystem.gui.statusbar" + local POSITIONS = {3, 1, 5} local HEROES_LINE = 3; function HeroFighter:new(owner, character, id) self.name = character self.super.new(self, owner, true, id) + + self.statusbar = StatusBar(self) +end + +function HeroFighter:updateAssets(dt) + self.statusbar:update(dt) end function HeroFighter:getAbstract() @@ -26,4 +34,14 @@ function HeroFighter:endAction() end +-- DRAW FUNCTIONS +function HeroFighter:drawIcon(x, y) + local iconID = 1 + self.assets.tileset["charicons"]:drawTile(iconID, x, y) +end + +function HeroFighter:drawHUD() + self.statusbar:draw() +end + return HeroFighter diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua index 7a6a9d5..35aa413 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/parent.lua @@ -7,6 +7,7 @@ function FighterParent:new(owner, isHero, id) self.turnSystem = owner.turnSystem self.world = owner.world self.isHero = isHero + self.assets = self.turnSystem.scene.assets -- Note : l'ID est ici relatif à chaque quand, il n'est donc pas unique, -- ce qui est unique étant le combo id + isHero @@ -34,6 +35,10 @@ function FighterParent:update(dt) end end +function FighterParent:updateAssets(dt) + -- Vide pour l'instant +end + function FighterParent:setActive() counter = 0 self.isActive = true @@ -73,4 +78,11 @@ function FighterParent:canFight() return true end +-- DRAW FUNCTIONS + +function FighterParent:drawIcon(x, y) + love.graphics.setColor(1, 1, 1, 1) + love.graphics.rectangle("fill", x, y, 16, 16) +end + return FighterParent diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/villain.lua b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/villain.lua index 5a392ec..45ac549 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/fighters/villain.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/fighters/villain.lua @@ -1,14 +1,23 @@ local FighterParent = require "scenes.battlesystem.controllers.fighters.parent" local VillainFighter = FighterParent:extend() +local SimpleHPBar = require "scenes.battlesystem.gui.simplehpbar" + local POSITIONS = {3, 1, 5} local ENNEMY_LINE = 10; function VillainFighter:new(owner, ennemy, id) self.name = ennemy self.super.new(self, owner, false, id) + + self.hpbar = SimpleHPBar(self.abstract.hp) end +function VillainFighter:updateAssets(dt) + self.hpbar:update(dt) +end + + function VillainFighter:getAbstract() return game.ennemies:getEnnemyData(self.name) end @@ -26,4 +35,15 @@ function VillainFighter:endAction() end +-- DRAW FUNCTIONS +function VillainFighter:drawIcon(x, y) + love.graphics.setColor(1, 0, 0, 1) + love.graphics.rectangle("fill", x, y, 16, 16) + love.graphics.setColor(1, 1, 1, 1) +end + +function VillainFighter:drawHUD(x, y) + self.hpbar:draw(x, y) +end + return VillainFighter diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/init.lua b/sonic-radiance.love/scenes/battlesystem/controllers/init.lua index c418647..0455c1d 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/init.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/init.lua @@ -3,15 +3,14 @@ local TurnController = Object:extend() local Player = require "scenes.battlesystem.controllers.player" local Ennemy = require "scenes.battlesystem.controllers.ennemy" +local HUD = require "scenes.battlesystem.gui.hud" + local maputils = require "scenes.battlesystem.utils" function TurnController:new(scene) self.scene = scene self.world = scene.world - self.player = Player(self) - self.ennemies = Ennemy(self) - self.isActive = false self.currentlyPlaying = "" @@ -24,13 +23,29 @@ function TurnController:new(scene) self.actionList = {} self.currentFighter = nil + + self.hud = HUD(self) + + self.player = Player(self) + self.ennemies = Ennemy(self) end function TurnController:startBattle() self.isActive = true + self.hud:movePlayerHUD(true) +end + +function TurnController:finishBattle() + self.isBattleActive = false + self.actionlist = {} + self.hud:movePlayerHUD(false) + self.scene:finishBattle() end function TurnController:update(dt) + self.player:update(dt) + self.ennemies:update(dt) + self.hud:update(dt) if (self.isActive) then if (self.currentFighter ~= nil) then self.currentFighter:update(dt) @@ -47,7 +62,7 @@ function TurnController:nextAction() self.turns.current = self.turns.current + 1 core.debug:print("cbs/turns", "switching to next action") end - --self.scene.hud:moveBattleCursor(self.turns.current) + self.hud:moveBattleCursor(self.turns.current) self:startAction() end @@ -86,13 +101,15 @@ function TurnController:endAction() self.currentlyPlaying = "" end -function TurnController:drawTurnList(x, y) - -end - function TurnController:sendSignalToCurrentBattler(signal, subSignal) self.actionList[self.turns.current].actor:receiveSignal(signal, subSignal) end +function TurnController:draw() + self.hud:draw() + self.player:draw() + self.ennemies:draw() +end + return TurnController diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/parent.lua b/sonic-radiance.love/scenes/battlesystem/controllers/parent.lua index 940a832..5bdf754 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/parent.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/parent.lua @@ -20,7 +20,9 @@ function FighterControllerParent:setActive(activeActor) end function FighterControllerParent:update(dt) - + for i, fighter in ipairs(self.list) do + fighter:updateAssets(dt) + end end function FighterControllerParent:endAction() @@ -42,5 +44,9 @@ function FighterControllerParent:putActions(actionList) end end +function FighterControllerParent:draw() + --Aucun dessin par defaut, ils sont géré différements +end + return FighterControllerParent diff --git a/sonic-radiance.love/scenes/battlesystem/controllers/player.lua b/sonic-radiance.love/scenes/battlesystem/controllers/player.lua index 6132d4f..007e734 100644 --- a/sonic-radiance.love/scenes/battlesystem/controllers/player.lua +++ b/sonic-radiance.love/scenes/battlesystem/controllers/player.lua @@ -14,4 +14,10 @@ function HeroFighterController:initHeroes() end end +function HeroFighterController:draw() + for i, hero in ipairs(self.list) do + hero:drawHUD() + end +end + return HeroFighterController diff --git a/sonic-radiance.love/scenes/battlesystem/gui/hud.lua b/sonic-radiance.love/scenes/battlesystem/gui/hud.lua index 469cad4..2c1a570 100644 --- a/sonic-radiance.love/scenes/battlesystem/gui/hud.lua +++ b/sonic-radiance.love/scenes/battlesystem/gui/hud.lua @@ -4,11 +4,11 @@ local gui = require "game.modules.gui" local TweenManager = require "game.modules.tweenmanager" -function HUD:new(scene) - self.scene = scene - self.world = scene.world - self.assets = scene.assets - self.turns = scene.turns +function HUD:new(turns) + self.turns = turns + self.scene = turns.scene + self.world = self.scene.world + self.assets = self.scene.assets self.frame = gui.newBorder(424, 30, 4) self.tweens = TweenManager(self) @@ -38,14 +38,10 @@ function HUD:getPlayerHUDPosition() end function HUD:draw() - for i, battler in ipairs(self.world.battlers) do - battler:drawHUD() - end - for i, action in ipairs(self.turns.actionList) do - action.actor:drawIcon(4 + (i-1)*(20), 6) + action.fighter:drawIcon(4 + (i-1)*(20), 6) end - local cursorx = self.battlerCursor * 20 - 6 + local cursorx = self.battlerCursor * 20 - 8 if #self.turns.actionList > 0 then self.assets.images["menucursor"]:draw(cursorx, 26, math.rad(-90), 1, 1, 4, 8) diff --git a/sonic-radiance.love/scenes/battlesystem/gui/simplehpbar.lua b/sonic-radiance.love/scenes/battlesystem/gui/simplehpbar.lua new file mode 100644 index 0000000..c568eb2 --- /dev/null +++ b/sonic-radiance.love/scenes/battlesystem/gui/simplehpbar.lua @@ -0,0 +1,29 @@ +local SimpleHPBar = Object:extend() + +local TweenManager = require "game.modules.tweenmanager" +local gui = require "game.modules.gui" + +function SimpleHPBar:new(hp) + self.tweens = TweenManager(self) + self.hp = hp + self.baseHP = hp +end + +function SimpleHPBar:setHP(newHP) + self.tweens:newTween(0, 0.1, {hp = newHP}, 'inCubic') +end + +function SimpleHPBar:update(dt) + self.tweens:update(dt) +end + +function SimpleHPBar:draw(x, y) + love.graphics.setColor(0, 0, 0, 1) + gui.drawBar(x, y, 26, 4) + love.graphics.setColor(248/255, 160/255, 0, 1) + local bar = math.floor(24 * (self.hp / self.baseHP)) + gui.drawBar(x, y + 1, bar, 2) + love.graphics.setColor(1, 1, 1, 1) +end + +return SimpleHPBar diff --git a/sonic-radiance.love/scenes/battlesystem/gui/statusbar.lua b/sonic-radiance.love/scenes/battlesystem/gui/statusbar.lua index c0be16f..9dd45ea 100644 --- a/sonic-radiance.love/scenes/battlesystem/gui/statusbar.lua +++ b/sonic-radiance.love/scenes/battlesystem/gui/statusbar.lua @@ -6,16 +6,17 @@ local gui = require "game.modules.gui" local HUDBASE = 8 local HUDSEP = 152 -function StatusBar:new(actor) - self.actor = actor - self.hud = self.actor.scene.hud - self.assets = self.actor.assets +function StatusBar:new(fighter) + self.fighter = fighter + self.hud = fighter.turnSystem.hud + self.assets = self.fighter.assets - self.charid = self.actor.charid - self.stats = game.characters.list[self.charid].stats + self.charid = self.fighter.name + self.stats = self.fighter:getStats() + self.abstract = self.fighter.abstract - self.hp = self.stats.hp - self.pp = self.stats.pp + self.hp = self.abstract.hp + self.pp = self.abstract.pp self.tweens = TweenManager(self) end @@ -41,8 +42,8 @@ function StatusBar:drawEmblem(x, y) end function StatusBar:draw() - local x = HUDBASE + (self.actor.charnumber-1)*HUDSEP - local y = self.actor.scene.hud:getPlayerHUDPosition() + local x = HUDBASE + (self.fighter.id-1)*HUDSEP + local y = self.hud:getPlayerHUDPosition() self:drawEmblem(x, y) self.assets.images["statusbar"]:draw(x+12, y-6) @@ -63,7 +64,7 @@ function StatusBar:draw() love.graphics.print(math.floor(self.hp) .. "/" .. hpmax, x+34, y+5) love.graphics.print(math.floor(self.pp) .. "/" .. ppmax, x+28, y+17) - local lvl = game.characters.list[self.charid].stats.level + local lvl = self.abstract.level if lvl < 100 then lvl = "0" .. lvl diff --git a/sonic-radiance.love/scenes/battlesystem/init.lua b/sonic-radiance.love/scenes/battlesystem/init.lua index 0e99bc9..8882278 100644 --- a/sonic-radiance.love/scenes/battlesystem/init.lua +++ b/sonic-radiance.love/scenes/battlesystem/init.lua @@ -6,8 +6,6 @@ local World = require "scenes.battlesystem.world" local MenuSystem = require "scenes.battlesystem.menu" local Turns = require "scenes.battlesystem.controllers" -local HUD = require "scenes.battlesystem.gui.hud" - local VictoryScreen = require "scenes.battlesystem.screens.victory" function BattleSystem:new() @@ -32,17 +30,13 @@ function BattleSystem:initManagers() self.world = World(self) self.menu = MenuSystem(self) self.turns = Turns(self) - - self.hud = HUD(self) end function BattleSystem:startBattle() self.turns:startBattle() - self.hud:movePlayerHUD(true) end function BattleSystem:finishBattle() - self.hud:movePlayerHUD(false) self.assets:setMusic("assets/music/victory.mp3") self.assets:playMusic() self.screen = VictoryScreen(self) @@ -58,7 +52,6 @@ end function BattleSystem:update(dt) self.world:update(dt) self.turns:update(dt) - self.hud:update(dt) if (self.screen ~= nil) then self.screen:update(dt) end @@ -66,7 +59,7 @@ end function BattleSystem:draw() self.world:draw() - self.hud:draw() + self.turns:draw() if (self.screen ~= nil) then self.screen:draw() end