feat: add a common message queue

Fixes #89
This commit is contained in:
Kazhnuz 2021-04-18 17:44:00 +02:00
parent e62e97b2d7
commit a1d81b9170
5 changed files with 92 additions and 17 deletions

View file

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

View file

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

View file

@ -27,7 +27,7 @@ local Game = require "game"
scenes = require "scenes" scenes = require "scenes"
function love.load() function love.load()
core = Core(false) core = Core(true)
game = Game() game = Game()
if (core.debug.active) then if (core.debug.active) then
scenes.debug.menu() scenes.debug.menu()

View file

@ -2,6 +2,7 @@ local HUD = Object:extend()
local gui = require "game.modules.gui" local gui = require "game.modules.gui"
local TweenManager = require "game.modules.tweenmanager" local TweenManager = require "game.modules.tweenmanager"
local MessageQueue = require "game.modules.messagequeue"
local PLAYER_HUD_HIDDEN = 240+64 local PLAYER_HUD_HIDDEN = 240+64
local PLAYER_HUD_VISIBLE = 240-44 local PLAYER_HUD_VISIBLE = 240-44
@ -19,19 +20,19 @@ function HUD:new(turns)
self.playerHUDPosition = PLAYER_HUD_HIDDEN self.playerHUDPosition = PLAYER_HUD_HIDDEN
self.battlerCursor = self.turns.turns.current self.battlerCursor = self.turns.turns.current
self.messages = MessageQueue(self.scene)
self.message = "Test de message" self.message = "Test de message"
self.messageOpacity = 0 self.messageOpacity = 0
end end
function HUD:showMessage(message) function HUD:showMessage(message)
self.message = message self.messages:addMessage(message)
self.messageOpacity = 1
self.tweens:newTween(0, 0.2, {messageOpacity = 1}, 'inOutCubic')
self.tweens:newTween(1, 0.2, {messageOpacity = 0}, 'inOutCubic')
end end
function HUD:update(dt) function HUD:update(dt)
self.tweens:update(dt) self.tweens:update(dt)
self.messages:update(dt)
end end
function HUD:movePlayerHUD(beginBattle) 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) self.assets.images["menucursor"]:draw(cursorx, 5, math.rad(90), 1, 1, 4, 16)
end end
self.messages:draw()
love.graphics.setColor(0,0,0, 0.5 * self.messageOpacity) love.graphics.setColor(0,0,0, 0.5 * self.messageOpacity)
love.graphics.rectangle("fill", 0, PLAYER_MESSAGE, 424, 16) love.graphics.rectangle("fill", 0, PLAYER_MESSAGE, 424, 16)
if (self.messageOpacity > 0) then if (self.messageOpacity > 0) then

View file

@ -33,6 +33,7 @@ local gui = require "game.modules.gui"
local TweenManager = require "game.modules.tweenmanager" local TweenManager = require "game.modules.tweenmanager"
local EventManager = require "game.events" local EventManager = require "game.events"
local MessageQueue = require "game.modules.messagequeue"
local PLAYER_MESSAGE = 240 - 32 local PLAYER_MESSAGE = 240 - 32
@ -61,6 +62,8 @@ function OverWorld:new(area, playerx, playery)
self.ringBorder = -16 self.ringBorder = -16
self.tweens:newTween(0, 0.3, {ringBorder=16}, "inOutQuad") self.tweens:newTween(0, 0.3, {ringBorder=16}, "inOutQuad")
self.timeBorder = -10 self.timeBorder = -10
self.messages = MessageQueue(self)
self.message = "Test de message" self.message = "Test de message"
self.messageOpacity = 0 self.messageOpacity = 0
@ -96,10 +99,7 @@ function OverWorld:endEvent()
end end
function OverWorld:showMessage(message) function OverWorld:showMessage(message)
self.message = message self.messages:addMessage(message)
self.messageOpacity = 1
self.tweens:newTween(0, 0.2, {messageOpacity = 1}, 'inOutCubic')
self.tweens:newTween(1, 0.2, {messageOpacity = 0}, 'inOutCubic')
end end
function OverWorld:registerScreen(screen) function OverWorld:registerScreen(screen)
@ -113,6 +113,7 @@ function OverWorld:update(dt)
local keys = self:getKeys(1) local keys = self:getKeys(1)
self.tweens:update(dt) self.tweens:update(dt)
self.events:update(dt) self.events:update(dt)
self.messages:update(dt)
if (self.world.isActive) then if (self.world.isActive) then
self.charsetManager:update(dt) self.charsetManager:update(dt)
@ -177,14 +178,7 @@ end
function OverWorld:draw() function OverWorld:draw()
self.events:draw() self.events:draw()
love.graphics.setColor(0,0,0, 0.5 * self.messageOpacity) self.messages:draw()
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:drawScreenBottomLayer() self:drawScreenBottomLayer()
end end