From a1d81b9170461341646894f848b14511d93fb7fc Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 18 Apr 2021 17:44:00 +0200 Subject: [PATCH] feat: add a common message queue Fixes #89 --- .../game/modules/messagequeue/init.lua | 37 ++++++++++++++++ .../game/modules/messagequeue/message.lua | 42 +++++++++++++++++++ sonic-radiance.love/main.lua | 2 +- .../scenes/battlesystem/gui/hud.lua | 10 +++-- sonic-radiance.love/scenes/overworld/init.lua | 18 +++----- 5 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 sonic-radiance.love/game/modules/messagequeue/init.lua create mode 100644 sonic-radiance.love/game/modules/messagequeue/message.lua diff --git a/sonic-radiance.love/game/modules/messagequeue/init.lua b/sonic-radiance.love/game/modules/messagequeue/init.lua new file mode 100644 index 0000000..2947ba2 --- /dev/null +++ b/sonic-radiance.love/game/modules/messagequeue/init.lua @@ -0,0 +1,37 @@ +local MessageQueue = Object:extend() +local Message = require "game.modules.messagequeue.message" +local MAX = 3 + +function MessageQueue:new(scene) + self.messages = {} + self.permaMessage = nil + self.scene = scene +end + +function MessageQueue:addMessage(newMessage) + for _, message in ipairs(self.messages) do + message:move() + end + table.insert(self.messages, 1, Message(self.scene, newMessage, false)) + self.messages[MAX + 1] = nil +end + +function MessageQueue:pinMessage(message) + if (self.permaMessage ~= nil and self.permaMessage.text ~= message) then + self.permaMessage = Message(self.scene, message, true) + end +end + +function MessageQueue:update(dt) + for _, message in ipairs(self.messages) do + message:update(dt) + end +end + +function MessageQueue:draw() + for _, message in ipairs(self.messages) do + message:draw() + end +end + +return MessageQueue \ No newline at end of file diff --git a/sonic-radiance.love/game/modules/messagequeue/message.lua b/sonic-radiance.love/game/modules/messagequeue/message.lua new file mode 100644 index 0000000..9fb5d04 --- /dev/null +++ b/sonic-radiance.love/game/modules/messagequeue/message.lua @@ -0,0 +1,42 @@ +local Message = Object:extend() +local TweenManager = require "game.modules.tweenmanager" + +local SPACING = 20 +local PLAYER_MESSAGE = 240 - 24 + +function Message:new(scene, message, isPinned) + self.message = message + self.scene = scene + assert(message ~= nil, "the message must be set") + self.isPinned = isPinned or false + self.opacity = 0 + self.y = 0 + + self.tweens = TweenManager(self) + self.tweens:newTween(0, 0.2, {opacity = 1}, "inOutCubic") + self.tweens:newTween(1, 0.2, {opacity = 0}, "inOutCubic") +end + +function Message:move() + self.tweens:newTween(0, 0.2, {y = self.y - SPACING}, "inOutCubic") +end + +function Message:update(dt) + self.tweens:update(dt) +end + +function Message:draw() + local yTransparency = math.max(0, math.min(1, (math.abs(self.y) - 32) / SPACING)) + local opacity = math.max(0, math.min(1, self.opacity - yTransparency)) + + love.graphics.setColor(0, 0, 0, 0.5 * opacity) + if (opacity > 0) then + love.graphics.rectangle("fill", 0, PLAYER_MESSAGE + self.y, 424, 16) + self.scene.assets.fonts["small"]:setColor(1, 1, 1, opacity) + self.scene.assets.fonts["small"]:draw(self.message, 424 / 2, PLAYER_MESSAGE - 1 + self.y, -1, "center") + self.scene.assets.fonts["small"]:setColor(1, 1, 1, 1) + end + utils.graphics.resetColor() +end + +return Message diff --git a/sonic-radiance.love/main.lua b/sonic-radiance.love/main.lua index 74204c2..e5406ca 100644 --- a/sonic-radiance.love/main.lua +++ b/sonic-radiance.love/main.lua @@ -27,7 +27,7 @@ local Game = require "game" scenes = require "scenes" function love.load() - core = Core(false) + core = Core(true) game = Game() if (core.debug.active) then scenes.debug.menu() diff --git a/sonic-radiance.love/scenes/battlesystem/gui/hud.lua b/sonic-radiance.love/scenes/battlesystem/gui/hud.lua index 47e9127..905f487 100644 --- a/sonic-radiance.love/scenes/battlesystem/gui/hud.lua +++ b/sonic-radiance.love/scenes/battlesystem/gui/hud.lua @@ -2,6 +2,7 @@ local HUD = Object:extend() local gui = require "game.modules.gui" local TweenManager = require "game.modules.tweenmanager" +local MessageQueue = require "game.modules.messagequeue" local PLAYER_HUD_HIDDEN = 240+64 local PLAYER_HUD_VISIBLE = 240-44 @@ -19,19 +20,19 @@ function HUD:new(turns) self.playerHUDPosition = PLAYER_HUD_HIDDEN self.battlerCursor = self.turns.turns.current + self.messages = MessageQueue(self.scene) + self.message = "Test de message" self.messageOpacity = 0 end function HUD:showMessage(message) - self.message = message - self.messageOpacity = 1 - self.tweens:newTween(0, 0.2, {messageOpacity = 1}, 'inOutCubic') - self.tweens:newTween(1, 0.2, {messageOpacity = 0}, 'inOutCubic') + self.messages:addMessage(message) end function HUD:update(dt) self.tweens:update(dt) + self.messages:update(dt) end function HUD:movePlayerHUD(beginBattle) @@ -75,6 +76,7 @@ function HUD:draw() self.assets.images["menucursor"]:draw(cursorx, 5, math.rad(90), 1, 1, 4, 16) end + self.messages:draw() love.graphics.setColor(0,0,0, 0.5 * self.messageOpacity) love.graphics.rectangle("fill", 0, PLAYER_MESSAGE, 424, 16) if (self.messageOpacity > 0) then diff --git a/sonic-radiance.love/scenes/overworld/init.lua b/sonic-radiance.love/scenes/overworld/init.lua index bde2665..d45109e 100644 --- a/sonic-radiance.love/scenes/overworld/init.lua +++ b/sonic-radiance.love/scenes/overworld/init.lua @@ -33,6 +33,7 @@ local gui = require "game.modules.gui" local TweenManager = require "game.modules.tweenmanager" local EventManager = require "game.events" +local MessageQueue = require "game.modules.messagequeue" local PLAYER_MESSAGE = 240 - 32 @@ -61,6 +62,8 @@ function OverWorld:new(area, playerx, playery) self.ringBorder = -16 self.tweens:newTween(0, 0.3, {ringBorder=16}, "inOutQuad") self.timeBorder = -10 + + self.messages = MessageQueue(self) self.message = "Test de message" self.messageOpacity = 0 @@ -96,10 +99,7 @@ function OverWorld:endEvent() end function OverWorld:showMessage(message) - self.message = message - self.messageOpacity = 1 - self.tweens:newTween(0, 0.2, {messageOpacity = 1}, 'inOutCubic') - self.tweens:newTween(1, 0.2, {messageOpacity = 0}, 'inOutCubic') + self.messages:addMessage(message) end function OverWorld:registerScreen(screen) @@ -113,6 +113,7 @@ function OverWorld:update(dt) local keys = self:getKeys(1) self.tweens:update(dt) self.events:update(dt) + self.messages:update(dt) if (self.world.isActive) then self.charsetManager:update(dt) @@ -177,14 +178,7 @@ end function OverWorld:draw() self.events:draw() - love.graphics.setColor(0,0,0, 0.5 * self.messageOpacity) - love.graphics.rectangle("fill", 0, PLAYER_MESSAGE, 424, 16) - if (self.messageOpacity > 0) then - self.assets.fonts["small"]:setColor(1,1,1, self.messageOpacity) - self.assets.fonts["small"]:draw(self.message, 424/2, PLAYER_MESSAGE - 1, -1, "center") - self.assets.fonts["small"]:setColor(1,1,1, 1) - end - utils.graphics.resetColor() + self.messages:draw() self:drawScreenBottomLayer() end