From 2d0139c3275d5c0bdaedc23d38da4d744870020d Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 1 Sep 2025 18:32:45 -0300 Subject: Centralização de logs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/GUI/MessageLog.cs | 23 +++++------------ scripts/Game.cs | 2 +- scripts/Utils/MessageLogData.cs | 48 +++++++++++++++++++++++++++++++++++ scripts/Utils/MessageLogData.cs.uid | 1 + scripts/Utils/SignalBus.cs | 6 ----- scripts/actors/Actor.cs | 2 +- scripts/actors/actions/MeleeAction.cs | 2 +- 7 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 scripts/Utils/MessageLogData.cs create mode 100644 scripts/Utils/MessageLogData.cs.uid (limited to 'scripts') diff --git a/scripts/GUI/MessageLog.cs b/scripts/GUI/MessageLog.cs index ebfb488..d481b19 100644 --- a/scripts/GUI/MessageLog.cs +++ b/scripts/GUI/MessageLog.cs @@ -4,34 +4,23 @@ using System.Threading.Tasks; public partial class MessageLog : ScrollContainer { - private Message lastMessage = null; private VBoxContainer MessageList; public override void _Ready() { base._Ready(); MessageList = GetNode("MessageList"); - SignalBus.Instance.messageSent += async (string text) => await AddMessageAsync(text); - } - private async Task AddMessageAsync(string text) { - if (lastMessage != null && lastMessage.PlainText == text) { - lastMessage.Count++; - return; + foreach (Message msg in MessageLogData.Instance.Messages) { + _ = AddMessageAsync(msg); } - Message message = new(text); - lastMessage = message; + MessageLogData.Instance.messageSent += async (Message msg) => await AddMessageAsync(msg); + } + + private async Task AddMessageAsync(Message message) { MessageList.AddChild(message); await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); EnsureControlVisible(message); } - - /// - /// Método estático disponível em todo o escopo do jogo. - /// - /// - public static void SendMessage(string text) { - SignalBus.Instance.EmitSignal(SignalBus.SignalName.messageSent, text); - } } diff --git a/scripts/Game.cs b/scripts/Game.cs index 3050e22..f25776e 100644 --- a/scripts/Game.cs +++ b/scripts/Game.cs @@ -48,7 +48,7 @@ public partial class Game : Node { turnManager = new(Map); - MessageLog.SendMessage("UMA FILA DE HOMENS EJACULANDO NA BOCA DA DALVA"); + MessageLogData.Instance.AddMessage("Boa sorte!"); } /// diff --git a/scripts/Utils/MessageLogData.cs b/scripts/Utils/MessageLogData.cs new file mode 100644 index 0000000..ed30062 --- /dev/null +++ b/scripts/Utils/MessageLogData.cs @@ -0,0 +1,48 @@ +using Godot; + +public partial class MessageLogData : Node +{ + public static MessageLogData Instance { get; private set; } + private Godot.Collections.Array messages = []; + public Godot.Collections.Array Messages {get => messages;} + +private Message LastMessage { + get { + if (messages.Count <= 0) { + return null; + } + return messages[^1]; + } + } + + public void ClearMessages() { + foreach (Message message in messages) { + message.QueueFree(); + } + } + + public override void _Ready() + { + base._Ready(); + Instance = this; + } + + public void AddMessage(string text) { + if (LastMessage != null && LastMessage.PlainText == text) { + LastMessage.Count++; + return; + } + + Message message = new(text); + messages.Add(message); + EmitSignal(SignalName.messageSent, message); + } + + /// + /// Acionado sempre que uma mensagem for adicionada para o log. + /// + /// Mensagem. + [Signal] + public delegate void messageSentEventHandler(Message message); + +} \ No newline at end of file diff --git a/scripts/Utils/MessageLogData.cs.uid b/scripts/Utils/MessageLogData.cs.uid new file mode 100644 index 0000000..0b8f55f --- /dev/null +++ b/scripts/Utils/MessageLogData.cs.uid @@ -0,0 +1 @@ +uid://dgb6vauj2v8sm diff --git a/scripts/Utils/SignalBus.cs b/scripts/Utils/SignalBus.cs index 7408db2..f5cb0d2 100644 --- a/scripts/Utils/SignalBus.cs +++ b/scripts/Utils/SignalBus.cs @@ -16,10 +16,4 @@ public partial class SignalBus : Node base._Ready(); Instance = this; } - /// - /// Acionado sempre que uma mensagem for adicionada para o log. - /// - /// Mensagem. - [Signal] - public delegate void messageSentEventHandler(string text); } diff --git a/scripts/actors/Actor.cs b/scripts/actors/Actor.cs index c03c8fa..2d22d44 100644 --- a/scripts/actors/Actor.cs +++ b/scripts/actors/Actor.cs @@ -236,7 +236,7 @@ public abstract partial class Actor : Sprite2D deathMessage = $"{ActorName} morreu!"; } - MessageLog.SendMessage(deathMessage); + MessageLogData.Instance.AddMessage(deathMessage); Texture = definition.deathTexture; blocksMovement = false; diff --git a/scripts/actors/actions/MeleeAction.cs b/scripts/actors/actions/MeleeAction.cs index e739159..ae94eff 100644 --- a/scripts/actors/actions/MeleeAction.cs +++ b/scripts/actors/actions/MeleeAction.cs @@ -33,6 +33,6 @@ public partial class MeleeAction : DirectionalAction attackDesc += $" mas {target.ActorName} tem músculos de aço."; } - MessageLog.SendMessage(attackDesc); + MessageLogData.Instance.AddMessage(attackDesc); } } -- cgit v1.2.3