Add FloatingTextManager and FloatingText for displaying UI messages; update CollectableComponent and HealthComponent to utilize new floating text features

This commit is contained in:
2025-09-11 04:35:30 +02:00
parent f9cb59d182
commit f229ff5b7d
16 changed files with 131 additions and 139 deletions

View File

@@ -1,56 +0,0 @@
using System.Collections.Generic;
using Godot;
using Mr.BrickAdventures.scripts.components;
using Mr.BrickAdventures.scripts.UI;
namespace Mr.BrickAdventures.Autoloads;
public partial class DamageNumberManager : Node
{
[Export] public PackedScene DamageNumberScene { get; set; }
private readonly List<Node> _managedNodes = [];
public void Register(Node node)
{
if (_managedNodes.Contains(node)) return;
var healthComponent = node.GetNodeOrNull<HealthComponent>("HealthComponent");
if (healthComponent == null)
{
GD.PrintErr($"Node '{node.Name}' tried to register with DamageNumberManager but has no HealthComponent.");
return;
}
healthComponent.HealthChanged += (delta, total) => OnHealthChanged(healthComponent, delta);
node.TreeExiting += () => Unregister(node);
_managedNodes.Add(node);
}
public void Unregister(Node node)
{
var healthComponent = node.GetNodeOrNull<HealthComponent>("HealthComponent");
if (healthComponent != null)
{
healthComponent.HealthChanged -= (delta, _) => OnHealthChanged(healthComponent, delta);
}
_managedNodes.Remove(node);
}
private void OnHealthChanged(HealthComponent healthComponent, float delta)
{
if (delta >= 0) return;
if (DamageNumberScene == null)
{
GD.PrintErr("DamageNumberManager: DamageNumberScene is not set!");
return;
}
var damageNumber = DamageNumberScene.Instantiate<DamageNumber>();
GetTree().CurrentScene.AddChild(damageNumber);
var position = healthComponent.GlobalPosition;
damageNumber.ShowDamage(Mathf.Abs(delta), position);
}
}

View File

@@ -1 +0,0 @@
uid://i06td076ej68

View File

@@ -0,0 +1,52 @@
using System.Globalization;
using Godot;
using Mr.BrickAdventures.scripts.UI;
namespace Mr.BrickAdventures.Autoloads;
public partial class FloatingTextManager : Node
{
[Export] public PackedScene FloatingTextScene { get; set; }
[ExportGroup("Colors")]
[Export] public Color DamageColor { get; set; } = new Color("#ff4b4b"); // Red
[Export] public Color HealColor { get; set; } = new Color("#4bff65"); // Green
[Export] public Color CoinColor { get; set; } = new Color("#ffd700"); // Gold
[Export] public Color MessageColor { get; set; } = new Color("#ffffff"); // White
public void ShowDamage(float amount, Vector2 position)
{
var text = Mathf.Round(amount * 100f).ToString(CultureInfo.InvariantCulture);
CreateFloatingText(text, position, DamageColor);
}
public void ShowHeal(float amount, Vector2 position)
{
var text = $"+{Mathf.Round(amount)}";
CreateFloatingText(text, position, HealColor);
}
public void ShowCoin(int amount, Vector2 position)
{
var text = $"+{amount}";
CreateFloatingText(text, position, CoinColor);
}
public void ShowMessage(string message, Vector2 position)
{
CreateFloatingText(message, position, MessageColor);
}
private void CreateFloatingText(string text, Vector2 position, Color color)
{
if (FloatingTextScene == null)
{
GD.PushError("FloatingTextManager: FloatingTextScene is not set!");
return;
}
var popup = FloatingTextScene.Instantiate<FloatingText>();
GetTree().CurrentScene.AddChild(popup);
popup.Show(text, position, color);
}
}