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 +0,0 @@
uid://bbupymh6krrgx

View File

@@ -1,41 +1,35 @@
using System.Globalization;
using Godot;
namespace Mr.BrickAdventures.scripts.UI;
[GlobalClass]
public partial class DamageNumber : Label
public partial class FloatingText : Label
{
[Export] public float Duration { get; set; } = 0.8f;
[Export] public float Duration { get; set; } = 1f;
[Export] public float FallDistance { get; set; } = 40f;
[Export] public float HorizontalDrift { get; set; } = 15f;
public void ShowDamage(float damageAmount, Vector2 position)
public void Show(string textToShow, Vector2 position, Color color)
{
Text = Mathf.Round(damageAmount * 100f).ToString(CultureInfo.InvariantCulture);
Text = textToShow;
GlobalPosition = position;
Modulate = color;
var rng = new RandomNumberGenerator();
var horizontalOffset = rng.RandfRange(-HorizontalDrift, HorizontalDrift);
var startPosition = GlobalPosition;
var endPosition = GlobalPosition + new Vector2(horizontalOffset, FallDistance);
var startColor = Colors.White;
startColor.A = 1f;
Modulate = startColor;
var tween = CreateTween();
tween.SetParallel();
var tween = CreateTween().SetParallel();
tween.TweenProperty(this, "global_position", endPosition, Duration)
.SetTrans(Tween.TransitionType.Quad)
.SetEase(Tween.EaseType.In);
.SetEase(Tween.EaseType.Out);
tween.Chain().TweenProperty(this, "modulate:a", 0f, Duration * 0.5f)
.SetTrans(Tween.TransitionType.Sine)
.SetEase(Tween.EaseType.Out);
.SetEase(Tween.EaseType.In);
tween.TweenCallback(Callable.From(QueueFree));
}
}

View File

@@ -1,5 +1,6 @@
using System;
using Godot;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.components;
@@ -15,6 +16,8 @@ public partial class CollectableComponent : Node
[Export] public AudioStreamPlayer2D Sfx {get; set; }
[Signal] public delegate void CollectedEventHandler(float amount, CollectableType type, Node2D body);
private FloatingTextManager _floatingTextManager;
public override void _Ready()
{
@@ -25,6 +28,8 @@ public partial class CollectableComponent : Node
if (Owner.HasNode("FadeAwayComponent"))
_hasFadeAway = true;
_floatingTextManager = GetNode<FloatingTextManager>("/root/FloatingTextManager");
}
private async void OnArea2DBodyEntered(Node2D body)
@@ -32,6 +37,22 @@ public partial class CollectableComponent : Node
try
{
if (!body.HasNode("CanPickUpComponent")) return;
if (Owner is Node2D ownerNode)
{
switch (Data.Type)
{
case CollectableType.Coin:
_floatingTextManager?.ShowCoin((int)Data.Amount, ownerNode.GlobalPosition);
break;
case CollectableType.Health:
_floatingTextManager?.ShowMessage("Healed!", ownerNode.GlobalPosition);
break;
case CollectableType.Kid:
_floatingTextManager?.ShowMessage("Rescued!", ownerNode.GlobalPosition);
break;
}
}
EmitSignalCollected(Data.Amount, Data.Type, body);
CollisionShape?.CallDeferred("set_disabled", true);

View File

@@ -15,12 +15,11 @@ public partial class HealthComponent : Node2D
[Signal] public delegate void HealthChangedEventHandler(float delta, float totalHealth);
[Signal] public delegate void DeathEventHandler();
private DamageNumberManager _damageNumberManager;
private FloatingTextManager _floatingTextManager;
public override void _Ready()
{
_damageNumberManager = GetNode<DamageNumberManager>("/root/DamageNumberManager");
_damageNumberManager?.Register(Owner);
_floatingTextManager = GetNode<FloatingTextManager>("/root/FloatingTextManager");
}
public void SetHealth(float newValue)
@@ -47,6 +46,11 @@ public partial class HealthComponent : Node2D
if (delta == 0.0f)
return;
if (delta < 0.0f)
_floatingTextManager?.ShowDamage(Mathf.Abs(delta), GlobalPosition);
else
_floatingTextManager?.ShowHeal(delta, GlobalPosition);
if (playSfx)
{