chore: separate HUD from world logic

This commit is contained in:
Kazhnuz 2020-05-01 14:19:00 +02:00
parent 9bf6be1364
commit 873cf12140
3 changed files with 75 additions and 47 deletions

View file

@ -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

View file

@ -5,6 +5,8 @@ local BattleSystem = Scene:extend()
local World = require "scenes.battlesystem.world" local World = require "scenes.battlesystem.world"
local MenuSystem = require "scenes.battlesystem.menu" local MenuSystem = require "scenes.battlesystem.menu"
local HUD = require "scenes.battlesystem.gui.hud"
local VictoryScreen = require "scenes.battlesystem.screens.victory" local VictoryScreen = require "scenes.battlesystem.screens.victory"
function BattleSystem:new() function BattleSystem:new()
@ -28,13 +30,16 @@ function BattleSystem:initManagers()
self.datas = {} self.datas = {}
self.world = World(self) self.world = World(self)
self.menu = MenuSystem(self) self.menu = MenuSystem(self)
self.hud = HUD(self)
end end
function BattleSystem:startBattle() function BattleSystem:startBattle()
self.world:startBattle() self.world:startBattle()
self.hud:movePlayerHUD(true)
end end
function BattleSystem:finishBattle() function BattleSystem:finishBattle()
self.hud:movePlayerHUD(false)
self.assets:setMusic("assets/music/victory.mp3") self.assets:setMusic("assets/music/victory.mp3")
self.assets:playMusic() self.assets:playMusic()
self.screen = VictoryScreen(self) self.screen = VictoryScreen(self)
@ -49,6 +54,7 @@ end
function BattleSystem:update(dt) function BattleSystem:update(dt)
self.world:update(dt) self.world:update(dt)
self.hud:update(dt)
if (self.screen ~= nil) then if (self.screen ~= nil) then
self.screen:update(dt) self.screen:update(dt)
end end
@ -56,6 +62,7 @@ end
function BattleSystem:draw() function BattleSystem:draw()
self.world:draw() self.world:draw()
self.hud:draw()
if (self.screen ~= nil) then if (self.screen ~= nil) then
self.screen:draw() self.screen:draw()
end end

View file

@ -34,24 +34,17 @@ function World:new(scene, battlefile)
self.battlers = {} self.battlers = {}
self.actionlist = {} self.actionlist = {}
self.BattlerCursor = self.turns.current
self.heroNumber = 0 self.heroNumber = 0
self.ennNumber = 0 self.ennNumber = 0
self.playerHUDPosition = -64
self.map = Map(self, "city") self.map = Map(self, "city")
self.cursor = Cursor(self) self.cursor = Cursor(self)
self.tweens = TweenManager(self)
self:resetActiveGrid() self:resetActiveGrid()
self:resetEffectGrid() self:resetEffectGrid()
self:initHeroes() self:initHeroes()
self:initEnnemies() self:initEnnemies()
self:initHUD()
self.isBattleActive = false self.isBattleActive = false
end end
@ -147,6 +140,8 @@ function World:generateActionList()
table.insert(self.actionlist, action) table.insert(self.actionlist, action)
end end
end end
return self.actionlist
end end
function World:countHeroes() function World:countHeroes()
@ -173,10 +168,6 @@ function World:countEnnemies()
return count return count
end end
function World:getPlayerHUDPosition()
return self.playerHUDPosition
end
-- UPDATE FUNCTION -- UPDATE FUNCTION
-- Update all actors -- Update all actors
@ -190,7 +181,6 @@ function World:update(dt)
end end
self.cursor:update(dt) self.cursor:update(dt)
self.tweens:update(dt)
self.map:update(dt) self.map:update(dt)
end end
@ -213,7 +203,6 @@ end
function World:startBattle() function World:startBattle()
self.isBattleActive = true self.isBattleActive = true
self.tweens:newTween(0, 0.4, {playerHUDPosition = 36}, 'inCubic')
end end
function World:recalculateTurns() function World:recalculateTurns()
@ -240,7 +229,7 @@ function World:switchActiveBattler()
self:selectNextAction() self:selectNextAction()
self.turns.changeBattler = false self.turns.changeBattler = false
self.tweens:newTween(0, 0.2, {BattlerCursor = self.turns.current}, 'inCubic') self.scene.hud:moveBattleCursor(self.turns.current)
end end
end end
@ -262,7 +251,6 @@ end
function World:finishBattle() function World:finishBattle()
self.isBattleActive = false self.isBattleActive = false
self.tweens:newTween(0, 0.4, {playerHUDPosition = -64}, 'inCubic')
self.actionlist = {} self.actionlist = {}
self.scene:finishBattle() self.scene:finishBattle()
end end
@ -303,8 +291,6 @@ function World:draw()
self:drawShadows() self:drawShadows()
self:drawActors() self:drawActors()
self.cursor:drawTop() self.cursor:drawTop()
self:drawHUD()
end end
function World:drawActors() function World:drawActors()
@ -319,36 +305,6 @@ function World:drawShadows()
end end
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 return World