From 873cf12140036449c7f1ee03ffa04ac2761e848b Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 1 May 2020 14:19:00 +0200 Subject: [PATCH] chore: separate HUD from world logic --- .../scenes/battlesystem/gui/hud.lua | 65 +++++++++++++++++++ .../scenes/battlesystem/init.lua | 7 ++ .../scenes/battlesystem/world.lua | 50 +------------- 3 files changed, 75 insertions(+), 47 deletions(-) create mode 100644 sonic-radiance.love/scenes/battlesystem/gui/hud.lua diff --git a/sonic-radiance.love/scenes/battlesystem/gui/hud.lua b/sonic-radiance.love/scenes/battlesystem/gui/hud.lua new file mode 100644 index 0000000..0ece6c3 --- /dev/null +++ b/sonic-radiance.love/scenes/battlesystem/gui/hud.lua @@ -0,0 +1,65 @@ +local HUD = Object:extend() + +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.frame = gui.newBorder(424, 30, 4) + self.tweens = TweenManager(self) + + self.playerHUDPosition = -64 + self.battlerCursor = self.world.turns.current +end + +function HUD:update(dt) + self.tweens:update(dt) +end + +function HUD:movePlayerHUD(beginBattle) + if (beginBattle) then + self.tweens:newTween(0, 0.4, {playerHUDPosition = 36}, 'inCubic') + else + self.tweens:newTween(0, 0.4, {playerHUDPosition = -64}, 'inCubic') + end +end + +function HUD:moveBattleCursor(moveCursorTo) + self.tweens:newTween(0, 0.2, {battlerCursor = moveCursorTo}, 'inCubic') +end + +function HUD:getPlayerHUDPosition() + return self.playerHUDPosition +end + +function HUD:draw() + for i, battler in ipairs(self.world.battlers) do + battler:drawHUD() + end + + for i, action in ipairs(self.world.actionlist) do + action.actor:drawIcon(4 + (i-1)*(20), 6) + end + local cursorx = self.battlerCursor * 20 - 6 + + if #self.world.actionlist > 0 then + self.assets.images["menucursor"]:draw(cursorx, 26, math.rad(-90), 1, 1, 4, 8) + end + + local x, y = 362, 3 + + love.graphics.draw(self.frame, 424, 20, 0, -1, -1) + self.assets.images["hudturn"]:draw(x, y) + self.assets.fonts["hudnbrs"]:set() + local turnnbr = self.world.turns.number + if (turnnbr < 10) then + turnnbr = "0" .. turnnbr + end + love.graphics.print(turnnbr, x + 33, y + 1) +end + +return HUD diff --git a/sonic-radiance.love/scenes/battlesystem/init.lua b/sonic-radiance.love/scenes/battlesystem/init.lua index 784ce53..018ff2a 100644 --- a/sonic-radiance.love/scenes/battlesystem/init.lua +++ b/sonic-radiance.love/scenes/battlesystem/init.lua @@ -5,6 +5,8 @@ local BattleSystem = Scene:extend() local World = require "scenes.battlesystem.world" local MenuSystem = require "scenes.battlesystem.menu" +local HUD = require "scenes.battlesystem.gui.hud" + local VictoryScreen = require "scenes.battlesystem.screens.victory" function BattleSystem:new() @@ -28,13 +30,16 @@ function BattleSystem:initManagers() self.datas = {} self.world = World(self) self.menu = MenuSystem(self) + self.hud = HUD(self) end function BattleSystem:startBattle() self.world: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) @@ -49,6 +54,7 @@ end function BattleSystem:update(dt) self.world:update(dt) + self.hud:update(dt) if (self.screen ~= nil) then self.screen:update(dt) end @@ -56,6 +62,7 @@ end function BattleSystem:draw() self.world:draw() + self.hud:draw() if (self.screen ~= nil) then self.screen:draw() end diff --git a/sonic-radiance.love/scenes/battlesystem/world.lua b/sonic-radiance.love/scenes/battlesystem/world.lua index 189b660..39fb591 100644 --- a/sonic-radiance.love/scenes/battlesystem/world.lua +++ b/sonic-radiance.love/scenes/battlesystem/world.lua @@ -34,24 +34,17 @@ function World:new(scene, battlefile) self.battlers = {} self.actionlist = {} - self.BattlerCursor = self.turns.current - self.heroNumber = 0 self.ennNumber = 0 - self.playerHUDPosition = -64 - self.map = Map(self, "city") self.cursor = Cursor(self) - self.tweens = TweenManager(self) - self:resetActiveGrid() self:resetEffectGrid() self:initHeroes() self:initEnnemies() - self:initHUD() self.isBattleActive = false end @@ -147,6 +140,8 @@ function World:generateActionList() table.insert(self.actionlist, action) end end + + return self.actionlist end function World:countHeroes() @@ -173,10 +168,6 @@ function World:countEnnemies() return count end -function World:getPlayerHUDPosition() - return self.playerHUDPosition -end - -- UPDATE FUNCTION -- Update all actors @@ -190,7 +181,6 @@ function World:update(dt) end self.cursor:update(dt) - self.tweens:update(dt) self.map:update(dt) end @@ -213,7 +203,6 @@ end function World:startBattle() self.isBattleActive = true - self.tweens:newTween(0, 0.4, {playerHUDPosition = 36}, 'inCubic') end function World:recalculateTurns() @@ -240,7 +229,7 @@ function World:switchActiveBattler() self:selectNextAction() self.turns.changeBattler = false - self.tweens:newTween(0, 0.2, {BattlerCursor = self.turns.current}, 'inCubic') + self.scene.hud:moveBattleCursor(self.turns.current) end end @@ -262,7 +251,6 @@ end function World:finishBattle() self.isBattleActive = false - self.tweens:newTween(0, 0.4, {playerHUDPosition = -64}, 'inCubic') self.actionlist = {} self.scene:finishBattle() end @@ -303,8 +291,6 @@ function World:draw() self:drawShadows() self:drawActors() self.cursor:drawTop() - - self:drawHUD() end function World:drawActors() @@ -319,36 +305,6 @@ function World:drawShadows() end end -function World:initHUD() - self.frame = gui.newBorder(424, 30, 4) -end - -function World:drawHUD() - for i, battler in ipairs(self.battlers) do - battler:drawHUD() - end - - for i, action in ipairs(self.actionlist) do - action.actor:drawIcon(4 + (i-1)*(20), 6) - end - local cursorx = self.BattlerCursor * 20 - 6 - - if #self.actionlist > 0 then - self.assets.images["menucursor"]:draw(cursorx, 26, math.rad(-90), 1, 1, 4, 8) - end - - local x, y = 362, 3 - - love.graphics.draw(self.frame, 424, 20, 0, -1, -1) - self.assets.images["hudturn"]:draw(x, y) - self.assets.fonts["hudnbrs"]:set() - local turnnbr = self.turns.number - if (turnnbr < 10) then - turnnbr = "0" .. turnnbr - end - love.graphics.print(turnnbr, x + 33, y + 1) -end - return World